コード例 #1
0
        public long AsyncInstantiateObject(string path, OnAsyncFinish outerCallBack, LoadResPriority priority,
                                           bool isSetSceneTrans = false, bool isClear = true, params object[] paramList)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(0);
            }
            uint       crc        = CRC32.GetCRC32(path);
            ObjectItem objectItem = GetCacheObjectItemFromPoolDic(crc);

            if (objectItem != null)
            {
                if (isSetSceneTrans)
                {
                    objectItem.CloneObj.transform.SetParent(SceneTrans, false);
                }
                outerCallBack?.Invoke(path, objectItem.CloneObj, paramList);
                return(objectItem.Guid);
            }

            objectItem = objectItemNativePool.Spawn();
            if (objectItem == null)
            {
                Debug.LogError("null");
            }

            long guid = ResourceManager.Instance.CreateGuid();

            objectItem.Crc = crc;
            objectItem.IsSetSceneParent = isSetSceneTrans;
            objectItem.isClear          = isClear;
            objectItem.outerCallBack    = outerCallBack;
            objectItem.paramList        = paramList;
            //调用ResourceManager异步加载接口

            ResourceManager.Instance.AsyncLoadResource(path, objectItem, (_path, item, plist) =>
            {
                if (item == null)
                {
                    return;
                }
                if (item.PrimitiveAssetItem.AssetObject == null)
                {
#if UNITY_EDITOR
                    Debug.LogError("异步资源加载为空" + _path);
#endif
                }
                else
                {
                    //实例化
                    item.CloneObj          = Object.Instantiate(item.PrimitiveAssetItem.AssetObject) as GameObject;
                    objectItem.OfflineData = item.CloneObj.GetComponent <OfflineData.OfflineData>();
                }

//加载完成移除
                if (asyncLoadingObjectDic.ContainsKey(item.Guid))
                {
                    asyncLoadingObjectDic.Remove(item.Guid);
                }
                if (item.CloneObj != null && item.IsSetSceneParent)
                {
                    item.CloneObj.transform.SetParent(SceneTrans);
                }

                if (item.outerCallBack != null)
                {
                    if (item.CloneObj != null)
                    {
                        int _guid = item.CloneObj.GetInstanceID();
                        if (!ObjectItemsInstanceTempDic.ContainsKey(_guid))
                        {
                            ObjectItemsInstanceTempDic.Add(_guid, item);
                        }
                    }

                    item.outerCallBack?.Invoke(_path, item.CloneObj, plist);
                }
            }, priority);
            return(guid);
        }