Last active
August 6, 2020 23:13
-
-
Save instance-id/01153406046cfdd4bb4092ae259953f8 to your computer and use it in GitHub Desktop.
In process of learning Addressables, this file is not to be taken seriously. Mostly just tests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using instance.id.ECS.Editor; | |
using Sirenix.OdinInspector; | |
using Unity.Entities; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.AddressableAssets; | |
using UnityEngine.ResourceManagement.AsyncOperations; | |
namespace instance.id.ECS | |
{ | |
public class LoadingHandler : SerializedMonoBehaviour | |
{ | |
private bool editorLoading; | |
private List<Task> tasks = new List<Task>(); | |
private void Awake() | |
{ | |
if (WaypointECSSystem.Instance.loadAddressables && Application.isPlaying) | |
{ | |
Init(); | |
} | |
} | |
#if UNITY_EDITOR | |
private static LoadingHandler instance; | |
public static LoadingHandler Instance => instance; | |
private LoadingHandler() | |
{ | |
instance = this; | |
} | |
[MenuItem("Tools/instance.id/Load Scene Assets")] | |
private static void LoadSceneAssets() | |
{ | |
Instance.Init(true); | |
} | |
[MenuItem("Tools/instance.id/Unload Scene Assets")] | |
private static void UnloadSceneAssets() | |
{ | |
Instance.DestroyObjects(); | |
} | |
[BoxGroup("Addressables")] | |
[TitleGroup("Addressables/Waypoints")] | |
[Button("Load Assets", ButtonSizes.Small)] | |
#endif | |
public async void Init(bool editorLoad = false) | |
{ | |
editorLoading = editorLoad; | |
if (!editorLoading) | |
await InitVehicles(); | |
await InitWaypoints(); | |
editorLoading = false; | |
} | |
#region Tags | |
// ----------------------------------------------------------------------------------- InitTags | |
// --- InitTags ------------------------------------------------------------------------------- | |
private async Task InitTags() | |
{ | |
var tagsHandle = Addressables.LoadAssetsAsync<ScriptableObject>(idConfig.tagsLabel, null); | |
} | |
private async Task InitTagContainers() | |
{ | |
var tagContainerHandle = Addressables.LoadAssetsAsync<ScriptableObject>(idConfig.tagContainersLabel, null); | |
} | |
#endregion | |
#region Waypoints | |
// ------------------------------------------------------------------------------ InitWaypoints | |
// --- InitWaypoints -------------------------------------------------------------------------- | |
[TitleGroup("Addressables/Waypoints")] [SerializeField] | |
private List<GameObject> loadedWaypoints = new List<GameObject>(); | |
[TitleGroup("Addressables/Waypoints")] [SerializeField] | |
private List<GameObject> instantiatedWaypoints = new List<GameObject>(); | |
#if UNITY_EDITOR | |
[TitleGroup("Addressables/Waypoints")] [SerializeField] | |
private List<Task<GameObject>> wpGoList = new List<Task<GameObject>>(); | |
#endif | |
[TitleGroup("Addressables/Waypoints")] [SerializeField] | |
private List<AsyncOperationHandle<GameObject>> waypointsHandleList = new List<AsyncOperationHandle<GameObject>>(); | |
private Task InitWaypoints() | |
{ | |
var waypointHandle = Addressables.LoadAssetsAsync<GameObject>(idConfig.waypointPrefabLabel, null); | |
waypointHandle.Completed += OnWaypointsLoaded; | |
return waypointHandle.Task; | |
} | |
// ----------------------------------------------------------------- OnWaypointsLoaded | |
// --- OnWaypointsLoaded ------------------------------------------------------------- | |
private async void OnWaypointsLoaded(AsyncOperationHandle<IList<GameObject>> obj) | |
{ | |
GameObject[] waypoints; | |
loadedWaypoints = obj.Result.ToList(); | |
#if UNITY_EDITOR | |
if (!editorLoading) | |
{ | |
#else | |
var | |
#endif | |
wpGoList = new List<Task<GameObject>>(); | |
foreach (var waypoint in loadedWaypoints) | |
{ | |
wpGoList.Add(Addressables.InstantiateAsync(waypoint.name, transform).Task); | |
} | |
waypoints = await Task.WhenAll(wpGoList); | |
#if UNITY_EDITOR | |
} | |
else | |
{ | |
var goListTmp = new List<GameObject>(); | |
for (int i = 0; i < loadedWaypoints.Count; i++) | |
{ | |
goListTmp.Add(PrefabUtility.InstantiatePrefab(loadedWaypoints[i].gameObject) as GameObject); | |
} | |
waypoints = goListTmp.ToArray(); | |
} | |
#endif | |
for (int i = 0; i < waypoints.Length; i++) | |
{ | |
if (waypoints[i].GetComponent<Waypoint>().waypointClass == WaypointClass.Vehicles) waypoints[i].transform.parent = WaypointECSSystem.Instance.vehicleParent; | |
else if (waypoints[i].GetComponent<Waypoint>().waypointClass == WaypointClass.Pedestrians) waypoints[i].transform.parent = WaypointECSSystem.Instance.pedestrianParent; | |
instantiatedWaypoints.Add(waypoints[i]); | |
} | |
Debug.Log("Init Waypoints Complete => Running InitiateWaypointCreation()"); | |
#if UNITY_EDITOR | |
if (!Application.isPlaying || !EditorApplication.isPlaying) | |
{ | |
WaypointManager.Instance.UpdateCollections(); | |
WaypointDebug.Instance.GetContainer(); | |
} | |
#endif | |
if ( | |
#if UNITY_EDITOR | |
EditorApplication.isPlaying || | |
#endif | |
Application.isPlaying) WaypointECSSystem.Instance.waypointConverter.InitiateWaypointCreation(); | |
Debug.Log("InitiateWaypointCreation() Complete"); | |
} | |
#endregion | |
#region Vehicles | |
// ------------------------------------------------------------------------------- InitVehicles | |
// --- InitVehicles --------------------------------------------------------------------------- | |
[TitleGroup("Addressables/Vehicles")] [SerializeField, ShowInInspector] | |
public AsyncOperationHandle<IList<VehicleData>> vehicleHandle; | |
[TitleGroup("Addressables/Vehicles")] [SerializeField] | |
private List<VehicleData> loadedVehicles = new List<VehicleData>(); | |
[TitleGroup("Addressables/Vehicles")] [SerializeField] | |
private List<GameObject> instantiatedVehicles = new List<GameObject>(); | |
[TitleGroup("Addressables/Vehicles")] [SerializeField] | |
private List<Task<GameObject>> vehGoList = new List<Task<GameObject>>(); | |
[TitleGroup("Addressables/Vehicles")] [SerializeField] | |
private GameObject[] vehResult; | |
[TitleGroup("Addressables/Vehicles")] [SerializeField] | |
private List<AsyncOperationHandle<GameObject>> vehicleHandleList = new List<AsyncOperationHandle<GameObject>>(); | |
[TitleGroup("Addressables/Vehicles")] | |
[Button("Vehicles Load", ButtonSizes.Small)] | |
public Task InitVehicles() | |
{ | |
Debug.Log("Begin Loading VehicleData"); | |
vehicleHandle = Addressables.LoadAssetsAsync<VehicleData>(idConfig.vehicleDataLabel, null); | |
Debug.Log("Loading Vehicles Complete => Callback Start"); | |
vehicleHandle.Completed += OnVehiclesLoaded; | |
return vehicleHandle.Task; | |
} | |
// ------------------------------------------------------------------ OnVehiclesLoaded | |
// --- OnVehiclesLoaded -------------------------------------------------------------- | |
private async void OnVehiclesLoaded(AsyncOperationHandle<IList<VehicleData>> obj) | |
{ | |
loadedVehicles = obj.Result.ToList(); | |
Debug.Log("Loading Vehicle Reference Object"); | |
if (loadedVehicles != null) | |
for (int i = 0; i < loadedVehicles.Count; i++) | |
{ | |
vehGoList.Add(loadedVehicles[i].vehicleReference.LoadAssetAsync<GameObject>().Task); | |
} | |
vehResult = await Task.WhenAll(vehGoList); | |
Debug.Log("Vehicle Reference Objects Loaded => "); | |
instantiatedVehicles = vehResult.ToList(); | |
VehicleConverter.Instance.vehicleData = new List<VehicleData>(loadedVehicles); | |
} | |
#endregion | |
// ------------------------------------------------------------------------------------ Destroy | |
// --- Destroy -------------------------------------------------------------------------------- | |
[TitleGroup("Addressables/Destroy")] | |
[Button("Destroy Objects", ButtonSizes.Small)] | |
public void DestroyObjects() | |
{ | |
// ----------------------------------------------------------------- Vehicles | |
// --- Vehicles ------------------------------------------------------------- | |
if (loadedVehicles != null && loadedVehicles.Count > 0) | |
{ | |
Debug.Log("Releasing instantiatedVehicles"); | |
foreach (var items in loadedVehicles) | |
{ | |
Addressables.Release(items); | |
} | |
} | |
if (instantiatedVehicles != null && instantiatedVehicles.Count > 0) | |
{ | |
Debug.Log("Releasing instantiatedVehicles"); | |
foreach (var items in instantiatedVehicles) | |
{ | |
Addressables.Release(items); | |
} | |
} | |
if (vehGoList != null && vehGoList.Count > 0) | |
{ | |
Debug.Log("Releasing instantiatedVehicles"); | |
foreach (var items in vehGoList) | |
{ | |
Addressables.Release(items); | |
} | |
} | |
if (vehResult != null && vehResult.Length > 0) | |
{ | |
Debug.Log("Releasing instantiatedVehicles"); | |
foreach (var items in vehResult) | |
{ | |
Addressables.Release(items); | |
} | |
} | |
vehicleHandle = new AsyncOperationHandle<IList<VehicleData>>(); | |
loadedVehicles = new List<VehicleData>(); | |
instantiatedVehicles = new List<GameObject>(); | |
VehicleConverter.Instance.vehicleData = new List<VehicleData>(); | |
VehicleConverter.Instance.vehicleDatas = new Dictionary<Entity, VehicleData>(); | |
// ---------------------------------------------------------------- Waypoints | |
// --- Waypoints ------------------------------------------------------------ | |
if (instantiatedWaypoints != null && instantiatedWaypoints.Count > 0) | |
{ | |
Debug.Log("Releasing instantiatedWaypoints"); | |
foreach (var items in instantiatedWaypoints) | |
{ | |
Addressables.Release(items); | |
} | |
} | |
loadedWaypoints = new List<GameObject>(); | |
instantiatedWaypoints = new List<GameObject>(); | |
wpGoList = new List<Task<GameObject>>(); | |
WaypointManager.Instance.waypointList = new List<Waypoint>(); | |
WaypointManager.Instance.RemoveWaypoints(); | |
#if UNITY_EDITOR | |
WaypointDebug.Instance.waypoints = new List<Waypoint>(); | |
#endif | |
WaypointECSSystem.Instance.waypointHashSet = new HashSet<int>(); | |
if (Application.isPlaying) | |
WaypointECSSystem.Instance.waypointConverter.wpList = new List<Waypoint>(); | |
} | |
private void OnDestroy() | |
{ | |
DestroyObjects(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment