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.Reflection;
|
||||
using log4net;
|
||||
using Managers;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
@ -9,62 +10,15 @@ public class AudioLibrary : MonoBehaviour
|
||||
{
|
||||
private static ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
|
||||
public List<GameObject> audios;
|
||||
public string audioAssetsBundleName = "audio";
|
||||
public string manageableAudioFolder = "Assets/Prefabs/Audio";
|
||||
private static readonly string manageableAudioFolder = "Assets/Prefabs/Audio";
|
||||
private static readonly string audioAssetsBundleName = "audio";
|
||||
|
||||
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
|
||||
string[] files = Directory.GetFiles(manageableAudioFolder, "*.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<ManageableAudio>(out _))
|
||||
{
|
||||
audios.Add(go);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Warn("Audio library can only load prefabs with ManageableAudio components!");
|
||||
}
|
||||
}
|
||||
GameManager.LoadPrefabsFromFolder<ManageableAudio>(manageableAudioFolder, audios);
|
||||
#else
|
||||
var audioAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, audioAssetsBundleName));
|
||||
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!");
|
||||
}
|
||||
}
|
||||
GameManager.LoadPrefabsFromBundle<ManageableAudio>(audioAssetsBundleName, audios);
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
@ -23,8 +23,8 @@ namespace Managers
|
||||
/// </summary>
|
||||
public static CharacterManager G { get; private set; }
|
||||
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 characterAssetsBundleName = "characters";
|
||||
|
||||
void Awake()
|
||||
{
|
||||
@ -44,20 +44,9 @@ namespace Managers
|
||||
private void LoadAvailableCharacters()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
string[] files = Directory.GetFiles(characterAssetsPath, "*.asset",
|
||||
SearchOption.TopDirectoryOnly);
|
||||
foreach (var file in files)
|
||||
{
|
||||
AvailableShips.Add(AssetDatabase.LoadAssetAtPath<ShipProperties>(file));
|
||||
}
|
||||
GameManager.LoadAssetsFromFolder(characterAssetsPath, AvailableShips);
|
||||
#else
|
||||
var characterAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, characterAssetsBundleName));
|
||||
if (characterAssetBundle == null)
|
||||
{
|
||||
Log.Error("Failed to load rules asset bundle!");
|
||||
return;
|
||||
}
|
||||
AvailableShips.AddRange(characterAssetBundle.LoadAllAssets<ShipProperties>());
|
||||
GameManager.LoadAssetsFromBundle(characterAssetsBundleName, AvailableShips);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@ -2,13 +2,13 @@ using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using log4net;
|
||||
using log4net.Config;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.SceneManagement;
|
||||
using FishNet;
|
||||
using System.Collections.Generic;
|
||||
using log4net;
|
||||
using UnityEditor;
|
||||
/// <summary>
|
||||
/// The available scenes in the order they are in the build settings.
|
||||
/// </summary>
|
||||
@ -324,6 +324,107 @@ namespace Managers
|
||||
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>
|
||||
|
||||
@ -56,10 +56,10 @@ namespace Managers
|
||||
{
|
||||
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 ruleAssetsPath = "Assets/ScriptedAssets/Rules";
|
||||
private static readonly string arenaAssetsBundleName = "arenas";
|
||||
private static readonly string ruleAssetsBundleName = "rules";
|
||||
private GameResult CurrentMatchResult;
|
||||
|
||||
public GameObject MatchArena;
|
||||
@ -110,50 +110,26 @@ namespace Managers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the different rules/game modes which are available,
|
||||
/// at the fixed asset path.
|
||||
/// Loads the different rules/game modes from the projects assets.
|
||||
/// </summary>
|
||||
private void LoadAvailableRules()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
string[] files = Directory.GetFiles(arenaAssetsPath, "*.asset",
|
||||
SearchOption.TopDirectoryOnly);
|
||||
foreach (var file in files)
|
||||
{
|
||||
AvailableArenas.Add(AssetDatabase.LoadAssetAtPath<Arena>(file));
|
||||
}
|
||||
GameManager.LoadAssetsFromFolder(ruleAssetsPath, AvailableRules);
|
||||
#else
|
||||
var ruleAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, ruleAssetsBundleName));
|
||||
if (ruleAssetBundle == null)
|
||||
{
|
||||
Log.Error("Failed to load rules asset bundle!");
|
||||
return;
|
||||
}
|
||||
AvailableRules.AddRange(ruleAssetBundle.LoadAllAssets<MatchRule>());
|
||||
GameManager.LoadAssetsFromBundle(ruleAssetsBundleName, AvailableRules);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the characters a player can choose from a fixed path
|
||||
/// in the project.
|
||||
/// Loads the characters a player can choose from the projects assets.
|
||||
/// </summary>
|
||||
private void LoadAvailableArenas()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
string[] files = Directory.GetFiles(ruleAssetsPath, "*.asset",
|
||||
SearchOption.TopDirectoryOnly);
|
||||
foreach (var file in files)
|
||||
{
|
||||
AvailableRules.Add(AssetDatabase.LoadAssetAtPath<MatchRule>(file));
|
||||
}
|
||||
GameManager.LoadAssetsFromFolder(arenaAssetsPath, AvailableArenas);
|
||||
#else
|
||||
var arenasAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, arenaAssetsBundleName));
|
||||
if (arenasAssetBundle == null)
|
||||
{
|
||||
Log.Error("Failed to load arenas asset bundle!");
|
||||
return;
|
||||
}
|
||||
AvailableArenas.AddRange(arenasAssetBundle.LoadAllAssets<Arena>());
|
||||
GameManager.LoadAssetsFromBundle(arenaAssetsBundleName, AvailableArenas);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -276,11 +252,12 @@ namespace Managers
|
||||
await Tween.Delay(2f * 0.66f);
|
||||
|
||||
UIManager.G.ShowMatchEndMenu(MatchArena.transform);
|
||||
|
||||
MatchCamera.SetActive(false);
|
||||
}
|
||||
|
||||
async public void StartRematch()
|
||||
{
|
||||
MatchCamera.SetActive(true);
|
||||
ResetMatch();
|
||||
|
||||
UIManager.G.Announcments.QueueAnnounceText("Starting rematch.", 0.3f);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user