Beispiel #1
0
    public object Load(string file, AssetType at)
    {
        AssetDesc des = new AssetDesc(file, at);

        if (cacheDict.ContainsKey(des.RelativePath))
        {
            object obj = cacheDict[des.RelativePath];
            return(obj);
        }
#if DEBUG_MODE
        object temp = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Res/" + file, GameUtil.GetAssetType(at));
        cacheDict.Add(des.RelativePath, temp);
        return(temp);
#else
        if (!loadingListSync.Contains(des.RelativePath))
        {
            loadingListSync.Add(des.RelativePath);
        }
        object temp = null;
        //加载所有依赖
        string[]      dependencies = Manifest.GetAllDependencies(des.AssetBundleTag);
        AssetBundle[] array        = new AssetBundle[dependencies.Length + 1];
        int           index        = 0;
        foreach (string str in dependencies)
        {
            string      fullPath = Config.AssetPath + str.Substring(4);
            AssetBundle ab       = AssetBundle.LoadFromFile(fullPath);
            array[index] = ab;
            index++;
        }
        //加载目标AssetBundle
        AssetBundle aa = AssetBundle.LoadFromFile(des.FullPath);
        array[index] = aa;
        //从目标AssetBundle中加载资源
        temp = aa.LoadAsset(des.AssetName, GameUtil.GetAssetType(des.AssetType));
        cacheDict.Add(des.RelativePath, temp);
        foreach (AssetBundle t in array)
        {
            t.Unload(false);
        }
        if (loadingListSync.Contains(des.RelativePath))
        {
            loadingListSync.Remove(des.RelativePath);
        }
        if (callbackDict.ContainsKey(file))
        {
            foreach (var act in callbackDict[file])
            {
                act(temp);
            }
            callbackDict[file] = new List <Action <object> >();
        }
        return(temp);
#endif
    }
Beispiel #2
0
        public override System.Drawing.Size GetExpectedResourceSize()
        {
            if (ResProtocol.IsSingleTexture(Res))
            {
                AssetDesc desc = Scene.Instance.GetAssetDesc(Res);
                if (desc == null)
                {
                    return(Const.ZERO_SIZE);
                }

                return(AssetUtil.GetImageSize(desc.Path));
            }
            else
            {
                return(ResourceManager.Instance.GetResourceSize(Res));
            }
        }
Beispiel #3
0
    public void LoadAsync(string file, AssetType at, Action <object> callback, Action <double> progress)
    {
        AssetDesc des = new AssetDesc(file, at);

        if (!File.Exists(des.ProjectPath))
        {
            Debug.LogError(des.ProjectPath + " is not exists!");
            return;
        }
        des.OnProgress = progress;
        if (cacheDict.ContainsKey(file))
        {
            object obj = cacheDict[file];
            callback(obj);
            return;
        }
#if DEBUG_MODE
        object temp = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Res/" + file, GameUtil.GetAssetType(at));
        cacheDict.Add(file, temp);
        callback(temp);
#else
        if (!loadingDict.ContainsKey(file))
        {
            loadingDict.Add(file, des);
            totalIndex++;
            foreach (string str in des.Dependencies)
            {
                if (!abTemp.ContainsKey(str))
                {
                    string fullPath = Config.AssetPath + "res/" + str.Substring(4);
                    AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(fullPath);
                    abTemp.Add(str, new BundleDes(request, fullPath));
                }
                abTemp[str].AddRefrenceCount();
                totalIndex++;
            }
        }
        if (!callbackDict.ContainsKey(file))
        {
            callbackDict.Add(file, new System.Collections.Generic.List <Action <object> >());
        }
        callbackDict[file].Add(callback);
#endif
    }
Beispiel #4
0
        public static TextureRenderInfo BuildSingleTexture(Gwen.Renderer.Tao renderer, string url)
        {
            if (!ResProtocol.IsSingleTexture(url))
            {
                return(null);
            }

            AssetDesc desc = Scene.Instance.GetAssetDesc(url);

            if (desc == null)
            {
                return(null);
            }

            string fullpath = Path.Combine(GState.AssetRoot, SysUtil.ToWindowsPath(desc.Path));

            if (!File.Exists(fullpath))
            {
                return(null);
            }

            Gwen.Texture t;
            t = new Gwen.Texture(renderer);
            if (t == null)
            {
                return(null);
            }

            t.Load(fullpath);
            if (t.Failed)
            {
                return(null);
            }

            TextureRenderInfo tri = new TextureRenderInfo();

            tri.u1      = 0;
            tri.v1      = 0;
            tri.u2      = 1;
            tri.v2      = 1;
            tri.texture = t;
            return(tri);
        }
Beispiel #5
0
        private void m_btApply_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(m_assetBrowser.SelectedAsset))
            {
                Logging.Instance.Message("请先选中一个资源。");
                return;
            }

            AssetDesc desc = AssetUtil.CreateDesc(m_assetBrowser.SelectedAsset);

            if (desc == null)
            {
                Logging.Instance.Message("无法引用该资源,细节请查看 Log。");
                return;
            }

            string name = Scene.Instance.Root.Assets.AppendIfNotExist(desc);

            UpdateSingleTextureURL(name);

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
Beispiel #6
0
    private void Update()
    {
        //print(Progress + "    " + curIndex + "/" + totalIndex);
        //print(curIndex + "/" + totalIndex);

        //while (callbackToRemoveQue.Count > 0) callbackDict.Remove(callbackToRemoveQue.Dequeue());

        //if (!ready) return;
        //if (loadingDict.Count > 0)
        //{
        //    StartCoroutine(CoLoad());
        //}

        //if (Input.GetKeyDown(KeyCode.Q))
        //{
        //    Debug.Log("---------------abTemp-----------------");
        //    if (abTemp.Count != 0) foreach (var k in abTemp.Keys) Debug.Log(k);
        //    else Debug.Log("abTemp's count is 0");
        //    Debug.Log("----------------abTemp----------------");
        //    Debug.Log("----------------Cached----------------");
        //    if (cacheDict.Count != 0) foreach (var k in cacheDict.Keys) Debug.Log(k);
        //    else Debug.Log("abTemp's count is 0");
        //    Debug.Log("----------------Cached----------------");
        //}

        //if (Input.GetKey(KeyCode.W)) print(Progress * 100 + "%" + "\t" + curIndex + "/" + totalIndex);

        if (loadingDict.Count <= 0)
        {
            return;
        }

#if PerFile
        foreach (var p in loadingDict)
        {
            if (mainAssetLoadingDict.ContainsKey(p.Key))
            {
                continue;
            }
            foreach (var tag in p.Value.Dependencies)
            {
                if (p.Value.LoadedDependencies.Contains(tag))
                {
                    continue;
                }
                if (abTemp.ContainsKey(tag))
                {
                    if (abTemp[tag].assetBundleReq.isDone)
                    {
                        p.Value.FinishNum++;
                        p.Value.LoadedDependencies.Add(tag);
                        curIndex++;
                    }
                }
                else
                {
                    Debug.LogError("dependence assets must preload!" + " assets: \n" + p.Value.RelativePath + "\ndependecies: \n" + tag);
                }
            }
            if (abTemp.ContainsKey(p.Key) && abTemp[p.Key].assetBundleReq.isDone)
            {
                if (!mainAssetLoadingDict.ContainsKey(p.Key))
                {
                    p.Value.FinishNum++;
                    var temp = abTemp[p.Key].assetBundleReq.assetBundle.LoadAssetAsync(p.Value.AssetName, GameUtil.GetAssetType(p.Value.AssetType));
                    mainAssetLoadingDict.Add(p.Key, temp);
                    abTemp[p.Key].AddRefrenceCount();
                    curIndex++;
                }
            }
            else
            {
                if (p.Value.FinishNum >= p.Value.Dependencies.Length && !abTemp.ContainsKey(p.Key))
                {
                    var req = AssetBundle.LoadFromFileAsync(p.Value.FullPath);
                    abTemp.Add(p.Key, new BundleDes(req, p.Value.FullPath));
                }
            }
            p.Value.Progress = (float)p.Value.FinishNum / (p.Value.Dependencies.Length + 1);
        }

        foreach (var p in mainAssetLoadingDict)
        {
            if (p.Value.isDone)
            {
                if (!cacheDict.ContainsKey(p.Key))
                {
                    cacheDict.Add(p.Key, p.Value.asset);
                }
                if (callbackDict.ContainsKey(p.Key))
                {
                    foreach (var cb in callbackDict[p.Key])
                    {
                        cb(p.Value.asset);
                    }
                    callbackDict[p.Key] = new List <Action <object> >();
                }
                else
                {
                    Debug.LogError(p.Key + " -> callback is none!");
                }
                abTemp[p.Key].ReduceRefrenceCount();
                if (abTemp[p.Key].refrenceCount <= 0)
                {
                    abUnloadQue.Enqueue(p.Key);
                }
                foreach (var key in loadingDict[p.Key].Dependencies)
                {
                    abTemp[key].ReduceRefrenceCount();
                    if (abTemp[key].refrenceCount <= 0)
                    {
                        abUnloadQue.Enqueue(key);
                    }
                }
            }
        }

        while (abUnloadQue.Count > 0)
        {
            var str = abUnloadQue.Dequeue();
            if (abTemp.ContainsKey(str))
            {
                abTemp[str].assetBundleReq.assetBundle.Unload(false);
                abTemp.Remove(str);
                //Debug.Log("remove from abTemp -> " + str);
            }
            if (loadingDict.ContainsKey(str))
            {
                loadingDict.Remove(str);
                //Debug.Log("remove from loadingDict -> " + str);
            }
            if (mainAssetLoadingDict.ContainsKey(str))
            {
                mainAssetLoadingDict.Remove(str);
                //Debug.Log("remove from mainAssetLoadingDict -> " + str);
            }
        }
#else
        foreach (var p in loadingDict)
        {
            if (mainAssetLoadingDict.ContainsKey(p.Key))
            {
                continue;
            }
            foreach (var tag in p.Value.Dependencies)
            {
                if (p.Value.LoadedDependencies.Contains(tag))
                {
                    continue;
                }
                if (abTemp.ContainsKey(tag))
                {
                    if (abTemp[tag].assetBundleReq.isDone)
                    {
                        p.Value.FinishNum++;
                        p.Value.LoadedDependencies.Add(tag);
                        curIndex++;
                    }
                }
                else
                {
                    Debug.LogError("dependence assets must preload!" + " assets: \n" + p.Value.RelativePath + "\ndependecies: \n" + tag);
                }
            }
            if (abTemp.ContainsKey(p.Value.AssetBundleTag) && abTemp[p.Value.AssetBundleTag].assetBundleReq.isDone)
            {
                if (!mainAssetLoadingDict.ContainsKey(p.Key))
                {
                    p.Value.FinishNum++;
                    var temp = abTemp[p.Value.AssetBundleTag].assetBundleReq.assetBundle.LoadAssetAsync(p.Value.AssetName, GameUtil.GetAssetType(p.Value.AssetType));
                    mainAssetLoadingDict.Add(p.Key, temp);
                    abTemp[p.Value.AssetBundleTag].AddRefrenceCount();
                    curIndex++;
                }
            }
            else
            {
                if (p.Value.FinishNum >= p.Value.Dependencies.Length && !abTemp.ContainsKey(p.Value.AssetBundleTag))
                {
                    var req = AssetBundle.LoadFromFileAsync(p.Value.FullPath);
                    abTemp.Add(p.Value.AssetBundleTag, new BundleDes(req, p.Value.FullPath));
                }
            }
            p.Value.Progress = (float)p.Value.FinishNum / (p.Value.Dependencies.Length + 1);
        }

        foreach (var p in mainAssetLoadingDict)
        {
            if (p.Value.isDone)
            {
                if (!cacheDict.ContainsKey(p.Key))
                {
                    cacheDict.Add(p.Key, p.Value.asset);
                }
                if (callbackDict.ContainsKey(p.Key))
                {
                    foreach (var cb in callbackDict[p.Key])
                    {
                        cb(p.Value.asset);
                    }
                    callbackDict[p.Key] = new List <Action <object> >();
                }
                else
                {
                    Debug.LogError(p.Key + " -> callback is none!");
                }
                abUnloadQue.Enqueue(p.Key);
                string tag = AssetDesc.GetAssetBundleTag(p.Key);
                abTemp[tag].ReduceRefrenceCount();
                if (abTemp[tag].refrenceCount <= 0)
                {
                    abUnloadQue.Enqueue(tag);
                }
                foreach (var key in loadingDict[p.Key].Dependencies)
                {
                    abTemp[key].ReduceRefrenceCount();
                    if (abTemp[key].refrenceCount <= 0)
                    {
                        abUnloadQue.Enqueue(key);
                    }
                }
            }
        }

        while (abUnloadQue.Count > 0)
        {
            var str = abUnloadQue.Dequeue();
            if (abTemp.ContainsKey(str))
            {
                abTemp[str].assetBundleReq.assetBundle.Unload(false);
                abTemp.Remove(str);
                //Debug.Log("remove from abTemp -> " + str);
            }
            if (loadingDict.ContainsKey(str))
            {
                loadingDict.Remove(str);
                //Debug.Log("remove from loadingDict -> " + str);
            }
            if (mainAssetLoadingDict.ContainsKey(str))
            {
                mainAssetLoadingDict.Remove(str);
                //Debug.Log("remove from mainAssetLoadingDict -> " + str);
            }
        }
#endif
    }