hch
5 小时以前 cba555476b1f1b856b9211451e739d8cdce0eeba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//--------------------------------------------------------
//    [Author]:           玩个游戏
//    [  Date ]:           Tuesday, March 19, 2019
//--------------------------------------------------------
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();
    }
    public void Reload()
    {
        instance = null;
        this.transform.ClearAllChilds();
        Create();
    }
 
    [ContextMenu("Create")]
    public virtual void Create()
    {
        if (instance != null)
            return;
 
        if (string.IsNullOrEmpty(prefabName))
            return;
 
        var find = this.transform.Find(prefabName);
        if (find)
        {
            instance = find.gameObject;
            return;
        }
        instance = UIUtility.CreateWidget(prefabName, prefabName);
        instance.transform.SetParentEx(this.transform, Vector3.zero, Quaternion.identity, Vector3.one);
    }
 
}