/// <summary>
        /// 导入资源到Mod时触发的事件
        /// </summary>
        static void ImportAsset(string ModName, UnityEngine.Object asset)
        {
            //如果该资源名称未处理过,先处理名称,然后让它在下一轮Import事件处理(重命名会重新Import)
            if (asset.GetAssetModNameEditor() != ModName)
            {
                AddModName(ModName, asset);
                return;
            }
            AssetIndexer ai = ModsEditor.GetAssetIndexer(ModName);
            //检测该资源是否已经存在
            int assetindex = ai.TryGet(AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(asset)));

            if (assetindex == -1)
            {
                ai.Add(asset.name, CreatAssetInfo(asset));
            }
            else
            {
                if (ai.NameList[assetindex] == asset.name)
                {
                    return;//没被重命名就无视
                }
                ai.Rename(ai.NameList[assetindex], asset.name);
            }
        }
示例#2
0
        public static AssetBundleBuild GetAllAssets(ModInfo info, string platform)
        {
            AssetIndexer     ai  = ModsEditor.GetAssetIndexer(info.InternalName);
            AssetBundleBuild abb = new AssetBundleBuild();

            abb.assetBundleName  = info.PackageName + "." + platform;
            abb.addressableNames = new string[ai.InfoList.Count + 3];
            abb.assetNames       = new string[ai.InfoList.Count + 3];
            int i = 0;

            for (i = 0; i < ai.InfoList.Count; i++)
            {
                abb.addressableNames[i] = ai.InfoList[i].FullName;
                abb.assetNames[i]       = AssetDatabase.GUIDToAssetPath(ai[i].AssetId.ToString());
            }
            //添加包信息内容
            abb.addressableNames[i] = "ModInfo";
            abb.assetNames[i++]     = ModsEditor.GetModInfoPath(info.InternalName);

            abb.addressableNames[i] = "AssetIndexer";
            abb.assetNames[i++]     = ModsEditor.GetAssetIndexerPath(info.InternalName);

            abb.addressableNames[i] = "LanguageDic";
            abb.assetNames[i++]     = ModsEditor.GetLanguageDicPath(info.InternalName);
            return(abb);
        }
示例#3
0
 private void OnGUI()
 {
     TagModName = EditorGUILayout.TextField("Tag's Mod Name", TagModName);
     TagName    = EditorGUILayout.TextField("Tag Name", TagName);
     if (GUILayout.Button("Add"))
     {
         target.Tags.Add(TagModName, TagName);
         EditorUtility.SetDirty(ModsEditor.GetAssetIndexer(target.ModName));
         AssetDatabase.SaveAssets();
         Close();
     }
 }
示例#4
0
        public static void drawAssetInfoGUI(Editor editor)
        {
            //如果目标对象不是在磁盘中的资源而是场景中的对象,就不进行任何操作
            if (!EditorUtility.IsPersistent(editor.target))
            {
                return;
            }
            //获得资源所在Mod
            var ModName = editor.target.GetAssetModNameEditor();

            //如果没匹配到,就代表该资源不在某个Mod的Assets文件夹下,忽略
            if (ModName == null || !ModsEditor.Exist(ModName))
            {
                return;
            }
            AssetInfo ai = ModsEditor.GetAssetInfo(ModName, editor.target.name);
            //赋值
            string newAssetName = EditorGUILayout.TextField("AssetName", ai.AssetName);

            if (newAssetName != ai.AssetName)
            {
                ai.AssetName = newAssetName;
                AssetDatabase.RenameAsset(AssetDatabase.GetAssetOrScenePath(editor.target), ai.ModName + "." + ai.AssetName);
            }
            GUILayout.Label("Tags:");
            GUILayout.BeginVertical();
            for (int i = 0; i < ai.Tags.Count; i++)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label(ai.Tags[i]);
                if (GUILayout.Button("Remove"))
                {
                    ai.Tags.RemoveAt(i);
                    EditorUtility.SetDirty(ModsEditor.GetAssetIndexer(ai.ModName));
                    AssetDatabase.SaveAssets();
                }
                GUILayout.EndHorizontal();
            }
            if (GUILayout.Button("Add Tag"))
            {
                AddTagWindow window = EditorWindow.GetWindow <AddTagWindow>();
                window.target = ai;
                window.Show();
            }
            GUILayout.EndVertical();
        }
        /// <summary>
        /// 用于重命名在Mod中的Asset
        /// </summary>
        /// <returns>经过处理后的名称</returns>
        static string RenameAsset(string NewAssetFullName, UnityEngine.Object asset)
        {
            AssetIndexer ai = ModsEditor.GetAssetIndexer(AssetUtility.GetModName(NewAssetFullName));

            if (ai.Contains(NewAssetFullName) != -1)
            {
                int i = 1;
                while (ai.Contains(NewAssetFullName + i) != -1)
                {
                    i++;
                }
                NewAssetFullName = NewAssetFullName + i;
                Debug.LogError("资源名字重复!资源名:" + asset.name + "。已改名为" + NewAssetFullName);
                AssetDatabase.RenameAsset(AssetDatabase.GetAssetOrScenePath(asset), NewAssetFullName);
            }
            else
            {
                AssetDatabase.RenameAsset(AssetDatabase.GetAssetOrScenePath(asset), NewAssetFullName);
            }
            return(NewAssetFullName);
        }
示例#6
0
        public static void Build()
        {
            Clear();
            var Mods   = ModsEditor.GetMods();
            int length = Mods.Length;

            for (int i = 0; i < length; i++)
            {
                var ModInfo         = ModsEditor.GetModInfo(Mods[i]);
                var ModOutPutFolder = OutPutFolder + "/" + ModInfo.PackageName;
                Directory.CreateDirectory(ModOutPutFolder);

                //生成ModInfo文件
                using (StreamWriter sw = File.CreateText(ModOutPutFolder + "/" + ModInfo.PackageName + ".json"))
                {
                    sw.Write(JsonConvert.SerializeObject(ModInfo));
                }

                //Windows64版本构建
                //构建参数
                var param = new BundleBuildParameters(BuildTarget.StandaloneWindows64, BuildTargetGroup.Standalone, ModOutPutFolder);
                param.BundleCompression = BuildCompression.LZ4;
                //填入资源
                var content = new BundleBuildContent(new AssetBundleBuild[] { GetAllAssets(ModInfo, "winmod") });
                IBundleBuildResults results;
                //构建包
                ReturnCode code = ContentPipeline.BuildAssetBundles(param, content, out results);
                if (code != ReturnCode.Success)
                {
                    if (code == ReturnCode.Canceled)
                    {
                        return;//如果取消,直接返回
                    }
                    Debug.LogError("构建失败!错误原因:" + code.ToString());
                }
            }
        }
示例#7
0
        public static void BuildAll()
        {
            Clear();
            var Mods   = ModsEditor.GetMods();
            int length = Mods.Length;

            for (int i = 0; i < length; i++)
            {
                var ModInfo         = ModsEditor.GetModInfo(Mods[i]);
                var ModOutPutFolder = OutPutFolder + "/" + ModInfo.PackageName;
                //Windows64版本构建
                //构建参数
                var param = new BundleBuildParameters(BuildTarget.StandaloneWindows64, BuildTargetGroup.Standalone, ModOutPutFolder);
                param.BundleCompression = BuildCompression.LZ4;
                //填入资源
                var content = new BundleBuildContent(new AssetBundleBuild[] { GetAllAssets(ModInfo, "winmod") });
                IBundleBuildResults results;
                //构建包
                ReturnCode code = ContentPipeline.BuildAssetBundles(param, content, out results);
                if (code != ReturnCode.Success)
                {
                    if (code == ReturnCode.Canceled)
                    {
                        return;//如果取消,直接返回
                    }
                    Debug.LogError("构建失败!错误原因:" + code.ToString());
                }

                //OSX版本构建
                //构建参数
                param.Target = BuildTarget.StandaloneOSX;
                //填入资源
                content = new BundleBuildContent(new AssetBundleBuild[] { GetAllAssets(ModInfo, "osxmod") });
                results = null;
                //构建包
                code = ContentPipeline.BuildAssetBundles(param, content, out results);
                if (code != ReturnCode.Success)
                {
                    if (code == ReturnCode.Canceled)
                    {
                        return;//如果取消,直接返回
                    }
                    Debug.LogError("构建失败!错误原因:" + code.ToString());
                }

                //Linux版本构建
                //构建参数
                param.Target = BuildTarget.StandaloneLinux64;
                //填入资源
                content = new BundleBuildContent(new AssetBundleBuild[] { GetAllAssets(ModInfo, "linuxmod") });
                results = null;
                //构建包
                code = ContentPipeline.BuildAssetBundles(param, content, out results);
                if (code != ReturnCode.Success)
                {
                    if (code == ReturnCode.Canceled)
                    {
                        return;//如果取消,直接返回
                    }
                    Debug.LogError("构建失败!错误原因:" + code.ToString());
                }

                //Android版本构建
                //构建参数
                param.Target = BuildTarget.Android;
                param.Group  = BuildTargetGroup.Android;
                //填入资源
                content = new BundleBuildContent(new AssetBundleBuild[] { GetAllAssets(ModInfo, "androidmod") });
                results = null;
                //构建包
                code = ContentPipeline.BuildAssetBundles(param, content, out results);
                if (code != ReturnCode.Success)
                {
                    if (code == ReturnCode.Canceled)
                    {
                        return;//如果取消,直接返回
                    }
                    Debug.LogError("构建失败!错误原因:" + code.ToString());
                }

                //ios版本构建
                //构建参数
                param.Target = BuildTarget.iOS;
                param.Group  = BuildTargetGroup.iOS;
                //填入资源
                content = new BundleBuildContent(new AssetBundleBuild[] { GetAllAssets(ModInfo, "iosmod") });
                results = null;
                //构建包
                code = ContentPipeline.BuildAssetBundles(param, content, out results);
                if (code != ReturnCode.Success)
                {
                    if (code == ReturnCode.Canceled)
                    {
                        return;//如果取消,直接返回
                    }
                    Debug.LogError("构建失败!错误原因:" + code.ToString());
                }

                //生成ModInfo文件
                ModInfo = ModsEditor.GetModInfo(Mods[i]);//由于构建后资源会被释放,这里需要重载
                using (FileStream fs = File.Create(ModOutPutFolder + "/" + ModInfo.PackageName + ".json"))
                {
                    StreamWriter sw = new StreamWriter(fs);
                    sw.Write(JsonConvert.SerializeObject(ModInfo));
                    sw.Dispose();
                }
            }
        }
示例#8
0
 /// <summary>
 /// 获得资源所属Mod的Guid
 /// </summary>
 public static string GetModIdEditor <T>(this T asset) where T : UnityEngine.Object
 {
     return(ModsEditor.GetModId(asset.GetAssetModNameEditor()));
 }
示例#9
0
 /// <summary>
 /// 获得资源的信息(请不要在一个不是Mod资源的对象上调用此函数)
 /// </summary>
 public static AssetInfo GetAssetInfoEditor <T>(this T asset) where T : UnityEngine.Object
 {
     return(ModsEditor.GetAssetInfo(asset.GetAssetModNameEditor(), asset.GetAssetNameEditor()));
 }
 /// <summary>
 /// 创建AssetInfo
 /// </summary>
 /// <returns></returns>
 public static AssetInfo CreatAssetInfo(UnityEngine.Object asset)
 {
     return(new AssetInfo(asset.GetAssetModNameEditor(), asset.GetAssetNameEditor(),
                          ModsEditor.GetModId(asset.GetAssetModNameEditor()),
                          AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(asset)), asset.GetType()));
 }
        /// <summary>
        /// 从Mod中移除资源时触发的事件
        /// </summary>
        static void DeleteAsset(string ModName, string AssetName)
        {
            AssetIndexer ai = ModsEditor.GetAssetIndexer(ModName);

            ai?.Remove(ModName + "." + AssetName);
        }
 static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetsPaths)
 {
     repeat += 1;
     if (repeat > 100)
     {
         return;
     }
     //先处理导入的资源
     for (int i = 0; i < importedAssets.Length; i++)
     {
         var asset   = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(importedAssets[i]);
         var ModName = ModsEditor.GetModNameByPath(importedAssets[i]);
         //如果该资源不属于任何一个Mod或者是文件夹,无视
         if (ModName == null || AssetDatabase.IsValidFolder(importedAssets[i]))
         {
             continue;
         }
         ImportAsset(ModName, asset);
     }
     //再处理删除的资源
     for (int i = 0; i < deletedAssets.Length; i++)
     {
         var ModName = ModsEditor.GetModNameByPath(deletedAssets[i]);
         //如果该资源不属于任何一个Mod或者是文件夹,无视
         if (ModName == null || AssetDatabase.IsValidFolder(deletedAssets[i]))
         {
             continue;
         }
         var AssetName = ModsEditor.GetAssetNameByPath(deletedAssets[i]);
         DeleteAsset(ModName, AssetName);
     }
     //最后处理移动的资源
     for (int i = 0; i < movedAssets.Length; i++)
     {
         var asset     = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(movedAssets[i]);
         var FromMod   = ModsEditor.GetModNameByPath(movedFromAssetsPaths[i]);
         var ToMod     = ModsEditor.GetModNameByPath(movedAssets[i]);
         var assetName = asset.GetAssetNameEditor();
         //如果没有移动到另一个Mod或移动的是文件夹,无视(都为null,即并非Mod之间的资源移动,也无视)
         if (FromMod == ToMod || AssetDatabase.IsValidFolder(movedAssets[i]))
         {
             continue;
         }
         if (FromMod == null)
         {
             ImportAsset(ToMod, asset);
             continue;
         }
         if (ToMod == null)
         {
             AssetDatabase.RenameAsset(movedAssets[i], assetName);
             DeleteAsset(FromMod, assetName);
             continue;
         }
         var    FromModAI   = ModsEditor.GetAssetIndexer(FromMod);
         string newFullName = ToMod + "." + assetName;
         //将AssetInfo转移出来
         var Info = FromModAI[asset.name];
         FromModAI.Remove(asset.name);
         //更改Mod名
         asset.name  = assetName;
         newFullName = RenameAsset(newFullName, asset);
         //再将AssetInfo转移到新Mod
         Info.ModName   = ToMod;
         Info.ModId     = ModsEditor.GetModId(ToMod);
         Info.AssetName = AssetUtility.GetAssetName(newFullName);//如果发生了重名事件可以修改成新名称
         ModsEditor.GetAssetIndexer(ToMod).Add(newFullName, Info);
     }
     AssetDatabase.SaveAssets();
     AssetDatabase.Refresh();
 }