示例#1
0
        public LoadHandle Load(
            string identifier,
            Type type,
            OnError onError,
            OnComplete onComplete       = null,
            GameObject holderGameObject = null)
        {
            string fileName;
            string assetName;

            _database.ParseIdentifier(out fileName, out assetName, identifier);

            var assetDictionaryKey = identifier + "_" + type.Name;

            AssetHandle assetHandle = null;

            _assetHandles.TryGetValue(assetDictionaryKey, out assetHandle);

            // みつからなかったので生成
            if (assetHandle == null)
            {
                FileHandle fileHandle = null;
                if (!_fileHandles.TryGetValue(fileName, out fileHandle))
                {
                    fileHandle = MakeFileHandle(fileName, onError);
                }
                assetHandle = new AssetHandle(fileHandle, assetName, assetDictionaryKey, type, onError);
                lock (_watchingAssetHandles)
                {
                    _watchingAssetHandles.Add(assetHandle);                     // 生成直後は監視が必要
                }
                _assetHandles.Add(assetDictionaryKey, assetHandle);
            }

            if (onComplete != null)
            {
                if (assetHandle.done)
                {
                    onComplete(assetHandle.asset);
                }
                else
                {
                    assetHandle.AddCallback(onComplete);
                }
            }
            assetHandle.IncrementReferenceThreadSafe();

            var handle = new LoadHandle(assetHandle, this);

            if (holderGameObject != null)
            {
                var holder = holderGameObject.GetComponent <LoadHandleHolder>();
                if (holder == null)
                {
                    holder = holderGameObject.AddComponent <LoadHandleHolder>();
                }
                holder.Add(handle);
            }
            return(handle);
        }
示例#2
0
 public void Add(LoadHandle handle)
 {
     if (_handles == null)
     {
         _handles = new List <LoadHandle>();
     }
     _handles.Add(handle);
 }
示例#3
0
        public LoadHandle Load(
            string identifier,
            OnComplete onComplete       = null,
            GameObject holderGameObject = null)
        {
            string assetBundleName, assetName;

            _database.ParseIdentifier(out assetBundleName, out assetName, identifier);

            var abHandleDictionaryKey    = assetBundleName;
            var assetHandleDictionaryKey = identifier;

            AssetHandle assetHandle = null;

            lock (_lock)
            {
                // まず完了済みから探す
                if (!_completeHandles.TryGetValue(assetHandleDictionaryKey, out assetHandle))
                {
                    // ロード中から探す
                    _loadingHandles.TryGetValue(assetHandleDictionaryKey, out assetHandle);
                }
            }

            // みつからなかったので生成
            if (assetHandle == null)
            {
                // ないのでハンドル生成が確定
                AssetBundleHandle abHandle = null;
                if (!_abHandles.TryGetValue(abHandleDictionaryKey, out abHandle))
                {
                    Hash128 hash;
                    uint    crc;
                    _database.GetAssetBundleMetaData(out hash, out crc, assetBundleName);

                    var cachedAssetBundle = new CachedAssetBundle();
                    cachedAssetBundle.hash = hash;
                    cachedAssetBundle.name = assetBundleName;
                    var path = _downloadRoot + assetBundleName;
                    abHandle = new AssetBundleHandle(
                        assetBundleName,
                        ref cachedAssetBundle,
                        path);
                    _abHandles.Add(abHandleDictionaryKey, abHandle);
                }
                assetHandle = new AssetHandle(abHandle, assetName, assetHandleDictionaryKey);
                _loadingHandles.Add(assetHandleDictionaryKey, assetHandle);
            }

            if (onComplete != null)
            {
                if (assetHandle.isDone)
                {
                    onComplete(assetHandle.asset);
                }
                else
                {
                    assetHandle.AddCallback(onComplete);
                }
            }
            assetHandle.IncrementReferenceThreadSafe();

            var handle = new LoadHandle(assetHandleDictionaryKey, this);

            if (holderGameObject != null)
            {
                var holder = holderGameObject.GetComponent <LoadHandleHolder>();
                if (holder == null)
                {
                    holder = holderGameObject.AddComponent <LoadHandleHolder>();
                }
                holder.Add(handle);
            }
            return(handle);
        }
示例#4
0
        public LoadHandle Load(
            string identifier,
            Type type,
            OnError onError,
            OnComplete onComplete       = null,
            GameObject holderGameObject = null)
        {
//Debug.Log("Loader.Load: " + identifier + " " + type.Name);
            if (!IsStarted())
            {
                if (onError != null)
                {
                    onError(Error.Other, identifier, new System.Exception("Loader.Start() hasn't called."));
                }
                Debug.Assert(this.ready, "Call Start().");
                return(null);
            }

            // 容量制限に引っかかってロードできない!!
            if (_memoryUsingBytes > this.loadLimit)
            {
                if (onError != null)
                {
                    onError(Error.ExceedMemoryLimit, identifier, new System.Exception("memory limit exceed. LOAD ABORTED. limit=" + this.loadLimit));
                }
                return(null);
            }

            var assetDictionaryKey = identifier + "_" + type.Name;

            AssetHandle assetHandle = null;

            _assetHandles.TryGetValue(assetDictionaryKey, out assetHandle);

            // みつからなかったので生成
            if (assetHandle == null)
            {
                string fileName;
                string assetName;
                if (!_database.ParseIdentifier(out fileName, out assetName, identifier))
                {
                    if (onError != null)
                    {
                        onError(Error.Other, identifier, new System.Exception("IAssetFileDatabase.ParseIdentifier() returned false."));
                    }
                    return(null);
                }
                Debug.Assert(fileName != null);
                Debug.Assert(assetName != null);
                Debug.Assert(fileName[0] != '/', "fileName must not starts with slash.");                 // スラッシュ始まりは許容しない

                var fileHandle = GetOrMakeFileHandle(fileName, onError);
                assetHandle = new AssetHandle(fileHandle, assetName, assetDictionaryKey, type, onError);
                lock (_watchingAssetHandles)
                {
                    _watchingAssetHandles.Add(assetHandle);                     // 生成直後は監視が必要
                }
                _assetHandles.Add(assetDictionaryKey, assetHandle);
            }
            else if (assetHandle.memoryCachedListNode != null)             // 参照カウント0だけどキャッシュされてる奴がみつかった
            {
                // キャッシュから外す
                Debug.Assert(assetHandle.disposable);                 // 破棄可能なはず
                _memoryCachedAssetHandles.Remove(assetHandle.memoryCachedListNode);
                assetHandle.SetMemoryCachedListNode(null);            // 切断
            }

            if (onComplete != null)
            {
                if (assetHandle.done)
                {
                    onComplete(assetHandle.asset);
                }
                else
                {
                    assetHandle.AddCallback(onComplete);
                }
            }
            assetHandle.IncrementReferenceThreadSafe();

            var handle = new LoadHandle(assetHandle, this);

            if (holderGameObject != null)
            {
                var holder = holderGameObject.GetComponent <LoadHandleHolder>();
                if (holder == null)
                {
                    holder = holderGameObject.AddComponent <LoadHandleHolder>();
                }
                holder.Add(handle);
            }
            return(handle);
        }