Example #1
0
    private void LoadObject(Type type, GameObject parent, Action <GameObject> onSuccess)
    {
        var objCache = CacheGet(type);

        if (objCache != null)
        {
            onSuccess.Execute(objCache);
        }
        else
        {
            LoadResource("UI/" + type, prefab =>
            {
                if (_objCache.ContainsKey(type))
                {
                    return; // Prevent duplicate object
                }
                GameObject obj;

                if (type.IsSubclassOf(typeof(Popup)))
                {
                    obj       = UIUtil.Clone(PopupHolder, parent);
                    var popup = UIUtil.Clone(prefab, obj).GetComponent <Popup>();
                    obj.GetComponent <PopupHolder>().SetPopup(popup);
                }
                else
                {
                    obj = UIUtil.Clone(prefab, parent);
                }

                obj.GetComponent <BaseUI>().OnCreate();
                if (!_objCache.ContainsKey(type))
                {
                    _objCache.Add(type, obj);
                }
                onSuccess.Execute(obj);
            });
        }
    }