-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
Assets/UnityScreenNavigator/Runtime/Foundation/AssetLoader/PreloadedAssetLoader.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Object = UnityEngine.Object; | ||
|
||
namespace UnityScreenNavigator.Runtime.Foundation.AssetLoader | ||
{ | ||
/// <summary> | ||
/// <see cref="IAssetLoader" /> that allows you to register preloaded assets. | ||
/// </summary> | ||
public sealed class PreloadedAssetLoader : IAssetLoader | ||
{ | ||
private int _nextControlId; | ||
|
||
public Dictionary<string, Object> PreloadedObjects { get; } = new Dictionary<string, Object>(); | ||
|
||
public AssetLoadHandle<T> Load<T>(string key) where T : Object | ||
{ | ||
var controlId = _nextControlId++; | ||
|
||
var handle = new AssetLoadHandle<T>(controlId); | ||
var setter = (IAssetLoadHandleSetter<T>)handle; | ||
T result = null; | ||
if (PreloadedObjects.TryGetValue(key, out var obj)) | ||
result = obj as T; | ||
|
||
setter.SetResult(result); | ||
var status = result != null ? AssetLoadStatus.Success : AssetLoadStatus.Failed; | ||
setter.SetStatus(status); | ||
if (result == null) | ||
{ | ||
var exception = new InvalidOperationException($"Requested asset(Key: {key})was not found."); | ||
setter.SetOperationException(exception); | ||
} | ||
|
||
setter.SetPercentCompleteFunc(() => 1.0f); | ||
setter.SetTask(Task.FromResult(result)); | ||
return handle; | ||
} | ||
|
||
public AssetLoadHandle<T> LoadAsync<T>(string key) where T : Object | ||
{ | ||
return Load<T>(key); | ||
} | ||
|
||
public void Release(AssetLoadHandle handle) | ||
{ | ||
// This class does not release any objects. | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/UnityScreenNavigator/Runtime/Foundation/AssetLoader/PreloadedAssetLoader.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
Assets/UnityScreenNavigator/Runtime/Foundation/AssetLoader/PreloadedAssetLoaderObject.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using Object = UnityEngine.Object; | ||
|
||
namespace UnityScreenNavigator.Runtime.Foundation.AssetLoader | ||
{ | ||
[CreateAssetMenu(fileName = "PreloadedAssetLoader", menuName = "Resource Loader/Preloaded Asset Loader")] | ||
public sealed class PreloadedAssetLoaderObject : AssetLoaderObject, IAssetLoader | ||
{ | ||
[SerializeField] private List<KeyAssetPair> _preloadedObjects = new List<KeyAssetPair>(); | ||
|
||
private readonly PreloadedAssetLoader _loader = new PreloadedAssetLoader(); | ||
|
||
public List<KeyAssetPair> PreloadedObjects => _preloadedObjects; | ||
|
||
private void OnEnable() | ||
{ | ||
foreach (var preloadedObject in _preloadedObjects) | ||
_loader.PreloadedObjects.Add(preloadedObject.Key, preloadedObject.Asset); | ||
} | ||
|
||
public override AssetLoadHandle<T> Load<T>(string key) | ||
{ | ||
return _loader.Load<T>(key); | ||
} | ||
|
||
public override AssetLoadHandle<T> LoadAsync<T>(string key) | ||
{ | ||
return _loader.LoadAsync<T>(key); | ||
} | ||
|
||
public override void Release(AssetLoadHandle handle) | ||
{ | ||
_loader.Release(handle); | ||
} | ||
|
||
[Serializable] | ||
public class KeyAssetPair | ||
{ | ||
[SerializeField] private string _key; | ||
[SerializeField] private Object _asset; | ||
|
||
public string Key | ||
{ | ||
get => _key; | ||
set => _key = value; | ||
} | ||
|
||
public Object Asset | ||
{ | ||
get => _asset; | ||
set => _asset = value; | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ts/UnityScreenNavigator/Runtime/Foundation/AssetLoader/PreloadedAssetLoaderObject.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.