فهرست منبع

1:添加ide编译dll功能2:修复打包运行dll未加载的问题 (#545)

* 1:添加ide编译dll功能
2:修复打包运行dll未加载的问题

* 屏蔽初次打开Unity加载GlobalConfig报错
XuWei 2 سال پیش
والد
کامیت
6cd38644df

+ 35 - 2
Unity/Assets/Scripts/Editor/AssetPostProcessor/OnGenerateCSProjectProcessor.cs

@@ -12,9 +12,23 @@ namespace ET
         /// </summary>
         public static string OnGeneratedCSProject(string path, string content)
         {
-            //刷新一下CodeMode,防止某些本地修改CodeMode测试后,使用svn更新,被改名的Ignore.asmdef被还原的问题
+            BuildType buildType = BuildType.Debug;
+            CodeMode codeMode = CodeMode.Client;
             GlobalConfig globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
-            AssemblyTool.RefreshCodeMode(globalConfig.CodeMode);
+            // 初次打开工程时会加载失败, 因为此时Unity的资源数据库(AssetDatabase)还未完成初始化
+            if (globalConfig)
+            {
+                buildType = globalConfig.BuildType;
+                codeMode = globalConfig.CodeMode;
+            }
+            //刷新一下CodeMode,防止某些本地修改CodeMode测试后,使用svn更新,被改名的Ignore.asmdef被还原的问题
+            AssemblyTool.RefreshCodeMode(codeMode);
+
+            if (buildType == BuildType.Release)
+            {
+                content = content.Replace("<Optimize>false</Optimize>", "<Optimize>true</Optimize>");
+                content = content.Replace(";DEBUG;", ";");
+            }
 
             if (path.EndsWith("Unity.Core.csproj"))
             {
@@ -24,26 +38,45 @@ namespace ET
             if (path.EndsWith("Unity.ModelView.csproj"))
             {
                 content = GenerateCustomProject(content);
+                content = AddCopyAfterBuild(content);
             }
 
             if (path.EndsWith("Unity.HotfixView.csproj"))
             {
                 content = GenerateCustomProject(content);
+                content = AddCopyAfterBuild(content);
             }
 
             if (path.EndsWith("Unity.Model.csproj"))
             {
                 content = GenerateCustomProject(content);
+                content = AddCopyAfterBuild(content);
             }
 
             if (path.EndsWith("Unity.Hotfix.csproj"))
             {
                 content = GenerateCustomProject(content);
+                content = AddCopyAfterBuild(content);
             }
 
             return content;
         }
 
+        /// <summary>
+        /// 编译dll文件后额外复制的目录配置
+        /// </summary>
+        private static string AddCopyAfterBuild(string content)
+        {
+            content = content.Replace("<Target Name=\"AfterBuild\" />",
+                "<Target Name=\"PostBuild\" AfterTargets=\"PostBuildEvent\">\n" +
+                $"    <Copy SourceFiles=\"$(TargetDir)/$(TargetName).dll\" DestinationFiles=\"$(ProjectDir)/{Define.CodeDir}/$(TargetName).dll.bytes\" ContinueOnError=\"false\" />\n" +
+                $"    <Copy SourceFiles=\"$(TargetDir)/$(TargetName).pdb\" DestinationFiles=\"$(ProjectDir)/{Define.CodeDir}/$(TargetName).pdb.bytes\" ContinueOnError=\"false\" />\n" +
+                $"    <Copy SourceFiles=\"$(TargetDir)/$(TargetName).dll\" DestinationFiles=\"$(ProjectDir)/{Define.BuildOutputDir}/$(TargetName).dll\" ContinueOnError=\"false\" />\n" +
+                $"    <Copy SourceFiles=\"$(TargetDir)/$(TargetName).pdb\" DestinationFiles=\"$(ProjectDir)/{Define.BuildOutputDir}/$(TargetName).pdb\" ContinueOnError=\"false\" />\n" +
+                "  </Target>\n");
+            return content;
+        }
+
         /// <summary>
         /// 自定义C#项目配置
         /// 参考链接:

+ 1 - 0
Unity/Assets/Scripts/Editor/BuildEditor/BuildEditor.cs

@@ -62,6 +62,7 @@ namespace ET
                 this.globalConfig.BuildType = codeOptimization;
                 EditorUtility.SetDirty(this.globalConfig);
                 AssetDatabase.SaveAssets();
+                BuildHelper.ReGenerateProjectFiles();
             }
 
             EditorGUILayout.LabelField("BuildAssetBundleOptions ");

+ 8 - 0
Unity/Assets/Scripts/Editor/GlobalConfigEditor/GlobalConfigEditor.cs

@@ -6,11 +6,13 @@ namespace ET
     public class GlobalConfigEditor : Editor
     {
         private CodeMode m_CurCodeMode;
+        private BuildType m_CurBuildType;
 
         private void OnEnable()
         {
             GlobalConfig globalConfig = (GlobalConfig)this.target;
             m_CurCodeMode = globalConfig.CodeMode;
+            m_CurBuildType = globalConfig.BuildType;
         }
 
         public override void OnInspectorGUI()
@@ -23,6 +25,12 @@ namespace ET
                 this.serializedObject.Update();
                 AssemblyTool.RefreshCodeMode(globalConfig.CodeMode);
             }
+
+            if (m_CurBuildType != globalConfig.BuildType)
+            {
+                m_CurBuildType = globalConfig.BuildType;
+                BuildHelper.ReGenerateProjectFiles();
+            }
         }
     }
 }

+ 10 - 12
Unity/Assets/Scripts/Loader/CodeLoader.cs

@@ -32,16 +32,12 @@ namespace ET
 
         public void Start()
         {
-            byte[] modelAssBytes;
-            byte[] modelPdbBytes;
-            byte[] modelViewAssBytes;
-            byte[] modelViewPdbBytes;
             if (!Define.IsEditor)
             {
-                modelAssBytes = this.dlls["Unity.Model.dll"].bytes;
-                modelPdbBytes = this.dlls["Unity.Model.pdb"].bytes;
-                modelViewAssBytes = this.dlls["Unity.ModelView.dll"].bytes;
-                modelViewPdbBytes = this.dlls["Unity.ModelView.pdb"].bytes;
+                byte[] modelAssBytes = this.dlls["Unity.Model.dll"].bytes;
+                byte[] modelPdbBytes = this.dlls["Unity.Model.pdb"].bytes;
+                byte[] modelViewAssBytes = this.dlls["Unity.ModelView.dll"].bytes;
+                byte[] modelViewPdbBytes = this.dlls["Unity.ModelView.pdb"].bytes;
                 // 如果需要测试,可替换成下面注释的代码直接加载Assets/Bundles/Code/Unity.Model.dll.bytes,但真正打包时必须使用上面的代码
                 //modelAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Unity.Model.dll.bytes"));
                 //modelPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Unity.Model.pdb.bytes"));
@@ -56,15 +52,17 @@ namespace ET
                         RuntimeApi.LoadMetadataForAOTAssembly(textAsset.bytes, HomologousImageMode.SuperSet);
                     }
                 }
+                this.modelAssembly = Assembly.Load(modelAssBytes, modelPdbBytes);
+                this.modelViewAssembly = Assembly.Load(modelViewAssBytes, modelViewPdbBytes);
             }
             else
             {
                 if (this.enableDll)
                 {
-                    modelAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Unity.Model.dll.bytes"));
-                    modelPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Unity.Model.pdb.bytes"));
-                    modelViewAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Unity.ModelView.dll.bytes"));
-                    modelViewPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Unity.ModelView.pdb.bytes"));
+                    byte[] modelAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Unity.Model.dll.bytes"));
+                    byte[] modelPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Unity.Model.pdb.bytes"));
+                    byte[] modelViewAssBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Unity.ModelView.dll.bytes"));
+                    byte[] modelViewPdbBytes = File.ReadAllBytes(Path.Combine(Define.CodeDir, "Unity.ModelView.pdb.bytes"));
                     this.modelAssembly = Assembly.Load(modelAssBytes, modelPdbBytes);
                     this.modelViewAssembly = Assembly.Load(modelViewAssBytes, modelViewPdbBytes);
                 }