コード例 #1
0
    private void _LoadPrefabToSceneFunc(string prefab_name, string child_path, string scene_name, object param = null)
    {
        var cur_scene = SceneManager.GetSceneByName(scene_name);

        SceneManager.SetActiveScene(cur_scene);
        Transform cur_parent = null;

        if (cur_scene != null)
        {
            var go_array = cur_scene.GetRootGameObjects();
            cur_parent = go_array[0].transform;
        }
        GameObject go = null, new_go = null;

        //当在缓冲池则自动拷贝
        if (InstantiateCache.I.CanSpawn(RESOURCE_CATEGORY.Game, prefab_name))
        {
            new_go = InstantiateCache.I.Spawn(RESOURCE_CATEGORY.Game, prefab_name, cur_parent).gameObject;
        }
        else
        {
            var path = InstantiateCache.GetAssetPath(RESOURCE_CATEGORY.Game, prefab_name, child_path);
            go          = Resources.Load(path) as GameObject;
            new_go      = MonoBehaviour.Instantiate(go, cur_parent);
            new_go.name = prefab_name;
            //不存在缓冲池则创建缓冲池
            if (!InstantiateCache.I.hasThisPool(RESOURCE_CATEGORY.Game))
            {
                InstantiateCache.AddPool(RESOURCE_CATEGORY.Game, prefab_name, new_go);
            }
            if (!InstantiateCache.I.HasThisPrefabPool(RESOURCE_CATEGORY.Game, prefab_name))
            {
                InstantiateCache.I.AddPrefabPool(RESOURCE_CATEGORY.Game, new_go);
            }
        }
        _poolGo.Add(new_go);
        new_go.SetActive(true);
        new_go.GetComponent <AllObjectBase> ()?.OnShow(param);
        _loadedCount -= 1;
        if (_loadedCount < 1)
        {
            _loadedCallBack?.Invoke();
        }
    }
コード例 #2
0
    private IEnumerator _LoadAsync(string prefabName, Transform parent, Action <GameObject> onLoad = null, object param = null)
    {
        if (parent == null)
        {
            yield break;
        }

        if (InstantiateCache.I.CanSpawn(RESOURCE_CATEGORY.UI, prefabName))
        {
            try
            {
                GameObject go   = null;
                var        inst = InstantiateCache.I.Spawn(RESOURCE_CATEGORY.UI, prefabName, parent);
                if (inst != null)
                {
                    go = inst.gameObject;
                }
                onLoad?.Invoke(go);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError(e);
            }
            yield break;
        }
        _prefabLoadingList.Add(prefabName);
        bool         isCompleted = false;
        AssetRequest req         = InstantiateCache.LoadAssets(RESOURCE_CATEGORY.UI, prefabName, false, (r) => { isCompleted = true; });

        if (!string.IsNullOrEmpty(req.assetPath))
        {
            var go    = Resources.Load(req.assetPath) as GameObject;
            var trans = parent ?? tran_Inactive;
            go = MonoBehaviour.Instantiate(go, trans);
            var view = go.GetComponent <ViewBase>();
            go.name = prefabName;
            view.OnLoad();
            trans.gameObject.SetActive(true);
            _OnViewLoadFinish(prefabName, view);
            yield return(_OnViewShow(view, param));
        }
        while (!isCompleted)
        {
            yield return(null);
        }

        if (parent != null)
        {
            if (req.loadedObj != null && req.loadedObj is GameObject)
            {
                var prefabObj = req.loadedObj as GameObject;

                var tran = InstantiateCache.I.Spawn(RESOURCE_CATEGORY.UI, prefabName, parent);
                if (tran != null)
                {
                    prefabObj = tran.gameObject;
                }
                else
                {
                    prefabObj = UnityEngine.Object.Instantiate(prefabObj, parent);
                }
                if (prefabObj == null)
                {
                    //UnityEngine.Debug.LogError("PrefabObj is error!");
                }
                else
                {
                    prefabObj.name = prefabName;

                    try
                    {
                        onLoad?.Invoke(prefabObj);
                    }catch (Exception e)
                    {
                        UnityEngine.Debug.LogError(e);
                    }
                }
            }
        }
        _prefabLoadingList.Remove(prefabName);
    }