//参数3:资源在跳转场景是否需要清空 public GameObject Instantiate(string path, bool setSceneObj = false, bool bClear = true) { uint crc = CrcHelper.StringToCRC32(path); //先尝试从缓存中取实例化Obj GameObjectItem gameObjectItem = GetGameObjectItemFromPool(crc); if (gameObjectItem == null) { gameObjectItem = m_gameObjectItemClassPool.Spawn(true); gameObjectItem.Crc = crc; gameObjectItem.Clear = bClear; ResourceMgr.Instance.LoadGameObjectItem(path, gameObjectItem); if (gameObjectItem.ResourceItem.Obj != null) { gameObjectItem.Obj = GameObject.Instantiate(gameObjectItem.ResourceItem.Obj) as GameObject; gameObjectItem.OfflineData = gameObjectItem.Obj.GetComponent <OfflineData>(); } } if (setSceneObj) { gameObjectItem.Obj.transform.SetParent(m_sceneGos, false); } gameObjectItem.Guid = gameObjectItem.Obj.GetInstanceID(); if (!m_gameObjectItemDict.ContainsKey(gameObjectItem.Guid)) { m_gameObjectItemDict.Add(gameObjectItem.Guid, gameObjectItem); } return(gameObjectItem.Obj); }
//加载指定ab包名的ab包 public AssetBundle LoadAssetBundle(string abName) { AssetBundleItem abItem = null; uint crc = CrcHelper.StringToCRC32(abName); //AB包名字创建crc if (!m_assetBundleItemDict.TryGetValue(crc, out abItem) || abItem == null) { AssetBundle assetBundle = null; string abFullPath = ABPathConfig.AssetBundleBuildTargetPath + "/" + abName; if (File.Exists(abFullPath)) { assetBundle = AssetBundle.LoadFromFile(abFullPath); } if (assetBundle == null) { Debug.Log("Load AssetBundle Error: " + abFullPath); return(null); } abItem = m_assetBundleItemPool.Spawn(true); abItem.assetBundle = assetBundle; abItem.refCount++; m_assetBundleItemDict.Add(crc, abItem); } else { abItem.refCount++; } return(abItem.assetBundle); }
//异步加载资源, 仅仅是不需要实例化的资源,音频,图片等 public void AsyncLoadAsset <T>(string path, OnAsyncResourceObjFinished dealFinished, LoadAssetPriority priority, object param1 = null, object param2 = null, object param3 = null, uint crc = 0) where T : UnityEngine.Object { if (crc == 0) { crc = CrcHelper.StringToCRC32(path); } ResourceItem item = GetAssetFromAssetCache(crc); if (item != null) { if (dealFinished != null) { dealFinished(path, item.Obj, param1, param2, param3); } return; } //判断是否在加载中 AsyncLoadAssetParam para = null; if (!m_loadingAssetDict.TryGetValue(crc, out para) || para == null) { para = m_asyncLoadAssetParamPool.Spawn(true); para.m_crc = crc; para.m_path = path; para.m_priority = priority; //这里应该还少了对Sprite的判定 m_loadingAssetDict.Add(crc, para); m_loadingAssetList[(int)priority].Add(para); } AsyncCallBack callback = m_asyncCallBackPool.Spawn(true); callback.m_resourceObjDealFinished = dealFinished; callback.m_param1 = param1; callback.m_param2 = param2; callback.m_param3 = param3; para.m_callbackList.Add(callback); }
//同步加载GameObject, 参数3:资源在跳转场景是否需要清空 public GameObject Instantiate(string path, bool bClear = true) { uint crc = CrcHelper.StringToCRC32(path); //先尝试从缓存中取实例化Obj GameObjectItem gameObjectItem = GetGameObjectItemFromPool(crc); if (gameObjectItem == null) { gameObjectItem = m_gameObjectItemClassPool.Spawn(true); gameObjectItem.Crc = crc; gameObjectItem.Clear = bClear; ResourceMgr.Instance.LoadGameObjectItem(path, gameObjectItem); if (gameObjectItem.ResourceItem.Obj != null) { gameObjectItem.Obj = GameObject.Instantiate(gameObjectItem.ResourceItem.Obj) as GameObject; } } gameObjectItem.Guid = gameObjectItem.Obj.GetInstanceID(); if (!m_gameObjectItemDict.ContainsKey(gameObjectItem.Guid)) { m_gameObjectItemDict.Add(gameObjectItem.Guid, gameObjectItem); } return(gameObjectItem.Obj); }