//--------------------------------------------------------
|
// [Author]: 玩个游戏
|
// [ Date ]: Tuesday, March 19, 2019
|
//--------------------------------------------------------
|
using Cysharp.Threading.Tasks;
|
using UnityEngine;
|
|
[ExecuteAlways]
|
[RequireComponent(typeof(RectTransform))]
|
public class UIPrefabLoader : MonoBehaviour
|
{
|
[SerializeField] protected string m_PrefabName;
|
public virtual string prefabName { get { return m_PrefabName; } }
|
|
protected GameObject instance;
|
|
[ExecuteAlways]
|
private void Awake()
|
{
|
Create().Forget();
|
}
|
public void Reload()
|
{
|
instance = null;
|
this.transform.ClearAllChilds();
|
Create().Forget();
|
}
|
|
[ContextMenu("Create")]
|
public virtual async UniTask Create()
|
{
|
if (instance != null)
|
return;
|
|
if (string.IsNullOrEmpty(prefabName))
|
return;
|
|
var find = this.transform.Find(prefabName);
|
if (find)
|
{
|
instance = find.gameObject;
|
return;
|
}
|
var inst = await UIUtility.CreateWidget(prefabName, prefabName);
|
if (this == null || inst == null)
|
{
|
if (null != inst) DestroyImmediate(inst);
|
return;
|
}
|
if (instance != null)
|
{
|
DestroyImmediate(inst);
|
return;
|
}
|
instance = inst;
|
instance.transform.SetParentEx(this.transform, Vector3.zero, Quaternion.identity, Vector3.one);
|
}
|
|
}
|