Second commit
This commit is contained in:
commit
36840f3ad4
999 files changed
+61364
No files matched your search
@@ -0,0 +1,159 @@
|
||||
//
|
||||
// This file is part of the Tremble package by Tiny Goose.
|
||||
// Copyright (c) 2024-2026 TinyGoose Ltd., All Rights Reserved.
|
||||
//
|
||||
|
||||
#if ADDRESSABLES_INSTALLED
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
using UnityEngine.ResourceManagement.ResourceLocations;
|
||||
using Debug = UnityEngine.Debug;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace TinyGoose.Tremble
|
||||
{
|
||||
public class AddressablesLoaderAdaptor : IAssetLoaderAdaptor
|
||||
{
|
||||
public bool SupportsPreloading => true;
|
||||
|
||||
private readonly List<AsyncOperationHandle> m_OwnedHandles = new(1024);
|
||||
private readonly Dictionary<string, object> m_LoadedAssets = new(1024);
|
||||
|
||||
public string[] FindAssetPaths(Type type, string name = "")
|
||||
{
|
||||
return TrembleAssetLoader.FindAssetDataMany(a => a.Path.ContainsInvariant(name))
|
||||
.Where(a => a.FullTypeName.EqualsInvariant(type.FullName))
|
||||
.Select(a => a.Path)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public TAsset LoadAssetByPath<TAsset>(string path) where TAsset : Object
|
||||
{
|
||||
if (path.IsNullOrEmpty())
|
||||
return null;
|
||||
|
||||
if (!TrembleAssetLoader.TryGetAssetDataFromPath(path, out AssetMetadata assetData))
|
||||
{
|
||||
Debug.LogError($"Not loading invalid addressable: {path}");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (m_LoadedAssets.TryGetValue(assetData.AddressableName, out object existingAsset) &&
|
||||
existingAsset is TAsset tAsset)
|
||||
{
|
||||
return tAsset;
|
||||
}
|
||||
|
||||
AsyncOperationHandle<TAsset> handle = Addressables.LoadAssetAsync<TAsset>(assetData.AddressableName);
|
||||
TAsset asset = handle.WaitForCompletion();
|
||||
if (!asset)
|
||||
{
|
||||
Addressables.Release(handle);
|
||||
return null;
|
||||
}
|
||||
|
||||
m_OwnedHandles.Add(handle);
|
||||
m_LoadedAssets[path] = handle;
|
||||
return asset;
|
||||
}
|
||||
|
||||
public async Task PreloadAssetsByPaths(IList<string> paths)
|
||||
{
|
||||
if (paths.Count == 0)
|
||||
return;
|
||||
|
||||
List<string> addresses = new(paths.Count);
|
||||
|
||||
// First, remove dupes, invalid and ones we already loaded
|
||||
for (int i = 0; i < paths.Count; i++)
|
||||
{
|
||||
string path = paths[i];
|
||||
|
||||
if (path.IsNullOrEmpty() || !TrembleAssetLoader.TryGetAssetDataFromPath(path, out AssetMetadata assetData))
|
||||
{
|
||||
Debug.LogError($"Not loading invalid addressable: {path}");
|
||||
paths.RemoveAt(i--);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (m_LoadedAssets.ContainsKey(assetData.AddressableName))
|
||||
{
|
||||
paths.RemoveAt(i--);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (paths.IndexOf(path) != i)
|
||||
{
|
||||
paths.RemoveAt(i--);
|
||||
continue;
|
||||
}
|
||||
|
||||
// This is valid - add to list of addresses
|
||||
addresses.Add(assetData.AddressableName);
|
||||
}
|
||||
|
||||
if (addresses.Count == 0)
|
||||
return;
|
||||
|
||||
AsyncOperationHandle<IList<Object>> resultsHandle = Addressables.LoadAssetsAsync<Object>(addresses, null, Addressables.MergeMode.Union);
|
||||
await resultsHandle.Task;
|
||||
|
||||
for (int i = 0; i < paths.Count; i++)
|
||||
{
|
||||
string path = paths[i];
|
||||
if (TrembleAssetLoader.TryGetAssetDataFromPath(path, out AssetMetadata assetData))
|
||||
{
|
||||
m_LoadedAssets[assetData.AddressableName] = resultsHandle.Result[i];
|
||||
}
|
||||
}
|
||||
m_OwnedHandles.Add(resultsHandle);
|
||||
}
|
||||
|
||||
public string GetPath(Object obj)
|
||||
{
|
||||
Debug.LogError("GetPath is not implemented in Addressables (yet!)");
|
||||
return null;
|
||||
}
|
||||
|
||||
public TrembleSyncSettings LoadSyncSettings()
|
||||
{
|
||||
return Addressables
|
||||
.LoadAssetAsync<TrembleSyncSettings>(TrembleSyncSettings.ADDRESSABLE_KEY)
|
||||
.WaitForCompletion();
|
||||
}
|
||||
|
||||
public Material LoadClipMaterial()
|
||||
{
|
||||
return Addressables.LoadAssetAsync<Material>("M_NullRender").WaitForCompletion();
|
||||
}
|
||||
|
||||
public void ReleaseAll()
|
||||
{
|
||||
foreach (AsyncOperationHandle handle in m_OwnedHandles)
|
||||
{
|
||||
Addressables.Release(handle);
|
||||
}
|
||||
m_OwnedHandles.Clear();
|
||||
}
|
||||
|
||||
private static bool IsAddressablePathValid(string path)
|
||||
{
|
||||
if (path == null)
|
||||
return false;
|
||||
|
||||
AsyncOperationHandle<IList<IResourceLocation>> locationLoadOperation = Addressables.LoadResourceLocationsAsync(path);
|
||||
IList<IResourceLocation> results = locationLoadOperation.WaitForCompletion();
|
||||
bool isValidPath = results is { Count: > 0 };
|
||||
Addressables.Release(locationLoadOperation);
|
||||
|
||||
return isValidPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4959867e61442c2aa78844ff6cd8e06
|
||||
timeCreated: 1716201342
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 277805
|
||||
packageName: Tremble - Map Importer
|
||||
packageVersion: 1.11.1
|
||||
assetPath: Assets/Tremble/AssetLoading/Adaptors/AddressablesLoaderAdaptor.cs
|
||||
uploadId: 939664
|
||||
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// This file is part of the Tremble package by Tiny Goose.
|
||||
// Copyright (c) 2024-2026 TinyGoose Ltd., All Rights Reserved.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
namespace TinyGoose.Tremble
|
||||
{
|
||||
public class EditorLoaderAdaptor : IAssetLoaderAdaptor
|
||||
{
|
||||
public bool SupportsPreloading => false;
|
||||
|
||||
private static readonly Dictionary<string, string[]> s_CachedQueryResults = new(1024);
|
||||
|
||||
public string[] FindAssetPaths(Type type, string name = "")
|
||||
{
|
||||
string typeString = type == typeof(GameObject) ? "prefab" : type.Name;
|
||||
string fullQuery = $"t:{typeString} {name}";
|
||||
|
||||
if (!s_CachedQueryResults.TryGetValue(fullQuery, out string[] results))
|
||||
{
|
||||
results = AssetDatabase
|
||||
.FindAssets(fullQuery, new[] { "Assets" })
|
||||
.Select(AssetDatabase.GUIDToAssetPath)
|
||||
.ToArray();
|
||||
|
||||
if (results.Length > 0)
|
||||
{
|
||||
s_CachedQueryResults[fullQuery] = results;
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public TAsset LoadAssetByPath<TAsset>(string path) where TAsset : Object
|
||||
=> AssetDatabase.LoadAssetAtPath<TAsset>(path);
|
||||
|
||||
public Task PreloadAssetsByPaths(IList<string> paths)
|
||||
{
|
||||
// We don't need to preload in Editor ;)
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task PreloadAssetsByPathsAsync(IList<string> paths)
|
||||
{
|
||||
// We don't need to preload in Editor ;)
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
public string GetPath(Object obj) => AssetDatabase.GetAssetPath(obj);
|
||||
|
||||
|
||||
public TrembleSyncSettings GetSyncSettings() => TrembleSyncSettings.Get();
|
||||
|
||||
public Material LoadClipMaterial()
|
||||
{
|
||||
string clipPath = Path.Combine(TrembleConsts.EDITOR_GetTrembleInstallFolder(), "Materials", "M_NullRender.mat");
|
||||
return LoadAssetByPath<Material>(clipPath);
|
||||
}
|
||||
|
||||
public TrembleSyncSettings LoadSyncSettings()
|
||||
{
|
||||
string[] paths = FindAssetPaths(typeof(TrembleSyncSettings));
|
||||
|
||||
switch (paths.Length)
|
||||
{
|
||||
case 0:
|
||||
return null;
|
||||
|
||||
case >1:
|
||||
Debug.LogError("More than one Tremble Sync Settings file found!");
|
||||
foreach (string path in paths)
|
||||
{
|
||||
Debug.LogWarning($"Found at: {path}", AssetDatabase.LoadAssetAtPath<TrembleSyncSettings>(path));
|
||||
}
|
||||
Debug.LogError("There should be only one! Picking one arbitrarily for now.");
|
||||
|
||||
goto default;
|
||||
default:
|
||||
return AssetDatabase.LoadAssetAtPath<TrembleSyncSettings>(paths[0]);
|
||||
}
|
||||
}
|
||||
|
||||
public void ReleaseAll() { }
|
||||
|
||||
|
||||
public static void DropCachedAssetPaths()
|
||||
{
|
||||
s_CachedQueryResults.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a410384015ae43959c21d5c273d941a0
|
||||
timeCreated: 1716201389
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 277805
|
||||
packageName: Tremble - Map Importer
|
||||
packageVersion: 1.11.1
|
||||
assetPath: Assets/Tremble/AssetLoading/Adaptors/EditorLoaderAdaptor.cs
|
||||
uploadId: 939664
|
||||
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// This file is part of the Tremble package by Tiny Goose.
|
||||
// Copyright (c) 2024-2026 TinyGoose Ltd., All Rights Reserved.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace TinyGoose.Tremble
|
||||
{
|
||||
public interface IAssetLoaderAdaptor
|
||||
{
|
||||
// -----------------------------------------------------------------------------------------------------------------------------
|
||||
// Finding & loading assets
|
||||
// -----------------------------------------------------------------------------------------------------------------------------
|
||||
string[] FindAssetPaths(Type type, string name = "");
|
||||
TAsset LoadAssetByPath<TAsset>(string path) where TAsset : Object;
|
||||
Task PreloadAssetsByPaths(IList<string> paths);
|
||||
bool SupportsPreloading { get; }
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------------
|
||||
// Getting path for a loaded asset
|
||||
// -----------------------------------------------------------------------------------------------------------------------------
|
||||
string GetPath(Object obj);
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------------------
|
||||
// Misc
|
||||
// -----------------------------------------------------------------------------------------------------------------------------
|
||||
TrembleSyncSettings LoadSyncSettings();
|
||||
Material LoadClipMaterial();
|
||||
void ReleaseAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7271c3afa0854533a18a87ea7a9690c8
|
||||
timeCreated: 1716201055
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 277805
|
||||
packageName: Tremble - Map Importer
|
||||
packageVersion: 1.11.1
|
||||
assetPath: Assets/Tremble/AssetLoading/Adaptors/IAssetLoaderAdaptor.cs
|
||||
uploadId: 939664
|
||||
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// This file is part of the Tremble package by Tiny Goose.
|
||||
// Copyright (c) 2024-2026 TinyGoose Ltd., All Rights Reserved.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace TinyGoose.Tremble
|
||||
{
|
||||
public class NullLoaderAdaptor : IAssetLoaderAdaptor
|
||||
{
|
||||
public bool SupportsPreloading => false;
|
||||
|
||||
public string[] FindAssetPaths(Type type, string name = "")
|
||||
{
|
||||
Debug.LogError("No valid loader adaptor - ensure you have Addressables installed or are in editor!");
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
|
||||
public TAsset LoadAssetByPath<TAsset>(string path) where TAsset : Object
|
||||
{
|
||||
Debug.LogError("No valid loader adaptor - ensure you have Addressables installed or are in editor!");
|
||||
return null;
|
||||
}
|
||||
|
||||
public Task PreloadAssetsByPaths(IList<string> paths)
|
||||
{
|
||||
Debug.LogError("No valid loader adaptor - ensure you have Addressables installed or are in editor!");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public string GetPath(Object obj)
|
||||
{
|
||||
Debug.LogError("No valid loader adaptor - ensure you have Addressables installed or are in editor!");
|
||||
return null;
|
||||
}
|
||||
|
||||
public TrembleSyncSettings GetSyncSettings()
|
||||
{
|
||||
Debug.LogError("No valid loader adaptor - ensure you have Addressables installed or are in editor!");
|
||||
return null;
|
||||
}
|
||||
|
||||
public TrembleSyncSettings LoadSyncSettings()
|
||||
{
|
||||
Debug.LogError("No valid loader adaptor - ensure you have Addressables installed or are in editor!");
|
||||
return null;
|
||||
}
|
||||
|
||||
public Material LoadClipMaterial()
|
||||
{
|
||||
Debug.LogError("No valid loader adaptor - ensure you have Addressables installed or are in editor!");
|
||||
return null;
|
||||
}
|
||||
public void ReleaseAll() { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3c2e961a9234fcb94549e1f5bb3e8f2
|
||||
timeCreated: 1716201662
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 277805
|
||||
packageName: Tremble - Map Importer
|
||||
packageVersion: 1.11.1
|
||||
assetPath: Assets/Tremble/AssetLoading/Adaptors/NullLoaderAdaptor.cs
|
||||
uploadId: 939664
|
||||
Reference in new issue
Block a user