Exemplo n.º 1
0
        private static Object LoadAsset(string assetPath, Type type)
        {
            if (string.IsNullOrEmpty(assetPath))
            {
                return(null);
            }

            var extensions = ResourceExtensions.GetExtOfType(type);

            foreach (var ext in extensions)
            {
                var    assetPathWithExt = assetPath + ext;
                Object asset            = null;
#if UNITY_EDITOR
                asset = AssetDatabase.LoadAssetAtPath(ASSET_ROOT_PATH + assetPathWithExt, type);
#endif
                if (asset == null)
                {
                    continue;
                }

                return(asset);
            }
            Debug.LogFormat("ResourceManager loading file {0} failed, type {1}", assetPath, type);
            return(null);
        }
Exemplo n.º 2
0
        private static Object LoadAsset(string assetPath, Type type, bool shouldCacheAsset = false)
        {
            if (string.IsNullOrEmpty(assetPath))
            {
                return(null);
            }

            var extensions = ResourceExtensions.GetExtOfType(type);

            foreach (var ext in extensions)
            {
                // 先找缓存
                var          assetPathWithExt = assetPath + ext;
                ResourcePool resourcePool;
                if (ResourcePoolManager._resourcePoolDict.TryGetValue(assetPathWithExt, out resourcePool))
                {
                    (resourcePool.Resource as GameObject).SetActive(true);
                    return(resourcePool.Resource);
                }

                var    hasCached = false;
                Object asset     = null;
                if (!AppEnv.IsUseAB)
                {
#if UNITY_EDITOR
                    asset = AssetManager.LoadFromAssetDatabase(assetPathWithExt, type);
#endif
                }
                else
                {
                    asset = BundleManager.LoadFromAssetBundle(assetPathWithExt, type);
                }

                if (asset == null)
                {
                    continue;
                }

                if (shouldCacheAsset && !hasCached)
                {
                    resourcePool = new ResourcePool(asset, ResourcePoolManager._poolParent);
                    ResourcePoolManager._resourcePoolDict.Add(assetPathWithExt, resourcePool);
                }

                return(asset);
            }

            LogHelper.INFO("ResourceManager", "loading file {0} failed, type {1}", assetPath, type);
            return(null);
        }
Exemplo n.º 3
0
        private static IEnumerator LoadAssetAsync(string assetPath, Type type, Action <int> _CachedDoneCallback, bool shouldCacheAsset = false)
        {
            if (string.IsNullOrEmpty(assetPath))
            {
                yield break;
            }

            var extensions = ResourceExtensions.GetExtOfType(type);

            foreach (var ext in extensions)
            {
                var          assetPathWithExt = assetPath + ext;
                Object       asset            = null;
                ResourcePool resourcePool;
                if (ResourcePoolManager._resourcePoolDict.TryGetValue(assetPathWithExt, out resourcePool))
                {
                    asset = resourcePool.Resource as GameObject;
                    (asset as GameObject).SetActive(true);
                    var obj = InstantiateObject(asset) as GameObject;
                    resourcePool = new ResourcePool(obj, ResourcePoolManager._poolParent);
                    ResourcePoolManager._resourcePoolDict.Add(assetPath, resourcePool);
                    _CachedDoneCallback?.Invoke(ResourcePoolManager._resourcePoolDict.Count);
                    yield break;
                }

                var hasCached = false;
                if (!AppEnv.IsUseAB)
                {
#if UNITY_EDITOR
                    _Instance.StartCoroutine(AssetManager.LoadFromAssetDatabaseAsync(assetPathWithExt, type, resourcePool, shouldCacheAsset, hasCached, _CachedDoneCallback));
#endif
                }
                else
                {
                    _Instance.StartCoroutine(BundleManager.LoadFromAssetBundleAsync(assetPathWithExt, type, resourcePool, shouldCacheAsset, hasCached, _CachedDoneCallback));
                }
            }
        }