137 lines
3.8 KiB
C#
137 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using AlicizaX.Runtime;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace AlicizaX.Resource.Runtime
|
|
{
|
|
[Serializable]
|
|
public struct AssetsRefInfo
|
|
{
|
|
public int instanceId;
|
|
|
|
public Object refAsset;
|
|
|
|
public AssetsRefInfo(Object refAsset)
|
|
{
|
|
this.refAsset = refAsset;
|
|
instanceId = this.refAsset.GetInstanceID();
|
|
}
|
|
}
|
|
|
|
public sealed class AssetsReference : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameObject _sourceGameObject;
|
|
|
|
[SerializeField] private List<AssetsRefInfo> _refAssetInfoList;
|
|
|
|
private ResourceComponent _resourceComponent;
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_resourceComponent == null)
|
|
{
|
|
_resourceComponent = GameEntry.GetComponent<ResourceComponent>();
|
|
}
|
|
|
|
if (_resourceComponent == null)
|
|
{
|
|
throw new GameFrameworkException($"ResourceManager is null.");
|
|
}
|
|
|
|
if (_sourceGameObject != null)
|
|
{
|
|
_resourceComponent.UnloadAsset(_sourceGameObject);
|
|
}
|
|
|
|
ReleaseRefAssetInfoList();
|
|
}
|
|
|
|
private void ReleaseRefAssetInfoList()
|
|
{
|
|
if (_refAssetInfoList != null)
|
|
{
|
|
foreach (var refInfo in _refAssetInfoList)
|
|
{
|
|
_resourceComponent.UnloadAsset(refInfo.refAsset);
|
|
}
|
|
|
|
_refAssetInfoList.Clear();
|
|
}
|
|
}
|
|
|
|
public AssetsReference Ref(GameObject source)
|
|
{
|
|
if (source == null)
|
|
{
|
|
throw new GameFrameworkException($"Source gameObject is null.");
|
|
}
|
|
|
|
if (source.scene.name != null)
|
|
{
|
|
throw new GameFrameworkException($"Source gameObject is in scene.");
|
|
}
|
|
|
|
_sourceGameObject = source;
|
|
return this;
|
|
}
|
|
|
|
public AssetsReference Ref<T>(T source) where T : UnityEngine.Object
|
|
{
|
|
if (source == null)
|
|
{
|
|
throw new GameFrameworkException($"Source gameObject is null.");
|
|
}
|
|
|
|
if (_refAssetInfoList == null)
|
|
{
|
|
_refAssetInfoList = new List<AssetsRefInfo>();
|
|
}
|
|
|
|
_refAssetInfoList.Add(new AssetsRefInfo(source));
|
|
return this;
|
|
}
|
|
|
|
public static AssetsReference Instantiate(GameObject source, Transform parent = null)
|
|
{
|
|
if (source == null)
|
|
{
|
|
throw new GameFrameworkException($"Source gameObject is null.");
|
|
}
|
|
|
|
if (source.scene.name != null)
|
|
{
|
|
throw new GameFrameworkException($"Source gameObject is in scene.");
|
|
}
|
|
|
|
GameObject instance = Object.Instantiate(source, parent);
|
|
return instance.AddComponent<AssetsReference>().Ref(source);
|
|
}
|
|
|
|
public static AssetsReference Ref(GameObject source, GameObject instance)
|
|
{
|
|
if (source == null)
|
|
{
|
|
throw new GameFrameworkException($"Source gameObject is null.");
|
|
}
|
|
|
|
if (source.scene.name != null)
|
|
{
|
|
throw new GameFrameworkException($"Source gameObject is in scene.");
|
|
}
|
|
|
|
return instance.GetOrAddComponent<AssetsReference>().Ref(source);
|
|
}
|
|
|
|
public static AssetsReference Ref<T>(T source, GameObject instance) where T : UnityEngine.Object
|
|
{
|
|
if (source == null)
|
|
{
|
|
throw new GameFrameworkException($"Source gameObject is null.");
|
|
}
|
|
|
|
return instance.GetOrAddComponent<AssetsReference>().Ref(source);
|
|
}
|
|
}
|
|
} |