Esempio n. 1
0
    private static void PackageOnePath(string configFilePath, TabFile tf)
    {
        while (tf.Next())
        {
            string strResFilePath = tf.Get <string>("FilePath");

            float val = m_curIndex * 1.0f / m_totalFileCount;
            EditorUtility.DisplayProgressBar("Updating", "Packaging, " + Path.GetFileNameWithoutExtension(strResFilePath) + "please wait..." + val * 100 + "%", val);
            m_curIndex++;


            string realFilePath = "Assets" + "/" + strResFilePath;
            //sPackageManager.AddAsset(realFilePath,AssetType);
            realFilePath = realFilePath.Replace("\\", "/");

            string[] allDependObjectPaths = AssetDatabase.GetDependencies(new string[] { realFilePath });
            for (int i = 0; i < allDependObjectPaths.Length; i++)
            {
                string Ext = Path.GetExtension(allDependObjectPaths[i]).ToLower();
                if (Ext == ".cs" || Ext == ".js")
                {
                    continue;
                }
                AssetImporter ai = AssetImporter.GetAtPath(allDependObjectPaths[i]);
                if (ai == null)
                {
                    UnityEngine.Debug.Log("not find Resource " + allDependObjectPaths[i]);
                    continue;
                }

                //替换符号
                allDependObjectPaths[i] = allDependObjectPaths[i].Replace("\\", "/");
                var    temp      = allDependObjectPaths[i].Split('/');
                string derictory = "";
                bool   isAdd     = false;
                foreach (var s in temp)
                {
                    if (isAdd)
                    {
                        if (derictory == "")
                        {
                            derictory = s;
                        }
                        else
                        {
                            derictory = derictory + "_" + s;
                        }
                    }
                    else if (s.Equals("Resources") || s.Equals("Resources"))
                    {
                        isAdd = true;
                    }
                }
                derictory             = derictory.Replace(".", "@");
                ai.assetBundleName    = derictory;
                ai.assetBundleVariant = "";
            }
        }
    }
Esempio n. 2
0
    public static void Deploy()
    {
        string[] resConfigs = new string[] {
            "Other/Config/Resource/Audio.txt",
            "Other/Config/Resource/TextAsset.txt",
            "Other/Config/Resource/Model.txt",
            "Other/Config/Resource/Effect.txt",
            "Other/Config/Resource/Scene.txt",
            "Other/Config/Resource/UIAtlas.txt",
            "Other/Config/Resource/UIPanel.txt",
            "Other/Config/Resource/Material.txt",
            "Other/Config/Resource/Cursor.txt",
        };

        // 生成MainAsset的List, 并验证配置文件是否合理
        bool             isMissFile = false;
        int              MissCount  = 0;
        List <MainAsset> mainAssets = new List <MainAsset>();

        for (int i = 0; i < resConfigs.Length; i++)
        {
            TextAsset textAsset = AssetDatabase.LoadMainAssetAtPath("Assets/" + resConfigs[i]) as TextAsset;
            if (null == textAsset)
            {
                Log.Write(LogLevel.WARN, "config is null {0}", resConfigs[i]);
                return;
            }

            string  strAssetType = Path.GetFileNameWithoutExtension(resConfigs[i]);
            TabFile tabFile      = new TabFile(textAsset.name, textAsset.text);
            while (tabFile.Next())
            {
                int    nId         = tabFile.Get <int>("id");
                string strFilePath = tabFile.Get <string>("FilePath");
                for (int j = 0; j < mainAssets.Count; j++)
                {
                    MainAsset mainAsset = mainAssets[j];
                    if (mainAsset.m_strFilePath == strFilePath)
                    {
                        Log.Write(LogLevel.WARN, "exist two same path {0} {1}, {2}", strFilePath, mainAsset.m_strAssetType, strAssetType);
                        return;
                    }
                    if (mainAsset.m_strAssetType == strAssetType && mainAsset.m_nId == nId)
                    {
                        Log.Write(LogLevel.WARN, "{0} repleat id {1}", strAssetType, nId);
                        return;
                    }
                }
                UnityEngine.Object fileObj = AssetDatabase.LoadMainAssetAtPath("Assets/" + strFilePath);
                if (null == fileObj)
                {
                    isMissFile = true;
                    Log.Write(LogLevel.WARN, "null id:{0} name:{1}", nId, strFilePath);
                    MissCount++;
                    continue;
                }

                //UnityEngine.Object[] dependObjects = EditorUtility.CollectDependencies(new UnityEngine.Object[] { fileObj });

                string    fileName = Path.GetFileNameWithoutExtension(strFilePath);
                MainAsset asset    = new MainAsset(strAssetType, nId, strFilePath, fileObj, fileName);
                mainAssets.Add(asset);
            }
        }

        if (isMissFile)
        {
            Log.Write(LogLevel.WARN, "has Miss File Count {0}", MissCount);
        }

        // 分析依赖关系
        SortedList <UnityEngine.Object, List <MainAsset> > depends = new SortedList <UnityEngine.Object, List <MainAsset> >(new objectCompare());

        for (int i = 0; i < mainAssets.Count; i++)
        {
            MainAsset            mainAsset     = mainAssets[i];
            UnityEngine.Object[] dependObjects = EditorUtility.CollectDependencies(new UnityEngine.Object[] { mainAsset.m_obj });
            for (int j = 0; j < dependObjects.Length; j++)
            {
                UnityEngine.Object dependObj = dependObjects[j];
                if (!depends.ContainsKey(dependObj))
                {
                    depends.Add(dependObj, new List <MainAsset>());
                }
                depends[dependObj].Add(mainAsset);
            }
        }

        // 根据依赖关系, 分包
        SortedList <List <MainAsset>, List <UnityEngine.Object> > assets = new SortedList <List <MainAsset>, List <UnityEngine.Object> >(new assetListCompare());

        foreach (KeyValuePair <UnityEngine.Object, List <MainAsset> > kvp in depends)
        {
            List <UnityEngine.Object> list = null;
            if (!assets.ContainsKey(kvp.Value))
            {
                list = new List <UnityEngine.Object>();
                assets.Add(kvp.Value, list);
            }
            if (null == list)
            {
                list = assets[kvp.Value];
            }
            list.Add(kvp.Key);
        }

        // 准备XML配置文件
        string      strXmlFile = "Other/Config/Resource/wwwResource.xml";
        XmlDocument xmlDoc     = new XmlDocument();

        xmlDoc.Load(Application.dataPath + "/" + strXmlFile);
        xmlDoc.RemoveAll();
        XmlElement root = xmlDoc.CreateElement("AssetList");

        xmlDoc.AppendChild((XmlNode)root);
        root.SetAttribute("TotalSize", "0");

        SortedList <string, XmlElement> typeXmlEle = new SortedList <string, XmlElement>();

        for (int i = 0; i < mainAssets.Count; i++)
        {
            MainAsset mainAsset = mainAssets[i];
            if (!typeXmlEle.ContainsKey(mainAsset.m_strAssetType))
            {
                XmlElement typeEle = xmlDoc.CreateElement(mainAsset.m_strAssetType);
                typeEle.SetAttribute("TotalSize", "0");
                root.AppendChild(typeEle);
                typeXmlEle.Add(mainAsset.m_strAssetType, typeEle);
            }
            mainAsset.m_xmlParentEle = typeXmlEle[mainAsset.m_strAssetType];
            mainAsset.m_xmlSelfEle   = xmlDoc.CreateElement("Asset");
            mainAsset.m_xmlSelfEle.SetAttribute("Id", "0");
            mainAsset.m_xmlSelfEle.SetAttribute("Version", mainAsset.m_Version.ToString());
            mainAsset.m_xmlSelfEle.SetAttribute("TotalSize", "0");
            mainAsset.m_xmlParentEle.AppendChild(mainAsset.m_xmlSelfEle);
        }

        //----------------------------------------------------------------------打包
        BuildPipeline.PushAssetDependencies();
        // 打包客户端
//		string errorHead = BuildPipeline.BuildPlayer(new string[]{"Assets/Scene/JueSe.unity"}, buildDir, BuildTarget.WebPlayer, BuildOptions.None);
//		if(!string.IsNullOrEmpty(errorHead))
//		{
//			Log.Write(LogLevel.ERROR,"{0}",errorHead);
//			return ;
//		}

        // 打包Shared
        int k = 0;

        foreach (KeyValuePair <List <MainAsset>, List <UnityEngine.Object> > kvp in assets)
        {
            List <MainAsset> listMainAsset = kvp.Key;
            if (listMainAsset.Count <= 1)
            {
                //MainAsset te1 = listMainAsset[0];
                //Log.Write(LogLevel.INFO,"objectName is {0}",te1.m_Name);
                continue;
            }

            List <UnityEngine.Object> listObject = kvp.Value;
            int    version    = MainAsset.GenVersion();
            string strTgtPath = "Shared/Shared" + (++k) + '_' + version.ToString() + ".asset";
            bool   isSucess   = BuildPipeline.BuildAssetBundle(null, listObject.ToArray(), buildDir + "/" + strTgtPath,
                                                               BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, EditorUserBuildSettings.activeBuildTarget);

            if (!isSucess)
            {
                Log.Write(LogLevel.DEBUG, "build {0} failed", strTgtPath);
                continue;
            }

            // 设置大小观察信息
            FileInfo fileInfo = new FileInfo(buildDir + "/" + strTgtPath);
            double   fileSize = fileInfo.Length / 1024f;
            root.SetAttribute("TotalSize", (fileSize + Convert.ToDouble(root.GetAttribute("TotalSize"))).ToString("0.00"));

            for (int j = 0; j < listMainAsset.Count; j++)
            {
                MainAsset  mainAsset = listMainAsset[j];
                XmlElement ele       = xmlDoc.CreateElement("Depend");
                ele.SetAttribute("Id", "" + k);
                ele.SetAttribute("Version", version.ToString());
                ele.SetAttribute("Size", fileSize.ToString("0.00"));
                mainAsset.m_xmlSelfEle.AppendChild(ele);
                mainAsset.m_xmlSelfEle.SetAttribute("TotalSize", (fileSize + Convert.ToDouble(mainAsset.m_xmlSelfEle.GetAttribute("TotalSize"))).ToString("0.00"));
            }
        }

        //打包共享
//		UnityEngine.Object temp1 = AssetDatabase.LoadMainAssetAtPath("Assets/UIResources/Atlases/Common/CommonAtlas.prefab");
//		BuildPipeline.BuildAssetBundle(temp1,null,buildDir + "/Shared/" + "CommonAtlas.asset",BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
//
//		UnityEngine.Object temp4 = AssetDatabase.LoadMainAssetAtPath("Assets/UIResources/Font/DySongTi/songti12.prefab");
//		BuildPipeline.BuildAssetBundle(temp4,null,buildDir + "/Shared/" + "Font12.asset",BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
//
//		UnityEngine.Object temp5 = AssetDatabase.LoadMainAssetAtPath("Assets/UIResources/Font/DySongTi/songti14.prefab");
//		BuildPipeline.BuildAssetBundle(temp5,null,buildDir + "/Shared/" + "Font14.asset",BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
//
//		UnityEngine.Object temp6 = AssetDatabase.LoadMainAssetAtPath("Assets/UIResources/Font/DySongTi/songti16.prefab");
//		BuildPipeline.BuildAssetBundle(temp6,null,buildDir + "/Shared/" + "Font16.asset",BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
//

        // 打包mainAsset
        for (int i = 0; i < mainAssets.Count; i++)
        {
            MainAsset mainAsset = mainAssets[i];
            // todo: 是否会出现mainAsset.m_strFilePath不一样但是strTgtPath一样的情况?

            string strTgtName = "" + mainAsset.m_nId + '_' + mainAsset.m_Version.ToString() + ".asset";
            string strTgtPath = mainAsset.m_strAssetType + "/" + strTgtName;
            if ("Scene" == mainAsset.m_strAssetType)
            {
                BuildPipeline.PushAssetDependencies();
                //string errorScene = BuildPipeline.BuildPlayer(new string[]{ "Assets/" + mainAsset.m_strFilePath }, buildDir  + "/" +  strTgtPath,
                //			EditorUserBuildSettings.activeBuildTarget, BuildOptions.BuildAdditionalStreamedScenes);
                BuildPipeline.PopAssetDependencies();

                //if(!string.IsNullOrEmpty(errorScene))
                //{
                //	Log.Write(LogLevel.ERROR,"{0}",errorScene);
                //	return ;
                //}
            }
            else
            {
                BuildPipeline.PushAssetDependencies();
                bool isSuccess = BuildPipeline.BuildAssetBundle(mainAsset.m_obj, null, buildDir + "/" + strTgtPath,
                                                                BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, EditorUserBuildSettings.activeBuildTarget);
                BuildPipeline.PopAssetDependencies();

                if (!isSuccess)
                {
                    Log.Write(LogLevel.DEBUG, "build {0} failed", strTgtPath);
                    continue;
                }
            }

            // 设置大小观察信息
            FileInfo fileInfo = new FileInfo(buildDir + "/" + strTgtPath);
            double   fileSize = fileInfo.Length / 1024f;
            root.SetAttribute("TotalSize", (fileSize + Convert.ToDouble(root.GetAttribute("TotalSize"))).ToString("0.00"));
            mainAsset.m_xmlSelfEle.SetAttribute("Id", "" + mainAsset.m_nId);
            mainAsset.m_xmlSelfEle.SetAttribute("Size", fileSize.ToString("0.00"));
            mainAsset.m_xmlSelfEle.SetAttribute("Name", mainAsset.OrignalName);

            mainAsset.m_xmlSelfEle.SetAttribute("TotalSize", (fileSize + Convert.ToDouble(mainAsset.m_xmlSelfEle.GetAttribute("TotalSize"))).ToString("0.00"));
            mainAsset.m_xmlParentEle.SetAttribute("TotalSize", (Convert.ToDouble(mainAsset.m_xmlParentEle.GetAttribute("TotalSize"))
                                                                + Convert.ToDouble(mainAsset.m_xmlSelfEle.GetAttribute("TotalSize"))).ToString("0.00"));
        }
        BuildPipeline.PopAssetDependencies();
        //----------------------------------------------------------------------

        xmlDoc.Save(Application.dataPath + "/" + strXmlFile);
        Log.Write(LogLevel.INFO, "pack end");
    }
Esempio n. 3
0
    public static void PackDBConfig()
    {
        string[] resConfigs = new string[] {
            "Other/Config/Resource/TextAsset.txt",
        };

        // 生成MainAsset的List, 并验证配置文件是否合理
        bool             isMissFile = false;
        int              MissCount  = 0;
        List <MainAsset> mainAssets = new List <MainAsset>();

        for (int i = 0; i < resConfigs.Length; i++)
        {
            TextAsset textAsset = AssetDatabase.LoadMainAssetAtPath("Assets/" + resConfigs[i]) as TextAsset;
            if (null == textAsset)
            {
                Log.Write(LogLevel.WARN, "config is null {0}", resConfigs[i]);
                return;
            }

            string  strAssetType = Path.GetFileNameWithoutExtension(resConfigs[i]);
            TabFile tabFile      = new TabFile(textAsset.name, textAsset.text);
            while (tabFile.Next())
            {
                int    nId         = tabFile.Get <int>("id");
                string strFilePath = tabFile.Get <string>("FilePath");
                for (int j = 0; j < mainAssets.Count; j++)
                {
                    MainAsset mainAsset = mainAssets[j];
                    if (mainAsset.m_strFilePath == strFilePath)
                    {
                        Log.Write(LogLevel.WARN, "{0} 同时出现在{1}, {2} 这两个配置中, 放弃本次发布", strFilePath, mainAsset.m_strAssetType, strAssetType);
                        return;
                    }
                    if (mainAsset.m_strAssetType == strAssetType && mainAsset.m_nId == nId)
                    {
                        Log.Write(LogLevel.WARN, "{0} 配置出现了Id重复 {1}, 放弃本次发布", strAssetType, nId);
                        return;
                    }
                }
                UnityEngine.Object fileObj = AssetDatabase.LoadMainAssetAtPath("Assets/" + strFilePath);
                if (null == fileObj)
                {
                    isMissFile = true;
                    Log.Write(LogLevel.WARN, "资源为空: {0}, 放弃本次发布", strFilePath);
                    MissCount++;
                    continue;
                }

                string    fileName = Path.GetFileNameWithoutExtension(strFilePath);
                MainAsset asset    = new MainAsset(strAssetType, nId, strFilePath, fileObj, fileName);
                mainAssets.Add(asset);
            }
        }

        if (isMissFile)
        {
            Log.Write(LogLevel.WARN, "资源为空文件数目{0}, 放弃本次发布", MissCount);
        }

        // 准备XML配置文件
        string      strXmlFile = "Other/Config/Resource/wwwResource.xml";
        XmlDocument xmlDoc     = new XmlDocument();

        xmlDoc.Load(Application.dataPath + "/" + strXmlFile);
        xmlDoc.RemoveAll();
        XmlElement root = xmlDoc.CreateElement("AssetList");

        xmlDoc.AppendChild((XmlNode)root);
        root.SetAttribute("TotalSize", "0");

        SortedList <string, XmlElement> typeXmlEle = new SortedList <string, XmlElement>();

        for (int i = 0; i < mainAssets.Count; i++)
        {
            MainAsset mainAsset = mainAssets[i];
            if (!typeXmlEle.ContainsKey(mainAsset.m_strAssetType))
            {
                XmlElement typeEle = xmlDoc.CreateElement(mainAsset.m_strAssetType);
                typeEle.SetAttribute("TotalSize", "0");
                root.AppendChild(typeEle);
                typeXmlEle.Add(mainAsset.m_strAssetType, typeEle);
            }
            mainAsset.m_xmlParentEle = typeXmlEle[mainAsset.m_strAssetType];
            mainAsset.m_xmlSelfEle   = xmlDoc.CreateElement("Asset");
            mainAsset.m_xmlSelfEle.SetAttribute("Id", "0");
            mainAsset.m_xmlSelfEle.SetAttribute("TotalSize", "0");
            mainAsset.m_xmlParentEle.AppendChild(mainAsset.m_xmlSelfEle);
        }

        BuildPipeline.PushAssetDependencies();

        // 打包mainAsset
        for (int i = 0; i < mainAssets.Count; i++)
        {
            MainAsset mainAsset = mainAssets[i];
            // todo: 是否会出现mainAsset.m_strFilePath不一样但是strTgtPath一样的情况?
            string strTgtName = "" + mainAsset.m_Name + ".asset";
            //string strTgtName = "" + mainAsset.m_nId + ".asset";
            string strTgtPath = mainAsset.m_strAssetType + "/" + strTgtName;
            if ("Scene" == mainAsset.m_strAssetType)
            {
                //string errorScene = BuildPipeline.BuildPlayer(new string[]{ "Assets/" + mainAsset.m_strFilePath }, buildDir  + "/" +  strTgtPath,
                //			EditorUserBuildSettings.activeBuildTarget, BuildOptions.BuildAdditionalStreamedScenes);

                //if(!string.IsNullOrEmpty(errorScene))
                //{
                //	Log.Write(LogLevel.ERROR,"{0}",errorScene);
                //	return ;
                //}
            }
            else
            {
                BuildPipeline.BuildAssetBundle(mainAsset.m_obj, null, buildDir + "/" + strTgtPath,
                                               BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, EditorUserBuildSettings.activeBuildTarget);
            }

            // 设置大小观察信息
            FileInfo fileInfo = new FileInfo(buildDir + "/" + strTgtPath);
            double   fileSize = fileInfo.Length / 1024f;
            root.SetAttribute("TotalSize", (fileSize + Convert.ToDouble(root.GetAttribute("TotalSize"))).ToString("0.00"));
            mainAsset.m_xmlSelfEle.SetAttribute("Id", "" + mainAsset.m_nId);
            mainAsset.m_xmlSelfEle.SetAttribute("TotalSize", (fileSize + Convert.ToDouble(mainAsset.m_xmlSelfEle.GetAttribute("TotalSize"))).ToString("0.00"));
            mainAsset.m_xmlParentEle.SetAttribute("TotalSize", (Convert.ToDouble(mainAsset.m_xmlParentEle.GetAttribute("TotalSize"))
                                                                + Convert.ToDouble(mainAsset.m_xmlSelfEle.GetAttribute("TotalSize"))).ToString("0.00"));
        }
        BuildPipeline.PopAssetDependencies();
        //----------------------------------------------------------------------

        xmlDoc.Save(Application.dataPath + "/" + strXmlFile);
    }