Exemple #1
0
    public List <AnimationItem> Create(AssetPackage[] assetPackages)
    {
        if (_snapScrolling != null)
        {
            for (int ii = 0; ii < assetPackages.Length; ii++)
            {
                AssetPackage assetPackage = assetPackages[ii];

                GameObject newPanel = _snapScrolling.CreateAPanel();

                GifferManager newPanelGifferManager = newPanel.GetComponent <GifferManager>();

                Sprite[] animationSprites = Array.ConvertAll(assetPackage.assets, sprites => sprites as Sprite);

                string animationName = assetPackage.GetAssetName();

                newPanelGifferManager.AddAnimationEntity(animationName, animationSprites);

                AnimationItem animationItem = newPanel.AddComponent <AnimationItem>();
                animationItem.Init(_targetAnim, animationName, repeatRate);
                animationItem.OnClicked += StopAllAnimationItems;
                animationItems.Add(animationItem);

                newPanelGifferManager.enabled = true;
            }
        }
        else
        {
            Debug.LogError("Snap Scrolling is null.");
        }
        return(animationItems);
    }
    public void sortAssetPackage(AssetPackage AssetPackages)
    {
        List <AssetPackage>            packageList = AssetPackages.packageList;
        SortedDictionary <int, string> tmpSortDict = new SortedDictionary <int, string>();

        foreach (AssetPackage assetPackage in packageList)
        {
            tmpSortDict.Add(assetPackage.nodeValue, assetPackage.name);
        }
        List <AssetPackage> newPackageList = new List <AssetPackage>();

        foreach (KeyValuePair <int, string> kv in tmpSortDict)
        {
            foreach (AssetPackage assetPackage in packageList)
            {
                if (assetPackage.name == kv.Value)
                {
                    newPackageList.Add(assetPackage);
                    break;
                }
                else
                {
                    continue;
                }
            }
        }
        AssetPackages.packageList = newPackageList;
        foreach (AssetPackage assetPackage in AssetPackages.packageList)
        {
            sortAssetPackage(assetPackage);
        }
    }
Exemple #3
0
        private void OnGUI()
        {
            EditorGUILayout.BeginVertical();

            for (int i = 0; i < configList.Count; i++)
            {
                AssetConfigInfo configInfo = configList[i];
                EditorGUILayout.ObjectField(configInfo.config.assetbundleName, configInfo.config, typeof(AssetConfig), true);
                if (Application.isPlaying && AssetManager.Instance != null)
                {
                    AssetPackage ap       = AssetManager.Instance.GetAssetBundle(configInfo.config.assetbundleName);
                    int          refCount = 0;
                    if (ap != null)
                    {
                        refCount = ap.refCount;
                    }

                    EditorGUILayout.LabelField("RefCount : " + refCount);

                    EditorGUILayout.Space();
                }
            }

            EditorGUILayout.EndVertical();
        }
    private void OnGUI()
    {
        if (m_ToExport.Count > 0)
        {
            EditorUtility.DisplayProgressBar("Exporting package...", "Exporting package", 0.5f);
        }
        else
        {
            EditorUtility.ClearProgressBar();
        }

        if (GUILayout.Button("Export All Package"))
        {
            ExportAll();
        }
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.BeginVertical(GUILayout.Width(128));
        if (GUILayout.Button("New..."))
        {
            string savePath = EditorUtility.SaveFilePanelInProject("Save package file", "package", "asset", "Save package");
            if (savePath != "")
            {
                AssetPackage package = CreateInstance <AssetPackage>();
                AssetDatabase.CreateAsset(package, savePath.Replace(Application.dataPath, "Assets"));
                m_CurrentlyEdited = package;
                ArrayUtility.Add(ref m_AssetPackageList, package);
                AssetDatabase.Refresh();
            }
        }

        m_PackageListScrollPos = EditorGUILayout.BeginScrollView(m_PackageListScrollPos, "box");

        for (int i = 0; i < m_AssetPackageList.Length; ++i)
        {
            if (m_AssetPackageList[i] != m_CurrentlyEdited)
            {
                if (GUILayout.Button(m_AssetPackageList[i].packageName))
                {
                    m_CurrentlyEdited = m_AssetPackageList[i];
                    PopulateTreeview();
                }
            }
            else
            {
                GUILayout.Label(m_AssetPackageList[i].packageName);
            }
        }
        EditorGUILayout.EndScrollView();
        EditorGUILayout.EndVertical();

        if (m_CurrentlyEdited != null)
        {
            EditedUI();
        }

        EditorGUILayout.EndHorizontal();
    }
    //updata package value
    public void nodeValueUpdate(AssetWeigth assetWeigth)
    {
        foreach (KeyValuePair <string, int> kv in assetWeigth.nodePackages)
        {
            if (_assetPackageDict.ContainsKey(kv.Key))
            {
                AssetPackage assetPackage = _assetPackageDict[kv.Key];
                assetPackage.nodeValue = kv.Value;
            }
        }

        sortAssetPackage(_assetPackage);
    }
    private IEnumerator AssetPackageDownLoad(AssetPackage assetPackage)
    {
        Debug.Log("AssetPackageDownLoad" + assetPackage.name);
        if (PlayerPrefs.HasKey(assetPackage.name))
        {
            int version = PlayerPrefs.GetInt(assetPackage.name);
            if (version == assetPackage.version)
            {
                Debug.Log("The Asset Package Has Being DownLoaded ...");
                yield break;
            }
        }

        List <string> assetList = assetPackage.assetList;

        /*
         * if(assetPackage.name == "")
         * {
         *
         * }
         */

        foreach (string asset in assetList)
        {
            Debug.Log("Ready To DownLoad One Asset ...... + Asset Name:" + asset);
            ResourceManager.Download(AssetPackageDownLoadCallBack, new string[] { asset });
            isDownLoaded = false;
            while (true)
            {
                if (isDownLoaded)
                {
                    break;
                }
                yield return(0);
            }
        }

        List <AssetPackage> assetPackageList = assetPackage.packageList;

        foreach (AssetPackage childAssetPackage in assetPackageList)
        {
            yield return(StartCoroutine(AssetPackageDownLoad(childAssetPackage)));
        }
        PlayerPrefs.SetInt(assetPackage.name, assetPackage.version);
    }
    void ExportAll()
    {
        string saveFolder = EditorUtility.SaveFolderPanel("Choose output Folder", Application.dataPath.Replace("/Assets", ""), "Package Ouput");

        if (saveFolder == "")
        {
            return;
        }

        AssetPackage current = m_CurrentlyEdited;

        for (int i = 0; i < m_AssetPackageList.Length; ++i)
        {
            m_CurrentlyEdited = m_AssetPackageList[i];
            ExportCurrentPackage(saveFolder + "/" + m_CurrentlyEdited.packageName + ".unitypackage");
        }
        m_CurrentlyEdited = current;
    }
    private AssetPackage LoadAssetPackage(SecurityElement element)
    {
        if (element.Tag != "Package")
        {
            Debug.Log("ERROR: Element Tag is Wrong");
            return(null);
        }

        AssetPackage assetPackage = new AssetPackage();

        int    id        = StrParser.ParseDecInt(element.Attribute("ID"), -1);
        string name      = StrParser.ParseStr(element.Attribute("Name"), "");
        int    hierarchy = StrParser.ParseDecInt(element.Attribute("Hierarchy"), -1);
        int    version   = StrParser.ParseDecInt(element.Attribute("Version"), -1);

        assetPackage.id        = id;
        assetPackage.name      = name;
        assetPackage.hierarchy = hierarchy;
        assetPackage.version   = version;

        foreach (SecurityElement childrenElement in element.Children)
        {
            if (childrenElement.Tag == "Asset")
            {
                string assetName = StrParser.ParseStr(childrenElement.Attribute("Name"), "");
                assetPackage.assetList.Add(assetName);
            }

            if (childrenElement.Tag == "Package")
            {
                AssetPackage childAssetPackage = this.LoadAssetPackage(childrenElement);
                assetPackage.packageList.Add(childAssetPackage);
            }
        }
        _assetPackageDict.Add(name, assetPackage);
        return(assetPackage);
    }
    /// <summary>
    /// 从StreamingAssets异步加载资源的携程体
    /// </summary>
    /// <param name="assetName">资源名</param>
    /// <param name="assetInternalFile">内部文件</param>
    /// <returns></returns>
    protected virtual IEnumerator LoadAssetFromStreamingAssets(string assetName, string assetInternalFile)
    {
        if (LogUtil.ShowDebug != null)
        {
            LogUtil.ShowDebug(string.Format("开始从内部资源加载资源:请求资源[{0}] 内部资源[{1}]", assetName, assetInternalFile));
        }
        string path = assetInternalFile;

        if (!assetInternalFile.Contains("://"))
        {
            path = Constants.urlFileHeader + assetInternalFile;
        }
        WWW www = new WWW(path);

        while (!www.isDone)
        {
            yield return(www.progress);
        }
        if (!string.IsNullOrEmpty(www.error))
        {
            if (LogUtil.ShowWarning != null)
            {
                LogUtil.ShowWarning(string.Format("读取内部资源出错[{0}]:\n{1}", Constants.urlFileHeader + assetInternalFile, www.error));
            }
            www.Dispose();
            www = null;
            // 资源加载失败后的处理
            ProcessFailedAsset(assetName);
            yield break;
        }
        AssetBundle assetbundle = www.assetBundle;

        www.Dispose();
        www = null;
        if (assetbundle == null)
        {
            if (LogUtil.ShowError != null)
            {
                LogUtil.ShowError(string.Format("从内部资源创建assetbundle出错[{0}]", assetInternalFile));
            }
            // 资源加载失败后的处理
            ProcessFailedAsset(assetName);
            yield break;
        }

        object res = null;

        if (AssetType != typeof(AssetBundle))
        {
            AssetPackage package = new AssetPackage();
            package.reletiveAssets = assetbundle.LoadAllAssets();
            if (package.reletiveAssets == null || package.reletiveAssets.Length < 1)
            {
                if (LogUtil.ShowError != null)
                {
                    LogUtil.ShowError(string.Format("在内部资源[{0}]中找不到资源", assetInternalFile));
                }
                // 资源加载失败后的处理
                ProcessFailedAsset(assetName);
                yield break;
            }
#if UNITY_EDITOR
            // 检查所有格式被设置为ARGB32的贴图(移动平台不支持ARGB32格式,必须都转成RGBA32)
            for (int i = 0; i < package.reletiveAssets.Length; ++i)
            {
                UnityObject uo = package.reletiveAssets[i];
                if (uo is Texture2D)
                {
                    if (((Texture2D)uo).format == TextureFormat.ARGB32)
                    {
                        Debug.LogWarning(string.Format("贴图{0}格式是ARGB32(5),{1}", uo, assetName));
                    }
                }
            }
#endif
            if (AssetType != null)
            {
                package.mainAsset = assetbundle.LoadAsset(assetName, AssetType);
            }
            //package.rawAssetbundle = assetbundle;
            yield return(0.99f);

            assetbundle.Unload(false);
            assetbundle = null;
            //if (AssetType != null && package.mainAsset == null)
            //{
            //	Debug.LogError(string.Format("在内部资源[{0}]中找不到指定资源[{1}]", assetInternalFile, assetName));
            //	// 资源加载失败后的处理
            //	ProcessFailedAsset(assetName);
            //	yield break;
            //}
            res = package;
        }
        else
        {
            res = assetbundle;
        }

        yield return(Constants.oneFloat);

        if (LogUtil.ShowDebug != null)
        {
            LogUtil.ShowDebug(string.Format("从内部资源加载资源完成:加载资源[{0}]", assetName));
        }
        // 资源加载成功后的处理
        ProcessLoadedAsset(assetName, res);
    }
    /// <summary>
    /// 从Resources异步加载资源的携程体
    /// </summary>
    /// <param name="assetName">资源名</param>
    /// <param name="assetResourcePath">内部资源路径</param>
    /// <returns></returns>
    protected virtual IEnumerator LoadAssetFromResource(string assetName, string assetResourcePath)
    {
        if (LogUtil.ShowDebug != null)
        {
            LogUtil.ShowDebug(string.Format("开始从内部资源加载资源:请求资源[{0}] 内部资源[{1}]", assetName, assetResourcePath));
        }
        float           weight1  = 0.5f;
        ResourceRequest request1 = Resources.LoadAsync(assetResourcePath, typeof(TextAsset));

        while (!request1.isDone)
        {
            yield return(request1.progress * weight1);
        }
        TextAsset ta = (TextAsset)request1.asset;

        request1 = null;
        if (ta == null)
        {
            if (LogUtil.ShowWarning != null)
            {
                LogUtil.ShowWarning(string.Format("找不到内部资源[{0}]", assetResourcePath));
            }
            // 资源加载失败后的处理
            ProcessFailedAsset(assetName);
            yield break;
        }

        float weight2 = 0.5f;
        AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(ta.bytes);

        while (!request2.isDone)
        {
            yield return(weight1 + request2.progress * weight2);
        }
        AssetBundle assetbundle = request2.assetBundle;

        request2 = null;
        Resources.UnloadAsset(ta);
        ta = null;
        if (assetbundle == null)
        {
            if (LogUtil.ShowError != null)
            {
                LogUtil.ShowError(string.Format("从内部资源创建assetbundle出错[{0}]", assetResourcePath));
            }
            // 资源加载失败后的处理
            ProcessFailedAsset(assetName);
            yield break;
        }

        object res = null;

        if (AssetType != typeof(AssetBundle))
        {
            AssetPackage package = new AssetPackage();
            package.reletiveAssets = assetbundle.LoadAllAssets();
            if (package.reletiveAssets == null || package.reletiveAssets.Length < 1)
            {
                if (LogUtil.ShowError != null)
                {
                    LogUtil.ShowError(string.Format("在内部资源[{0}]中找不到资源", assetResourcePath));
                }
                // 资源加载失败后的处理
                ProcessFailedAsset(assetName);
                yield break;
            }
            if (AssetType != null)
            {
                package.mainAsset = assetbundle.LoadAsset(assetName, AssetType);
            }
            //package.rawAssetbundle = assetbundle;
            yield return(0.99f);

            assetbundle.Unload(false);
            assetbundle = null;
            //if (AssetType != null && package.mainAsset == null)
            //{
            //	Debug.LogError(string.Format("在内部资源[{0}]中找不到指定资源[{1}]", assetResourcePath, assetName));
            //	// 资源加载失败后的处理
            //	ProcessFailedAsset(assetName);
            //	yield break;
            //}
            res = package;
        }
        else
        {
            res = assetbundle;
        }

        yield return(Constants.oneFloat);

        if (LogUtil.ShowDebug != null)
        {
            LogUtil.ShowDebug(string.Format("从内部资源加载资源完成:加载资源[{0}]", assetName));
        }
        // 资源加载成功后的处理
        ProcessLoadedAsset(assetName, res);
    }
    /// <summary>
    /// 从外部文件异步加载资源的携程体
    /// </summary>
    /// <param name="assetName">资源名</param>
    /// <param name="assetExternalFile">外部文件</param>
    /// <returns></returns>
    protected virtual IEnumerator LoadAssetFromExternalFile(string assetName, string assetExternalFile)
    {
        if (LogUtil.ShowDebug != null)
        {
            LogUtil.ShowDebug(string.Format("开始从外部文件加载资源:请求资源[{0}] 外部文件[{1}]", assetName, assetExternalFile));
        }
        WWW www = new WWW(Constants.urlFileHeader + assetExternalFile);

        while (!www.isDone)
        {
            yield return(www.progress);
        }
        if (!string.IsNullOrEmpty(www.error))
        {
            if (LogUtil.ShowWarning != null)
            {
                LogUtil.ShowWarning(string.Format("读取外部文件出错[{0}]:\n{1}", Constants.urlFileHeader + assetExternalFile, www.error));
            }
            www.Dispose();
            www = null;
            // 资源加载失败后的处理
            ProcessFailedAsset(assetName);
            yield break;
        }
        AssetBundle assetbundle = www.assetBundle;

        www.Dispose();
        www = null;
        if (assetbundle == null)
        {
            if (LogUtil.ShowError != null)
            {
                LogUtil.ShowError(string.Format("从外部文件创建assetbundle出错[{0}]", assetExternalFile));
            }
            // 资源加载失败后的处理
            ProcessFailedAsset(assetName);
            yield break;
        }

        object res = null;

        if (AssetType != typeof(AssetBundle))
        {
            AssetPackage package = new AssetPackage();
            package.reletiveAssets = assetbundle.LoadAllAssets();
            if (package.reletiveAssets == null || package.reletiveAssets.Length < 1)
            {
                if (LogUtil.ShowError != null)
                {
                    LogUtil.ShowError(string.Format("在外部文件[{0}]中找不到资源", assetExternalFile));
                }
                // 资源加载失败后的处理
                ProcessFailedAsset(assetName);
                yield break;
            }
            if (AssetType != null)
            {
                package.mainAsset = assetbundle.LoadAsset(assetName, AssetType);
            }
            //package.rawAssetbundle = assetbundle;
            yield return(0.99f);

            assetbundle.Unload(false);
            assetbundle = null;
            //if (AssetType != null && package.mainAsset == null)
            //{
            //	Debug.LogError(string.Format("在外部文件[{0}]中找不到指定资源[{1}]", assetExternalFile, assetName));
            //	// 资源加载失败后的处理
            //	ProcessFailedAsset(assetName);
            //	yield break;
            //}
            res = package;
        }
        else
        {
            res = assetbundle;
        }

        yield return(Constants.oneFloat);

        if (LogUtil.ShowDebug != null)
        {
            LogUtil.ShowDebug(string.Format("从外部文件加载资源完成:加载资源[{0}]", assetName));
        }
        // 资源加载成功后的处理
        ProcessLoadedAsset(assetName, res);
    }
 /// <summary>
 /// 从内存里删除这个资源
 /// </summary>
 /// <param name="resourceName">资源名</param>
 /// <param name="resourceObject">资源实例</param>
 protected virtual void UnloadResourceObject(string resourceName, object resourceObject)
 {
     if (AssetType != typeof(AssetBundle))
     {
         AssetPackage asset = (AssetPackage)resourceObject;
         if (asset != null)
         {
             if (asset.mainAsset != null)
             {
                 if (asset.mainAsset is Component || asset.mainAsset is GameObject)
                 {
                     UnityObject.DestroyImmediate(asset.mainAsset, true);
                 }
                 else
                 {
                     Resources.UnloadAsset(asset.mainAsset);
                 }
             }
             if (asset.reletiveAssets != null)
             {
                 for (int i = 0; i < asset.reletiveAssets.Length; ++i)
                 {
                     UnityObject reletiveAsset = asset.reletiveAssets[i];
                     if (reletiveAsset == null)
                     {
                         continue;
                     }
                     if (reletiveAsset is Component || reletiveAsset is GameObject)
                     {
                         UnityObject.DestroyImmediate(reletiveAsset, true);
                     }
                     else if (reletiveAsset is Shader)
                     {
                         if (!ResourceManager.Instance.IsCommonResource(reletiveAsset.name))
                         {
                             Resources.UnloadAsset(reletiveAsset);
                         }
                     }
                     else
                     {
                         Resources.UnloadAsset(reletiveAsset);
                     }
                 }
             }
             //asset.rawAssetbundle.Unload(true);
             asset = null;
             // 卸载不再使用的资源,推迟到场景卸载
             //Resources.UnloadUnusedAssets();
             if (LogUtil.ShowInfo != null)
             {
                 LogUtil.ShowInfo(string.Format("释放资源[{0}]", resourceName));
             }
         }
         else if (LogUtil.ShowError != null)
         {
             LogUtil.ShowError("这个资源不是AssetPackage类型的!");
         }
     }
     else
     {
         AssetBundle asset = (AssetBundle)resourceObject;
         if (asset != null)
         {
             // 场景比较特殊,场景的资源释放并不是不再使用这部分资源了,而是把多余的资源释放掉,而已经加载进内存的实例是需要保留的
             // 场景资源的完全释放由Unity场景切换(卸载)时自动完成
             asset.Unload(false);
             asset = null;
             // 卸载不再使用的资源,推迟到场景卸载
             //Resources.UnloadUnusedAssets();
             if (LogUtil.ShowInfo != null)
             {
                 LogUtil.ShowInfo(string.Format("释放资源[{0}]", resourceName));
             }
         }
         else if (LogUtil.ShowError != null)
         {
             LogUtil.ShowError("这个资源不是AssetBundle类型的!");
         }
     }
 }