feat: improved loading from Files/AssetBundles depending on the environment -> Editor or Built Executable
This commit is contained in:
parent
f2d1fbfe5b
commit
29f2bd1bec
@ -2,6 +2,7 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using log4net;
|
using log4net;
|
||||||
|
using Managers;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
@ -9,62 +10,15 @@ public class AudioLibrary : MonoBehaviour
|
|||||||
{
|
{
|
||||||
private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
||||||
public List<GameObject> audios;
|
public List<GameObject> audios;
|
||||||
public string audioAssetsBundleName = "audio";
|
private static readonly string manageableAudioFolder = "Assets/Prefabs/Audio";
|
||||||
public string manageableAudioFolder = "Assets/Prefabs/Audio";
|
private static readonly string audioAssetsBundleName = "audio";
|
||||||
|
|
||||||
public void Awake()
|
public void Awake()
|
||||||
{
|
|
||||||
LoadAvailableSounds();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Loads all ManageableAudioPrefabs from an asset bundle if built or
|
|
||||||
/// from the assets folder when in editor.
|
|
||||||
/// TODO: DO NOT FORGET TO manually BUILD ASSET BUNDLES WHEN BUILDING
|
|
||||||
/// </summary>
|
|
||||||
private void LoadAvailableSounds()
|
|
||||||
{
|
{
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
string[] files = Directory.GetFiles(manageableAudioFolder, "*.prefab",
|
GameManager.LoadPrefabsFromFolder<ManageableAudio>(manageableAudioFolder, audios);
|
||||||
SearchOption.TopDirectoryOnly);
|
|
||||||
List<GameObject> gos = new List<GameObject>();
|
|
||||||
foreach (var file in files)
|
|
||||||
{
|
|
||||||
gos.Add(AssetDatabase.LoadAssetAtPath<GameObject>(file));
|
|
||||||
}
|
|
||||||
foreach (GameObject go in gos)
|
|
||||||
{
|
|
||||||
if (go.TryGetComponent<ManageableAudio>(out _))
|
|
||||||
{
|
|
||||||
audios.Add(go);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log.Warn("Audio library can only load prefabs with ManageableAudio components!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
var audioAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, audioAssetsBundleName));
|
GameManager.LoadPrefabsFromBundle<ManageableAudio>(audioAssetsBundleName, audios);
|
||||||
if (audioAssetBundle == null)
|
|
||||||
{
|
|
||||||
Log.Error("Failed to load arenas asset bundle!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
List<GameObject> gos = new List<GameObject>();
|
|
||||||
gos.AddRange(audioAssetBundle.LoadAllAssets<GameObject>());
|
|
||||||
|
|
||||||
foreach (GameObject go in gos)
|
|
||||||
{
|
|
||||||
if (go.TryGetComponent<ManageableAudio>(out _))
|
|
||||||
{
|
|
||||||
audios.Add(go);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log.Warn("Audio library can only load prefabs with ManageableAudio components!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -23,8 +23,8 @@ namespace Managers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static CharacterManager G { get; private set; }
|
public static CharacterManager G { get; private set; }
|
||||||
public List<ShipProperties> AvailableShips { get; private set; } = new List<ShipProperties>();
|
public List<ShipProperties> AvailableShips { get; private set; } = new List<ShipProperties>();
|
||||||
private static readonly string characterAssetsBundleName = "characters";
|
|
||||||
private static readonly string characterAssetsPath = "Assets/ScriptedAssets/Characters";
|
private static readonly string characterAssetsPath = "Assets/ScriptedAssets/Characters";
|
||||||
|
private static readonly string characterAssetsBundleName = "characters";
|
||||||
|
|
||||||
void Awake()
|
void Awake()
|
||||||
{
|
{
|
||||||
@ -44,20 +44,9 @@ namespace Managers
|
|||||||
private void LoadAvailableCharacters()
|
private void LoadAvailableCharacters()
|
||||||
{
|
{
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
string[] files = Directory.GetFiles(characterAssetsPath, "*.asset",
|
GameManager.LoadAssetsFromFolder(characterAssetsPath, AvailableShips);
|
||||||
SearchOption.TopDirectoryOnly);
|
|
||||||
foreach (var file in files)
|
|
||||||
{
|
|
||||||
AvailableShips.Add(AssetDatabase.LoadAssetAtPath<ShipProperties>(file));
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
var characterAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, characterAssetsBundleName));
|
GameManager.LoadAssetsFromBundle(characterAssetsBundleName, AvailableShips);
|
||||||
if (characterAssetBundle == null)
|
|
||||||
{
|
|
||||||
Log.Error("Failed to load rules asset bundle!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
AvailableShips.AddRange(characterAssetBundle.LoadAllAssets<ShipProperties>());
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,13 +2,13 @@ using System;
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using log4net;
|
|
||||||
using log4net.Config;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Events;
|
using UnityEngine.Events;
|
||||||
using UnityEngine.SceneManagement;
|
using UnityEngine.SceneManagement;
|
||||||
using FishNet;
|
using FishNet;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using log4net;
|
||||||
|
using UnityEditor;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The available scenes in the order they are in the build settings.
|
/// The available scenes in the order they are in the build settings.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -324,6 +324,107 @@ namespace Managers
|
|||||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads all prefabs/gameobjects from a folder if the contain the given type
|
||||||
|
/// as a component and adds them to a list.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">Path of the folder</param>
|
||||||
|
/// <param name="prefabList">List the loaded prefabs will be added to</param>
|
||||||
|
public static void LoadPrefabsFromFolder<T>(string path, in List<GameObject> prefabList)
|
||||||
|
{
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
string[] files = Directory.GetFiles(path, "*.prefab",
|
||||||
|
SearchOption.TopDirectoryOnly);
|
||||||
|
List<GameObject> gos = new List<GameObject>();
|
||||||
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
gos.Add(AssetDatabase.LoadAssetAtPath<GameObject>(file));
|
||||||
|
}
|
||||||
|
foreach (GameObject go in gos)
|
||||||
|
{
|
||||||
|
if (go.TryGetComponent<T>(out _))
|
||||||
|
{
|
||||||
|
prefabList.Add(go);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.Warn($"GameObject: {go.name} did not contain a {typeof(T).Name} component. It won't be added to the list.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
Log.Error("Can only access prefabs in folders when in editor!");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads all prefabs/gameobjects from an asset bundle if the contain the given type
|
||||||
|
/// as a component and adds them to a list.
|
||||||
|
/// TODO: DO NOT FORGET TO manually BUILD ASSET BUNDLES WHEN BUILDING
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">Name of the asset bundle</param>
|
||||||
|
/// <param name="prefabList">List the loaded prefabs will be added to</param>
|
||||||
|
public static void LoadPrefabsFromBundle<T>(string name, in List<GameObject> prefabList)
|
||||||
|
{
|
||||||
|
var audioAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, name));
|
||||||
|
if (audioAssetBundle == null)
|
||||||
|
{
|
||||||
|
Log.Error("Failed to load arenas asset bundle!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<GameObject> gos = new List<GameObject>();
|
||||||
|
gos.AddRange(audioAssetBundle.LoadAllAssets<GameObject>());
|
||||||
|
|
||||||
|
foreach (GameObject go in gos)
|
||||||
|
{
|
||||||
|
if (go.TryGetComponent<T>(out _))
|
||||||
|
{
|
||||||
|
prefabList.Add(go);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.Warn($"GameObject: {go.name} did not contain a {typeof(T).Name} component. It won't be added to the list.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads all assets from a folder, if they are of the given type and adds them to a list.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">Path of the folder</param>
|
||||||
|
/// <param name="prefabList">List the loaded prefabs will be added to</param>
|
||||||
|
public static void LoadAssetsFromFolder<T>(string path, in List<T> prefabList)
|
||||||
|
where T : UnityEngine.Object
|
||||||
|
{
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
string[] files = Directory.GetFiles(path, "*.asset",
|
||||||
|
SearchOption.TopDirectoryOnly);
|
||||||
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
prefabList.Add(AssetDatabase.LoadAssetAtPath<T>(file));
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
Log.Error("Can only access prefabs in folders when in editor!");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads all assets from an asset bundle, if they are of the given type and adds them to a list.
|
||||||
|
/// TODO: DO NOT FORGET TO manually BUILD ASSET BUNDLES WHEN BUILDING
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">Name of the asset bundle</param>
|
||||||
|
/// <param name="prefabList">List the loaded prefabs will be added to</param>
|
||||||
|
public static void LoadAssetsFromBundle<T>(string name, in List<T> prefabList)
|
||||||
|
where T : UnityEngine.Object
|
||||||
|
{
|
||||||
|
var arenasAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, name));
|
||||||
|
if (arenasAssetBundle == null)
|
||||||
|
{
|
||||||
|
Log.Error("Failed to load arenas asset bundle!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
prefabList.AddRange(arenasAssetBundle.LoadAllAssets<T>());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -56,10 +56,10 @@ namespace Managers
|
|||||||
{
|
{
|
||||||
private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
||||||
|
|
||||||
private static readonly string arenaAssetsBundleName = "arenas";
|
|
||||||
private static readonly string ruleAssetsBundleName = "rules";
|
|
||||||
private static readonly string arenaAssetsPath = "Assets/ScriptedAssets/Arenas";
|
private static readonly string arenaAssetsPath = "Assets/ScriptedAssets/Arenas";
|
||||||
private static readonly string ruleAssetsPath = "Assets/ScriptedAssets/Rules";
|
private static readonly string ruleAssetsPath = "Assets/ScriptedAssets/Rules";
|
||||||
|
private static readonly string arenaAssetsBundleName = "arenas";
|
||||||
|
private static readonly string ruleAssetsBundleName = "rules";
|
||||||
private GameResult CurrentMatchResult;
|
private GameResult CurrentMatchResult;
|
||||||
|
|
||||||
public GameObject MatchArena;
|
public GameObject MatchArena;
|
||||||
@ -110,50 +110,26 @@ namespace Managers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads the different rules/game modes which are available,
|
/// Loads the different rules/game modes from the projects assets.
|
||||||
/// at the fixed asset path.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void LoadAvailableRules()
|
private void LoadAvailableRules()
|
||||||
{
|
{
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
string[] files = Directory.GetFiles(arenaAssetsPath, "*.asset",
|
GameManager.LoadAssetsFromFolder(ruleAssetsPath, AvailableRules);
|
||||||
SearchOption.TopDirectoryOnly);
|
|
||||||
foreach (var file in files)
|
|
||||||
{
|
|
||||||
AvailableArenas.Add(AssetDatabase.LoadAssetAtPath<Arena>(file));
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
var ruleAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, ruleAssetsBundleName));
|
GameManager.LoadAssetsFromBundle(ruleAssetsBundleName, AvailableRules);
|
||||||
if (ruleAssetBundle == null)
|
|
||||||
{
|
|
||||||
Log.Error("Failed to load rules asset bundle!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
AvailableRules.AddRange(ruleAssetBundle.LoadAllAssets<MatchRule>());
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads the characters a player can choose from a fixed path
|
/// Loads the characters a player can choose from the projects assets.
|
||||||
/// in the project.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void LoadAvailableArenas()
|
private void LoadAvailableArenas()
|
||||||
{
|
{
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
string[] files = Directory.GetFiles(ruleAssetsPath, "*.asset",
|
GameManager.LoadAssetsFromFolder(arenaAssetsPath, AvailableArenas);
|
||||||
SearchOption.TopDirectoryOnly);
|
|
||||||
foreach (var file in files)
|
|
||||||
{
|
|
||||||
AvailableRules.Add(AssetDatabase.LoadAssetAtPath<MatchRule>(file));
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
var arenasAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, arenaAssetsBundleName));
|
GameManager.LoadAssetsFromBundle(arenaAssetsBundleName, AvailableArenas);
|
||||||
if (arenasAssetBundle == null)
|
|
||||||
{
|
|
||||||
Log.Error("Failed to load arenas asset bundle!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
AvailableArenas.AddRange(arenasAssetBundle.LoadAllAssets<Arena>());
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,11 +252,12 @@ namespace Managers
|
|||||||
await Tween.Delay(2f * 0.66f);
|
await Tween.Delay(2f * 0.66f);
|
||||||
|
|
||||||
UIManager.G.ShowMatchEndMenu(MatchArena.transform);
|
UIManager.G.ShowMatchEndMenu(MatchArena.transform);
|
||||||
|
MatchCamera.SetActive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
async public void StartRematch()
|
async public void StartRematch()
|
||||||
{
|
{
|
||||||
|
MatchCamera.SetActive(true);
|
||||||
ResetMatch();
|
ResetMatch();
|
||||||
|
|
||||||
UIManager.G.Announcments.QueueAnnounceText("Starting rematch.", 0.3f);
|
UIManager.G.Announcments.QueueAnnounceText("Starting rematch.", 0.3f);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user