Beispiel #1
0
    /// <summary>
    /// 资源所在层级
    /// </summary>
    /// <param name="filePath">资源路径</param>
    /// <returns></returns>
    public static int getAssetLevel(string filePath)
    {
        //取得资源所有引用,包括脚本cs
        string[]      depencys = AssetDatabase.GetDependencies(new string[] { filePath });
        List <string> deps     = new List <string>();

        foreach (string file in depencys)
        {
            //排除关联脚本
            string suffix = BuildCommon.getFileSuffix(file); //后缀名
            if (suffix == "dll")                             //也就说只要是脚本就舍弃,不加到引用资源里面
            {
                continue;
            }
            deps.Add(file);
        }
        if (deps.Count == 1)
        {
            return(1);
        }
        int maxLevel = 0;

        foreach (string file in deps)
        {
            if (file == filePath)
            {
                continue;
            }
            int level = getAssetLevel(file);
            maxLevel = maxLevel > level + 1 ? maxLevel : level + 1;
        }
        return(maxLevel);
    }
Beispiel #2
0
    public List <AssetUnit> mDirectUpperDependences; //直接上层依赖,用于判定assetbundle加载后是否可以马上删除
    #endregion
    #region 构造方法
    public AssetUnit(string path)
    {
        mPath   = path;
        mName   = BuildCommon.getFileName(mPath, true); //获取文件名带后缀
        mSuffix = BuildCommon.getFileSuffix(mName);     //获取文件的后缀名
        switch (mSuffix)
        {
        case "shader":
            mType = EnumAssetType.eAssetType_AssetBundleShader;
            break;

        case "prefab":
            mType = EnumAssetType.eAssetType_AssetBundlePrefab;
            break;

        case "tex":
            break;
        }
        mLevel                 = BuildCommon.getAssetLevel(mPath);
        mAllDependencies       = new List <string>();
        mNextLevelDependencies = new List <AssetUnit>();
        //获取这个资源的所有引用
        string[] deps = AssetDatabase.GetDependencies(new string[] { mPath });
        //循环遍历所有引用,加入到allDependencies
        foreach (var file in deps)
        {
            string suffix = BuildCommon.getFileSuffix(file);
            if (file == mPath || suffix == "cs" || suffix == "dll")
            {
                continue;
            }
            mAllDependencies.Add(file);
        }
        mDirectUpperDependences = new List <AssetUnit>();
    }
Beispiel #3
0
    public static void BuildAllArtsFileList()
    {
        s_AllArtsFiles.Clear();

        DateTime dt1 = System.DateTime.UtcNow;

        DisposeSceneRes();

        DateTime dt2 = System.DateTime.UtcNow;

        DisposeResourceFolder();

        DateTime            dt3      = System.DateTime.UtcNow;
        ArtResourceFileList filelist = BuildMD5();

        DateTime dt4 = System.DateTime.UtcNow;

        BuildCommon.WriteJsonToFile("Assets", "ArtsFileList.txt", filelist, true);

        DateTime dt5 = System.DateTime.UtcNow;

        string info = string.Format("MD5计算完成\n总共{0}个文件\n耗时:{1}分钟\n其中:\n"
                                    , s_AllArtsFiles.Count, (dt5 - dt1).TotalMinutes.ToString("f1"));

        info = string.Format("{0}分析场景资源耗时:{1}秒\n", info, (dt2 - dt1).TotalSeconds.ToString("f1"));
        info = string.Format("{0}分析Resource资源耗时:{1}秒\n", info, (dt3 - dt2).TotalSeconds.ToString("f1"));
        info = string.Format("{0}生成MD5耗时:{1}秒\n", info, (dt4 - dt3).TotalSeconds.ToString("f1"));
        info = string.Format("{0}保存MD5文件耗时:{1}秒\n", info, (dt5 - dt4).TotalSeconds.ToString("f1"));

        EditorUtility.DisplayDialog("计算完成", info, "好的");
    }
    public static void GetBuildResources()
    {
        mResources.Clear();

        //Resource资源路径
        string resourcePath = Application.dataPath + "/Resources/";

        string[] files = Directory.GetFiles(resourcePath, "*.*", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            string suffix = BuildCommon.getFileSuffix(file);
            if (suffix == "meta")
            {
                continue;
            }
            string realFile = file.Replace("\\", "/");
            realFile = realFile.Replace(Application.dataPath, "Assets");

            if (realFile == "Assets/Resources/Version.bytes")
            {
                continue;
            }

            mResources.Add(realFile);
        }
    }
Beispiel #5
0
    public static void CheckEffectInResource()
    {
        //获取所有资源路径
        mEffectResources.Clear();

        //Resource资源路径
        string resourcePath = Application.dataPath + "/Resources/effect";

        string[] files = Directory.GetFiles(resourcePath, "*.*", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            string suffix = BuildCommon.getFileSuffix(file);
            if (suffix == "meta")
            {
                continue;
            }

            //查找预制件件
            if (suffix == "prefab")
            {
                string realFile = file.Replace("\\", "/");
                realFile = realFile.Replace(Application.dataPath, "Assets");
                mEffectResources.Add(realFile);
            }
        }

        List <ParticleSystem> pss = new List <ParticleSystem>();

        //查找是否缺少EffectScript
        foreach (string assetPath in mEffectResources)
        {
            GameObject asset = AssetDatabase.LoadMainAssetAtPath(assetPath) as GameObject;

            //检测是否缺少EffectScript脚本
            EffectScript effectScript = asset.GetComponent <EffectScript>();
            if (effectScript == null)
            {
                Debug.Log("the effect " + assetPath + " miss EffectScript!");
            }

            SL_DestroyByTime destTimeScript = asset.GetComponent <SL_DestroyByTime>();
            if (destTimeScript != null)
            {
                Debug.Log("do not use the SL_DestoryByTime script now!");
            }

            pss.Clear();
            //检测ParticleSystem
            RecusionParticleSystemObject(asset, ref pss);
            foreach (ParticleSystem ps in pss)
            {
                if (ps.GetComponent <Renderer>().castShadows || ps.GetComponent <Renderer>().receiveShadows)
                {
                    Debug.Log(asset.name + " " + ps.name + "is cast shadow or receive shadow");
                }
            }
        }
        Debug.Log("Check finised!");
    }
 public static void DisposeAllFbx()
 {
     foreach (string fbx in fbxList)
     {
         GameObject gObj = AssetDatabase.LoadAssetAtPath(fbx, typeof(GameObject)) as GameObject;
         BuildCommon.ChangeMaterial(gObj);
     }
 }
    public static void BuildFileList(bool show_dialog)
    {
        Clear();

        DateTime dt1 = System.DateTime.UtcNow;
        // 获取AssetBundleManifest;
        string manifestPath          = string.Format("{0}/{1}", PackAssetBundle.bundleBuildFolder, ResourceConst.PkgBundleFolder);
        AssetBundleManifest manifest = PackBundleTools.GetAssetBundleManifest(manifestPath);

        if (manifest == null)
        {
            return;
        }

        // 计算总控文件;
        AddBundleInfo(ResourceConst.PkgBundleFolder, ref s_FileList);

        // 计算Config;
        AddConfInfo(s_FileList);

        // 计算其它文件;
        string[] allBundleKeyList = manifest.GetAllAssetBundles();
        for (int i = 0; i < allBundleKeyList.Length; i++)
        {
            string file = allBundleKeyList[i];
            if (string.IsNullOrEmpty(file))
            {
                continue;
            }
            AddBundleInfo(file, ref s_FileList);

            PackAssetBundleUtlis.ShowProgress(i, allBundleKeyList.Length, "计算MD5:", file);
        }
        EditorUtility.ClearProgressBar();

        string   strFileList = string.Format("{0}.txt", ResourceConst.FileListName);
        DateTime dt2         = System.DateTime.UtcNow;

        BuildCommon.WriteJsonToFile(PackAssetBundle.bundleBuildFolder, strFileList, s_FileList);

        FileDepencies.WriteMd5Info();

        DateTime dt3 = System.DateTime.UtcNow;

        BuildFileListAssetBundle(PackAssetBundle.bundleBuildFolder, strFileList, ResourceConst.FileListName);

        DateTime dt4 = System.DateTime.UtcNow;

        if (show_dialog)
        {
            string info = string.Format("FileList生成完成,总时长{0}秒", (dt4 - dt1).TotalSeconds.ToString("f1"));
            EditorUtility.DisplayDialog("打包完成", info, "好的");
        }
    }
    public static string GetBundleName(string path, string folder = "")
    {
        if (string.IsNullOrEmpty(path))
        {
            return("");
        }

        // Shader统一打包;
        string extension = BuildCommon.GetExtension(path);

        if (BuildCommon.IsShaderAsset(path, extension))
        {
            return(ResourceConst.bundleShader);
        }

        string bundleName = path.ToLower();
        string fileName   = Path.GetFileNameWithoutExtension(bundleName);

        if (!string.IsNullOrEmpty(folder))
        {
            bundleName = string.Format("{0}/{1}{2}", folder, fileName, ResourceConst.BundleExtensions);

            return(bundleName);
        }

        if (bundleName.StartsWith(ResourceConst.AssetResourceName.ToLower()))
        {
            bundleName = bundleName.Substring(ResourceConst.AssetResourceName.Length);
        }

        string bundleExtension = Path.GetExtension(bundleName);

        bundleName = bundleName.Trim();
        bundleName = bundleName.Replace('\\', '/');
        bundleName = bundleName.Replace(" ", "_");
        bundleName = bundleName.Replace("#", "_");
        bundleName = bundleName.Replace(bundleExtension, ResourceConst.BundleExtensions);

        if (BuildCommon.IsTextureAsset(path, extension))
        {
            bundleName = bundleName.Replace("_rgb", "_texture");
            bundleName = bundleName.Replace("_alpha", "_texture");
        }

        //if ( path.Contains("bg_2.jpg") )
        //{
        //    Debug.LogErrorFormat("{0} -------------> {1}", path, bundleName);
        //}

        return(bundleName);
    }
Beispiel #9
0
 public void InitCollectDepData(AssetUnit unit)
 {
     if (!this.mDicCollectDepResourceData.ContainsKey(unit.mName))
     {
         List <string> deps = unit.mAllDependencies;
         List <string> temp = new List <string>();
         foreach (var dep in deps)
         {
             string name = BuildCommon.GetLevelABPathName(dep);
             temp.Add(name);
         }
         this.mDicCollectDepResourceData.Add(unit.mName, new CollectDepResourceData(unit.mName, temp));
     }
 }
    public static void CompareFileListTxt()
    {
        string newPath = EditorUtility.OpenFilePanel("选择新版FileList", last_newFileList_dir, "");
        string oldPath = EditorUtility.OpenFilePanel("选择旧版FileList", last_oldFileList_dir, "");

        if (string.IsNullOrEmpty(newPath) || string.IsNullOrEmpty(oldPath))
        {
            return;
        }
        last_newFileList_dir = Path.GetDirectoryName(newPath);
        last_oldFileList_dir = Path.GetDirectoryName(oldPath);

        FileListCompareAll compareAll = Compare(newPath, oldPath);

        if (compareAll == null)
        {
            EditorUtility.DisplayDialog("比较失败", "比较失败,详情看Log", "好的");
            return;
        }

        if (!EditorUtility.DisplayDialog("比较完成", string.Format("{0}\n{1}\n{2}", newPath, oldPath, compareAll.ToString()), "保存", "好的"))
        {
            return;
        }

        string savePath = EditorUtility.SaveFilePanel("保存", "", "filelist_compare", "txt");

        if (string.IsNullOrEmpty(savePath))
        {
            return;
        }

        BuildCommon.WriteJsonToFile("", savePath, compareAll);
        Debug.LogFormat("保存");

//         return;
//
//         FileList newFileList = BuildCommon.ReadJsonFromFile<FileList>(newPath);
//         FileList oldFileList = BuildCommon.ReadJsonFromFile<FileList>(oldPath);
//
//         if (newFileList == null || oldFileList == null)
//         {
//             return;
//         }
//
//
//         FileListCompareData comData = FileList.Compare(newFileList, oldFileList);
//         Debug.LogErrorFormat("{0}, {1}, {2}", comData.addList.Count, comData.deleteList.Count, comData.modifiyList.Count);
    }
    public static bool BuildFileListBundle(FileList fileList, string fileName, string bundleName)
    {
        try
        {
            BuildCommon.WriteJsonToFile(PackAssetBundle.bundleBuildFolder, fileName, fileList);

            BuildFileListAssetBundle(PackAssetBundle.bundleBuildFolder, fileName, bundleName);
        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex.ToString());
            return(false);
        }
        return(true);
    }
    private static FileList ReadFileList(string path)
    {
        string fileName = System.IO.Path.GetFileName(path);

        if (string.Equals(fileName, ResourceConst.FileListName))
        {
            return(PackBundleTools.GetAssetBundleFileListFromAbsPath(path));
        }

        if (string.Equals(fileName, ResourceConst.FileListName + ".txt"))
        {
            return(BuildCommon.ReadJsonFromFile <FileList>(path));
        }

        return(null);
    }
    public static void TestDep()
    {
        if (Selection.activeObject == null)
        {
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);

        if (string.IsNullOrEmpty(path))
        {
            return;
        }

        List <string> dep_PrefabList  = new List <string>();
        List <string> dep_MatList     = new List <string>();
        List <string> dep_OtherList   = new List <string>();
        List <string> dep_TextureList = new List <string>();

        string[] deps = AssetDatabase.GetDependencies(path);
        foreach (string dep in deps)
        {
            Debug.LogError(dep);
            string extension = System.IO.Path.GetExtension(dep);
            if (BuildCommon.IsPrefabAsset(dep, extension))
            {
                dep_PrefabList.Add(dep);
            }
            else if (BuildCommon.IsMaterialAsset(dep, extension))
            {
                dep_MatList.Add(dep);
            }
            else if (BuildCommon.IsTextureAsset(dep, extension))
            {
                dep_TextureList.Add(dep);
            }
            else
            {
                dep_OtherList.Add(dep);
            }
        }

        Print(dep_PrefabList);
        Print(dep_MatList);
        Print(dep_OtherList);
        Print(dep_TextureList);
    }
    public static void DumpVersionFile()
    {
        List <string> allFiles = new List <string>();

        BuildCommon.GetFiles(ResourceCommon.assetbundleFilePath, allFiles, true);

        XmlDocument doc  = new XmlDocument();
        XmlElement  root = doc.CreateElement("Version");

        root.SetAttribute("Number", "1.0.0");
        root.SetAttribute("Big", "false");

        foreach (string element in allFiles)
        {
            int        size         = 0;
            string     md5          = GetFileMD5(element, ref size);
            XmlElement ele          = doc.CreateElement("file");
            string     relativePath = element.Replace(Application.dataPath + "/assetbundles/", "");

            ele.SetAttribute("fpath", relativePath);
            ele.SetAttribute("size", size.ToString());
            ele.SetAttribute("md5", md5);
            root.AppendChild(ele);

            //保存大小信息到AssetUnit中
            string assetName = "Assets/" + relativePath;
            assetName = assetName.Substring(0, assetName.Length - 6);
            AssetUnit assetUnit = GetAssetUnit(assetName);
            if (assetUnit != null)
            {
                assetUnit.mAssetSize = size;
            }
        }

        doc.AppendChild(root);
        string assetBundleVersionPath = ResourceCommon.assetbundleFilePath + "Version.bytes";

        doc.Save(assetBundleVersionPath);

        //拷贝到对应的Resource目录
        string resourceVersionPath = Application.dataPath + "/Resources/" + "Version.bytes";

        File.Copy(assetBundleVersionPath, resourceVersionPath, true);

        Debug.Log("Dump Version Success!!!");
    }
    public static bool BuildFileListByConf()
    {
        Clear();

        // 读取旧的FileList;
        string   strFileList  = string.Format("{0}.txt", ResourceConst.FileListName);
        string   fileListPath = string.Format("{0}/{1}", PackAssetBundle.bundleBuildFolder, strFileList);
        FileList fileList     = BuildCommon.ReadJsonFromFile <FileList>(fileListPath);

        if (fileList == null)
        {
            Debug.LogErrorFormat("ReadJsonFromFile {0} Failed!", fileListPath);
            return(false);
        }
        // 计算FileList;
        AddConfInfo(fileList);
        return(BuildFileListBundle(fileList, strFileList, ResourceConst.FileListName));
    }
Beispiel #16
0
    public static void RenameFromZero()
    {
        string selectForderPath = AssetDatabase.GetAssetPath(Selection.activeObject);

        string[] files = Directory.GetFiles(selectForderPath, "*.*", SearchOption.AllDirectories);
        int      i     = 0;

        foreach (var file in files)
        {
            if (BuildCommon.getFileSuffix(file) == "meta")
            {
                continue;
            }
            AssetDatabase.RenameAsset(file, i.ToString());
            AssetDatabase.Refresh();
            i++;
        }
    }
Beispiel #17
0
    public static string GetMD5FromFile(string path)
    {
        if (string.IsNullOrEmpty(path))
        {
            return("");
        }

        if (s_FileList == null)
        {
            s_FileList = BuildCommon.ReadJsonFromFile <ArtResourceFileList>("Assets/" + "ArtsFileList.txt");
        }

        if (s_FileList == null)
        {
            return("");
        }

        return(s_FileList.GetMD5(path));
    }
    private static void GetBuildResource()
    {
        mResource.Clear();
        string resourcePath = Application.dataPath + "/Resources/";

        //文件过滤,选取*.*文件
        string[] files = Directory.GetFiles(resourcePath, "*.*", SearchOption.AllDirectories);
        foreach (var file in files)
        {
            string suffix = BuildCommon.getFileSuffix(file);
            if (suffix == "meta")
            {
                continue;
            }
            string realFile = file.Replace("\\", "/");
            realFile = realFile.Replace(Application.dataPath, "Assets");
            mResource.Add(realFile);
        }
    }
    //创建资源索引
    public static void DumpResourceFile()
    {
        ////创建所有资源Asset列表
        XmlDocument doc  = new XmlDocument();
        XmlElement  root = doc.CreateElement("AllResources");

        XmlElement resource = doc.CreateElement("Resources");

        root.AppendChild(resource);
        foreach (string res in mResources)
        {
            string ex   = BuildCommon.getFileSuffix(res);
            string path = res.Replace("Assets/", "");
            path = path.Replace("." + ex, "");

            XmlElement ele = doc.CreateElement("file");
            ele.SetAttribute("name", path);
            ele.SetAttribute("type", ex);
            resource.AppendChild(ele);
        }

        //创建所有需要打包的Level列表
        XmlElement sceneRes = doc.CreateElement("Level");

        root.AppendChild(sceneRes);
        foreach (string scene in mScenes)
        {
            XmlElement ele = doc.CreateElement("file");

            string path = scene.Replace("Assets/", "");
            path = path.Replace(".unity", "");

            ele.SetAttribute("name", path);
            ele.SetAttribute("type", "unity");
            sceneRes.AppendChild(ele);
        }
        doc.AppendChild(root);
        BuildCommon.CheckFolder(BuildCommon.getPath(ResourceCommon.assetbundleFilePath));
        doc.Save(ResourceCommon.assetbundleFilePath + "Resource.bytes");

        Debug.Log("CreateResourceCfg success!!!");
    }
    public static bool IsFilterSceneRes(string dep, string file)
    {
        if (string.IsNullOrEmpty(dep) || string.Equals(dep, file))
        {
            return(true);
        }

        // 只记录贴图、动作、音效资源;
        string extension = BuildCommon.GetExtension(dep);

        if (BuildCommon.IsTextureAsset(dep, extension) ||
            BuildCommon.IsAudioAsset(dep, extension) ||
            BuildCommon.IsShaderAsset(dep, extension)
            //|| BuildCommon.IsBigModelAsset(dep,extension)
            )
        {
            return(false);
        }

        return(true);
    }
Beispiel #21
0
    public List <AssetUnit> mDirectUpperDependences; //直接上层依赖,用于判定assetbundle加载后是否直接删除

    public AssetUnit(string path)
    {
        mAssetSize             = 0;
        mPath                  = path;
        mName                  = Path.GetFileNameWithoutExtension(path);
        mSuffxi                = Path.GetExtension(path);
        mLevel                 = BuildCommon.getAssetLevel(mPath);
        mAllDependencies       = new List <string>();
        mNextLevelDependencies = new List <AssetUnit>();
        string[] deps = AssetDatabase.GetDependencies(new string[] { mPath });

        foreach (string file in deps)
        {
            string suffix = Path.GetExtension(file);
            if (suffix.Equals("dll") || file == mPath)
            {
                continue;
            }
            mAllDependencies.Add(file);
        }
        mDirectUpperDependences = new List <AssetUnit>();
    }
Beispiel #22
0
    public static void FindMissScriptInResource()
    {
        go_count         = 0;
        components_count = 0;
        missing_count    = 0;

        //获取所有资源路径
        mResources.Clear();

        //Resource资源路径
        string resourcePath = Application.dataPath + "/Resources/";

        string[] files = Directory.GetFiles(resourcePath, "*.*", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            string suffix = BuildCommon.getFileSuffix(file);
            if (suffix == "meta")
            {
                continue;
            }

            //查找预制件件
            if (suffix == "prefab")
            {
                string realFile = file.Replace("\\", "/");
                realFile = realFile.Replace(Application.dataPath, "Assets");
                mResources.Add(realFile);
            }
        }

        //查找所有miss文件
        foreach (string assetPath in mResources)
        {
            GameObject asset = AssetDatabase.LoadMainAssetAtPath(assetPath) as GameObject;
            FindInGO(asset);
        }
        Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count));
    }
    public List <AssetUnit> mDirectUpperDependences; //直接上层依赖,用于判定assetbundle加载后是否可以马上删除

    public AssetUnit(string path)
    {
        mAssetSize             = 0;
        mPath                  = path;
        mName                  = BuildCommon.getFileName(mPath, true);
        mSuffix                = BuildCommon.getFileSuffix(mName);
        mLevel                 = BuildCommon.getAssetLevel(mPath);
        mAllDependencies       = new List <string>();
        mNextLevelDependencies = new List <AssetUnit>();
        string[] deps = AssetDatabase.GetDependencies(new string[] { mPath });

        foreach (string file in deps)
        {
            string suffix = BuildCommon.getFileSuffix(file);
            //if (file == mPath || suffix == "dll" || suffix == "cs")
            if (file == mPath || suffix == "dll")
            {
                continue;
            }
            mAllDependencies.Add(file);
        }
        mDirectUpperDependences = new List <AssetUnit>();
    }
Beispiel #24
0
    public static List <string> GetDepTextures(string filePath)
    {
        List <string> depList = new List <string>();

        if (string.IsNullOrEmpty(filePath))
        {
            return(depList);
        }

        string[] ass  = { filePath };
        string[] deps = AssetDatabase.GetDependencies(ass);

        foreach (string dep in deps)
        {
            if (!BuildCommon.IsTextureAsset(dep, BuildCommon.GetExtension(dep)))
            {
                continue;
            }

            depList.Add(dep);
        }

        return(depList);
    }
    public void DisposeFbx()
    {
        fbxList.Clear();
        AssetDatabase.Refresh();
        foreach (AssetBundleBuildEx ab in mBundleDictionary.Values)
        {
            foreach (string dep in ab.assetNames)
            {
                string extension = BuildCommon.GetExtension(dep);
                if (!BuildCommon.IsModelAsset(dep, extension))
                {
                    continue;
                }

                if (fbxList.Contains(dep))
                {
                    continue;
                }

                fbxList.Add(dep);
            }
        }
        DisposeAllFbx();
    }
Beispiel #26
0
    public static void WriteMd5Info()
    {
        string strFileList = string.Format("{0}.txt", ResourceConst.MD5Name);

        BuildCommon.WriteJsonToFile(PackAssetBundle.bundleBuildFolder, strFileList, md5List);
    }
    //创建Asset资源信息
    public static void DumpAssetInfoFile()
    {
        if (allLevelAssets.Count == 0)
        {
            return;
        }

        ////创建所有资源Asset列表
        XmlDocument doc  = new XmlDocument();
        XmlElement  root = doc.CreateElement("AllAssets");

        //遍历所有Asset数据
        for (int level = 1; level <= allLevelAssets.Count; level++)
        {
            Dictionary <string, AssetUnit> levelAssets = allLevelAssets[level];
            foreach (KeyValuePair <string, AssetUnit> asset in levelAssets)
            {
                string    assetName = asset.Key;
                AssetUnit assetUnit = asset.Value;

                XmlElement ele = doc.CreateElement("Asset");

                //设置路径名称
                assetName = assetName.Replace("Assets/", "");
                ele.SetAttribute("name", assetName);

                //设置asset索引
                ele.SetAttribute("index", assetUnit.mIndex.ToString());

                //设置等级
                ele.SetAttribute("level", level.ToString());

                List <AssetUnit> sortDepencys = new List <AssetUnit>();
                //获取AssetUnit所有依赖,并排序
                List <string> depencys = assetUnit.mAllDependencies;
                foreach (string depency in depencys)
                {
                    AssetUnit depencyUnit = GetAssetUnit(depency);
                    sortDepencys.Add(depencyUnit);
                }

                //排序
                sortDepencys.Sort(SortAssetUnit);
                //保存依赖索引
                string depencystr = "";
                for (int i = 0; i < sortDepencys.Count; i++)
                {
                    AssetUnit unit = sortDepencys[i];

                    if (i != sortDepencys.Count - 1)
                    {
                        depencystr += unit.mIndex + ",";
                    }
                    else
                    {
                        depencystr += unit.mIndex;
                    }
                }
                ele.SetAttribute("depency", depencystr.ToString());

                root.AppendChild(ele);
            }
        }

        doc.AppendChild(root);
        BuildCommon.CheckFolder(BuildCommon.getPath(ResourceCommon.assetbundleFilePath));
        doc.Save(ResourceCommon.assetbundleFilePath + "AssetInfo.bytes");

        Debug.Log("CreateAssetInfo success!!!");
    }
Beispiel #28
0
    public static void CheckMaterialReference()
    {
        Object[] objects = Selection.objects;
        if (objects.Length != 1)
        {
            EditorUtility.DisplayDialog("Error", "you should select one items", "ok");
            return;
        }

        Object   obj = objects[0];
        Material mat = obj as Material;

        if (mat == null)
        {
            EditorUtility.DisplayDialog("Error", "you should select material", "ok");
            return;
        }


        string matPath = AssetDatabase.GetAssetPath(mat);

        //Debug.Log(matPath);



        //获取所有资源路径
        mResources.Clear();

        //Resource资源路径
        string resourcePath = Application.dataPath + "/Resources/";

        string[] files = Directory.GetFiles(resourcePath, "*.*", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            string suffix = BuildCommon.getFileSuffix(file);
            if (suffix == "meta")
            {
                continue;
            }

            //查找预制件件
            if (suffix == "prefab")
            {
                string realFile = file.Replace("\\", "/");
                realFile = realFile.Replace(Application.dataPath, "Assets");
                mResources.Add(realFile);
            }
        }

        //查找所有引用该Material的文件
        foreach (string assetPath in mResources)
        {
            //获取包含的所有依赖
            string[] depencies = AssetDatabase.GetDependencies(new string[] { assetPath });
            foreach (string dep in depencies)
            {
                //如果是材质
                if (dep.EndsWith(".mat") && matPath == dep)
                {
                    Debug.Log(mat.name + "is referenced by" + assetPath);
                }
            }
        }
    }
    //[MenuItem("Bundles/DisposeAllFbxs2")]
    //public static void DisposeAllFbxs2()
    //{
    //    BuildCommon.ChangeMaterial(Selection.activeGameObject);
    //}

    //[MenuItem("Bundles/DisposeAllFbxs")]
    //public static void DisposeAllFbxs()
    //{
    //    List<string> fbxs = EditorCommon.GetAllFiles("Assets/Arts/", "*.fbx");
    //    Debug.LogFormat("{0}", fbxs.Count);

    //    for( int i=0; i< fbxs.Count; i++ )
    //    {
    //        string fbxPath = fbxs[i];
    //        GameObject gObj = AssetDatabase.LoadAssetAtPath(fbxPath, typeof(GameObject)) as GameObject;
    //        BuildCommon.ChangeMaterial(gObj);

    //        PackAssetBundleUtlis.ShowProgress(i, fbxs.Count, "处理Fbx", "处理Fbx");

    //        if (i % 50 == 0)
    //        {
    //            AssetDatabase.SaveAssets();
    //            AssetDatabase.Refresh();
    //        }
    //    }
    //    EditorUtility.ClearProgressBar();
    //}

    public static int GetAssetBundleBuildPriority(AssetBundleBuild build)
    {
        if (build.assetNames == null || build.assetNames.Length < 1)
        {
            return(-1);
        }

        string path = build.assetNames[0];

        if (string.IsNullOrEmpty(path))
        {
            return(-1);
        }

        string extension = System.IO.Path.GetExtension(path);

        extension = extension.ToLower();

        if (string.Equals(extension, ".unity"))
        {
            return(100);
        }

        if (BuildCommon.IsShaderAsset(path, extension))
        {
            return(0);
        }

        if (BuildCommon.IsTextureAsset(path, extension, false))
        {
            return(1);
        }

        if (BuildCommon.IsAnimAsset(path, extension))
        {
            return(2);
        }

        if (BuildCommon.IsAudioAsset(path, extension))
        {
            return(3);
        }

        if (BuildCommon.IsMaterialAsset(path, extension))
        {
            return(4);
        }

        if (BuildCommon.IsModelAsset(path, extension))
        {
            return(5);
        }

        if (BuildCommon.IstTTFFontAsset(path, extension))
        {
            return(6);
        }

        if (BuildCommon.IsUIFontAsset(path, extension))
        {
            return(21);
        }

        if (BuildCommon.IsAtlasAsset(path, extension))
        {
            return(22);
        }

        if (BuildCommon.IsPrefabAsset(path, extension))
        {
            return(99);
        }
        //Debug.Log(path);
        return(20);
    }
    public static void BuildResource()
    {
        AssetDatabase.Refresh();

        //执行依赖性打包

        //资源最大等级
        int maxLevel = allLevelAssets.Count;

        if (maxLevel == 0)
        {
            return;
        }

        //从最低等级开始打包
        for (int level = 1; level <= maxLevel; level++)
        {
            BuildPipeline.PushAssetDependencies();

            //获取不同等级的aaset
            Dictionary <string, AssetUnit> levelAssets = allLevelAssets[level];

            //遍历该等级的所有asset打包
            foreach (KeyValuePair <string, AssetUnit> pair in levelAssets)
            {
                //根据路径获取asset资源
                Object asset = AssetDatabase.LoadMainAssetAtPath(pair.Value.mPath);
                if (null == asset)
                {
                    DebugEx.LogError("load " + pair.Value.mPath + " failed!!!", "BuildResource", true);
                }

                //生成打包保存路径
                string savePath = pair.Value.mPath.Insert(6, assetFilePath) + ResourceCommon.assetbundleFileSuffix;
                BuildCommon.CheckFolder(BuildCommon.getPath(savePath));

                //打包名称去Asset



                //普通资源
                if (pair.Value.mSuffix != "unity")//普通资源
                {
                    string assetName = pair.Value.mPath.Replace("Assets/", "");

                    //资源打包
                    if (!BuildPipeline.BuildAssetBundleExplicitAssetNames(
                            new Object[] { asset }, new string[] { assetName }, savePath, options, buildPlatform))
                    {
                        DebugEx.LogError("build assetbundle " + savePath + " failed!!!", "BuildResource", true);
                    }
                }
                //场景资源,没有依赖场景的
                else
                {
                    AssetDatabase.Refresh();
                    BuildPipeline.PushAssetDependencies();
                    string error = BuildPipeline.BuildStreamedSceneAssetBundle(new string[] { pair.Value.mPath }, savePath, buildPlatform);
                    if (error != "")
                    {
                        DebugEx.LogError(error, "BuildResource", true);
                    }
                    BuildPipeline.PopAssetDependencies();

                    Debug.Log("scene path" + pair.Value.mPath);
                    //pair.Value.mPath
                }
            }
        }

        //popdepency依赖
        for (int level = 1; level <= maxLevel; level++)
        {
            BuildPipeline.PopAssetDependencies();
        }
    }