Skip to content

Commit

Permalink
Add PreloadedAssetLoader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Haruma-K committed Jun 14, 2022
1 parent 8a6cddf commit 1b22017
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
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.
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1b22017

Please sign in to comment.