Skip to content

Commit

Permalink
修复develop分支跟随运行指南不能正确打包的问题 (#526)
Browse files Browse the repository at this point in the history
* 修复日志重定向导致的编译报错双击不跳转的问题 (#525)

* 修复打包失败问题

1.HybridCLR的'hotUpdateAssemblies'设置成新的程序集
2.优化运行指南描述
3.打包完后, Init场景的小数值自动变化(还原了下次打包又变回来,直接提交算了)

* 修复初次打开工程的加载报错

add global config null check

* Upgrade Rider package

to 3.0.27

* 优化初次编译说明

add build note

---------

Co-authored-by: ET6 <[email protected]>
  • Loading branch information
EP-Toushirou and 752636090 authored Dec 6, 2023
1 parent 7311a19 commit b151275
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 68 deletions.
43 changes: 21 additions & 22 deletions Book/1.1运行指南.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,45 @@

**使用[Rider2023.2](https://www.jetbrains.com/zh-cn/rider/)**(更新到最新版),需要安装以下内容:
- 安装.Net7,不要用.Net8,目前并不支持.Net8
- 不支持VS,新人用VS搞出各种问题请不要来问我,我也没用过VS,后期搞熟了可以自己改用VS
- 不支持VS,新人用VS搞出各种问题请不要来问我,我也没用过VS,后期搞熟了可以自己改用VS

2. master分支必须使用Unity**2021.3.29**(初学者请在此版本用熟后再切换其他版本)
2. master分支必须使用Unity**2021.3.29**(初学者请在此版本用熟后再切换其他版本)

3. 启动UnityHub,打开(Open) -> 选中'ET/Unity'文件夹所在目录后打开工程
3. 启动UnityHub,打开(Open) -> 选中'ET/Unity'文件夹所在目录后打开工程

4. 打开工程后,点击Unity菜单 -> Edit -> Preferences -> External Tools 在下拉框'**External ScriptEditor**'**Rider**,Generate .csproj files for全部**不要勾选(□)**
4. 打开工程后,点击Unity菜单 -> Edit -> Preferences -> External Tools,点击下拉框'**External ScriptEditor**'选择**Rider**,Generate .csproj files for全部**不要勾选(□)**

5. 点击Unity菜单 -> Assets -> Open C# Project 启动IDE
5. 点击Unity菜单 -> Assets -> Open C# Project 启动IDE

6. 在Project视图中选中'Assets/Resources/GlobalConfig',把AppType选择成'**Demo(状态同步)**'或者'**LockStep(帧同步)**'
6. 在Project视图中选中'Assets/Resources/GlobalConfig',把AppType选择成'**Demo(状态同步)**'或者'**LockStep(帧同步)**'

7. 打开Unity菜单 -> ET -> BuildTool,CodeMode选择**Client Server**,然后**点击ReGenerateProjectFiles**
7. 打开Unity菜单 -> ET -> BuildTool,CodeMode选择**Client Server**,然后**点击ReGenerateProjectFiles**

8. 打开ET.sln,**编译整个ET.sln**,注意要翻墙,否则可能nuget包下载不下来,导致编译出错
8. 打开ET.sln,**编译整个ET.sln**,注意要翻墙,否则可能nuget包下载不下来,导致编译出错(翻墙后如果还有报错解决不了可以尝试先用VS打开ET.sln编译一次后再回到Rider重新编译一次)

9. Unity中双击'Assets/Scenes'目录中的**Init**场景,点击Play(▶)即可运行
9. Unity中双击'Assets/Scenes'目录中的**Init**场景,点击Play(▶)即可运行

10. 帧同步默认是一个人匹配,如果需要多人匹配,修改**LSConstValue.cs**中的**MatchCount**即可
10. 帧同步默认是一个人匹配,如果需要多人匹配,修改**LSConstValue.cs**中的**MatchCount**即可

# 打包过程
1. 点击HybridCLR -> Installer,点击安装,等待安装完成
1. 点击HybridCLR -> Installer,点击安装,等待安装完成

2. 用Rider或VS,编译好Unity.AllHotfix
2. 用Rider编译整个ET.sln工程(翻墙后如果还有报错解决不了可以尝试先用VS打开ET.sln编译一次后再回到Rider重新编译一次)

3. 点击HybridCLR -> Generate -> All
3. 点击HybridCLR -> Generate -> All

4. 点击HybridCLR -> CopyAotDlls,这一步会把需要补充元数据的dll复制到'Assets/Bundles/AotDlls'目录
4. 点击HybridCLR -> CopyAotDlls,这一步会把需要补充元数据的dll复制到'Assets/Bundles/AotDlls'目录

5. 打开YooAsset菜单 -> 点击AssetBundle Builder -> 点击下拉框'Copy Buildin File Option' -> 选择'Clear And Copy All' -> 点击'构建'
5. 打开YooAsset菜单 -> 点击AssetBundle Builder -> 点击下拉框'Copy Buildin File Option' -> 选择'Clear And Copy All' -> 点击'构建'

6. 打开Unity菜单 -> ET -> BuildTool 点击'BuildPackage',Windows下生成的exe在'ET/Release'里面
6. 打开Unity菜单 -> ET -> BuildTool -> EPlayMode选择'HostPlayMode' -> 点击'BuildPackage',Windows下生成的exe在'ET/Release'里面

另:**请自行研究**YooAsset包管理库的使用方式([YooAsset官网](https://www.yooasset.com/)),若需要升级请自行替换'ET/Unity/Packages/YooAsset'
另:**请自行研究**YooAsset包管理库的使用方式([YooAsset官网](https://www.yooasset.com/)),若需要升级请自行替换'ET/Unity/Packages/YooAsset'

# ENABLE_DLL模式
1. 该模式用于**逻辑全热更新**,还有**热重载**,默认就是使用该模式
2. 在Unity菜单 -> ET -> ChangeDefines -> Remove ENABLE_DLL(关闭) 或 Add ENABLE_DLL(开启)
3. 逻辑打成了一个**Model.dll****Hotfix.dll**,开启该模式时游戏运行是通过加载dll运行,但这样Unity Editor将无法引用逻辑代码。所以如果有Editor代码需要用到逻辑代码,可以关闭ENABLE_DLL模式
4. 若需要体验**热重载**,需要在Unity菜单 -> Edit -> Preferences -> General窗口的ScriptChangesWhilePlaying 中 选择 '**RecompileAfterFinishedPlaying**',运行后修改Hotfix代码然后用Rider或VS编译好Unity.AllHotfix工程,再在状态同步Demo中按'R'键
# 热重载
1. 若需要体验此功能,需要在Unity菜单 -> Edit -> Preferences -> General窗口的 'ScriptChangesWhilePlaying' 中 选择 '**RecompileAfterFinishedPlaying**'

2. 运行后修改'Hotfix'或'HotfixView'的代码然后用Rider编译好对应的Hotfix工程,再在状态同步Demo中按'R'键

# 注意事项:

Expand Down
2 changes: 1 addition & 1 deletion Unity/Assets/Scenes/Init.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.12731715, g: 0.13414735, b: 0.121078536, a: 1}
m_IndirectSpecularColor: {r: 0.12731749, g: 0.13414757, b: 0.1210787, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Text;
using System.IO;
using System.Xml;
using UnityEditor;

Expand All @@ -13,8 +11,17 @@ public class OnGenerateCSProjectProcessor: AssetPostprocessor
/// </summary>
public static string OnGeneratedCSProject(string path, string content)
{
BuildType buildType = BuildType.Debug;
CodeMode codeMode = CodeMode.Client;
GlobalConfig globalConfig = GetGlobalConfig();
if (globalConfig.BuildType == BuildType.Release)
// 初次打开工程时会加载失败, 因为此时Unity的资源数据库(AssetDatabase)还未完成初始化
if (globalConfig)
{
buildType = globalConfig.BuildType;
codeMode = globalConfig.CodeMode;
}

if (buildType == BuildType.Release)
{
content = content.Replace("<Optimize>false</Optimize>", "<Optimize>true</Optimize>");
content = content.Replace(";DEBUG;", ";");
Expand Down Expand Up @@ -45,7 +52,7 @@ public static string OnGeneratedCSProject(string path, string content)
if (path.EndsWith("Unity.Model.csproj"))
{
string[] files = { };
switch (globalConfig.CodeMode)
switch (codeMode)
{
case CodeMode.Client:
files = new[]
Expand Down Expand Up @@ -73,7 +80,7 @@ public static string OnGeneratedCSProject(string path, string content)
if (path.EndsWith("Unity.Hotfix.csproj"))
{
string[] files = { };
switch (globalConfig.CodeMode)
switch (codeMode)
{
case CodeMode.Client:
files = new[]
Expand Down Expand Up @@ -115,6 +122,9 @@ public static string OnGeneratedCSProject(string path, string content)
return content;
}

/// <summary>
/// 编译dll文件后额外复制的目录配置
/// </summary>
private static string AddCopyAfterBuild(string content)
{
content = content.Replace("<Target Name=\"AfterBuild\" />",
Expand All @@ -127,28 +137,6 @@ private static string AddCopyAfterBuild(string content)
return content;
}

private static string RemoveRef(string content)
{
string[] lines = content.Split('\n');
StringBuilder sb = new StringBuilder();
foreach (string line in lines)
{
if (line.Contains("<Compile Include="))
{
continue;
}

if (line.Contains("<Folder Include="))
{
continue;
}

sb.AppendLine(line);
}

return sb.ToString();
}

/// <summary>
/// 获取全局配置
/// </summary>
Expand All @@ -160,6 +148,7 @@ private static GlobalConfig GetGlobalConfig()
/// <summary>
/// 自定义C#项目配置
/// 参考链接:
/// https://zhuanlan.zhihu.com/p/509046784
/// https://learn.microsoft.com/zh-cn/visualstudio/ide/reference/build-events-page-project-designer-csharp?view=vs-2022
/// https://learn.microsoft.com/zh-cn/visualstudio/ide/how-to-specify-build-events-csharp?view=vs-2022
/// </summary>
Expand Down
35 changes: 24 additions & 11 deletions Unity/Assets/Scripts/Editor/LogRedirection/LogRedirection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ private static bool OnOpenAsset(int instanceID, int line)
if (!string.IsNullOrEmpty(stackTrace))
{
// 使用正则表达式匹配at的哪个脚本的哪一行
var matches = Regex.Match(stackTrace, @"\(at (.+)\)",
Match logMatches = Regex.Match(stackTrace, @"\(at (.+)\)",
RegexOptions.IgnoreCase);
while (matches.Success)
if (!logMatches.Success)
{
var pathLine = matches.Groups[1].Value;
Match compileErrorMatch = Regex.Match(stackTrace, @"(.*?)\(([0-9]+),([0-9]+)\): error");
if (compileErrorMatch.Success)
{
OpenIDE(compileErrorMatch.Groups[1].Value, Convert.ToInt32(compileErrorMatch.Groups[2].Value), Convert.ToInt32(compileErrorMatch.Groups[3].Value));
}
}
while (logMatches.Success)
{
var pathLine = logMatches.Groups[1].Value;

if (!pathLine.Contains("Log.cs") &&
!pathLine.Contains("UnityLogger.cs") &&
Expand All @@ -38,17 +46,11 @@ private static bool OnOpenAsset(int instanceID, int line)
var path = pathLine.Substring(0, splitIndex);
// 行号
line = Convert.ToInt32(pathLine.Substring(splitIndex + 1));
var fullPath = UnityEngine.Application.dataPath.Substring(0, UnityEngine.Application.dataPath.LastIndexOf("Assets", StringComparison.Ordinal));
fullPath = $"{fullPath}{path}";
#if UNITY_STANDALONE_WIN
fullPath = fullPath.Replace('/', '\\');
#endif
// 跳转到目标代码的特定行
InternalEditorUtility.OpenFileAtLineExternal(fullPath, line);
OpenIDE(path, line);
break;
}

matches = matches.NextMatch();
logMatches = logMatches.NextMatch();
}

return true;
Expand All @@ -57,6 +59,17 @@ private static bool OnOpenAsset(int instanceID, int line)
return false;
}

private static void OpenIDE(string path, int line, int column = 0)
{
var fullPath = UnityEngine.Application.dataPath.Substring(0, UnityEngine.Application.dataPath.LastIndexOf("Assets", StringComparison.Ordinal));
fullPath = $"{fullPath}{path}";
#if UNITY_STANDALONE_WIN
fullPath = fullPath.Replace('/', '\\');
#endif
// 跳转到目标代码的特定行
InternalEditorUtility.OpenFileAtLineExternal(fullPath, line, column);
}

/// <summary>
/// 获取当前日志窗口选中的日志的堆栈信息。
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public class AOTGenericReferences : UnityEngine.MonoBehaviour
// MemoryPack.Formatters.ListFormatter<long>
// MemoryPack.Formatters.ListFormatter<object>
// MemoryPack.IMemoryPackFormatter<Unity.Mathematics.float3>
// MemoryPack.IMemoryPackFormatter<byte>
// MemoryPack.IMemoryPackFormatter<long>
// MemoryPack.IMemoryPackFormatter<object>
// MemoryPack.IMemoryPackable<ET.LSInput>
Expand Down Expand Up @@ -562,13 +563,17 @@ public void RefMethods()
// System.Void MemoryPack.Formatters.ListFormatter.SerializePackable<object>(MemoryPack.MemoryPackWriter&,System.Collections.Generic.List<object>&)
// byte[] MemoryPack.Internal.MemoryMarshalEx.AllocateUninitializedArray<byte>(int,bool)
// byte& MemoryPack.Internal.MemoryMarshalEx.GetArrayDataReference<byte>(byte[])
// MemoryPack.MemoryPackFormatter<byte> MemoryPack.MemoryPackFormatterProvider.GetFormatter<byte>()
// MemoryPack.MemoryPackFormatter<long> MemoryPack.MemoryPackFormatterProvider.GetFormatter<long>()
// MemoryPack.MemoryPackFormatter<object> MemoryPack.MemoryPackFormatterProvider.GetFormatter<object>()
// bool MemoryPack.MemoryPackFormatterProvider.IsRegistered<ET.LSInput>()
// bool MemoryPack.MemoryPackFormatterProvider.IsRegistered<object>()
// System.Void MemoryPack.MemoryPackFormatterProvider.Register<ET.LSInput>(MemoryPack.MemoryPackFormatter<ET.LSInput>)
// System.Void MemoryPack.MemoryPackFormatterProvider.Register<object>(MemoryPack.MemoryPackFormatter<object>)
// System.Void MemoryPack.MemoryPackReader.DangerousReadUnmanagedArray<byte>(byte[]&)
// byte[] MemoryPack.MemoryPackReader.DangerousReadUnmanagedArray<byte>()
// MemoryPack.IMemoryPackFormatter<byte> MemoryPack.MemoryPackReader.GetFormatter<byte>()
// MemoryPack.IMemoryPackFormatter<long> MemoryPack.MemoryPackReader.GetFormatter<long>()
// MemoryPack.IMemoryPackFormatter<object> MemoryPack.MemoryPackReader.GetFormatter<object>()
// System.Void MemoryPack.MemoryPackReader.ReadPackable<object>(object&)
// object MemoryPack.MemoryPackReader.ReadPackable<object>()
Expand All @@ -594,19 +599,27 @@ public void RefMethods()
// System.Void MemoryPack.MemoryPackReader.ReadUnmanaged<byte,uint>(byte&,uint&)
// System.Void MemoryPack.MemoryPackReader.ReadUnmanaged<byte>(byte&)
// System.Void MemoryPack.MemoryPackReader.ReadUnmanaged<int>(int&)
// System.Void MemoryPack.MemoryPackReader.ReadUnmanaged<long,ET.LSInput>(long&,ET.LSInput&)
// System.Void MemoryPack.MemoryPackReader.ReadUnmanaged<long,TrueSync.TSVector,TrueSync.TSQuaternion>(long&,TrueSync.TSVector&,TrueSync.TSQuaternion&)
// System.Void MemoryPack.MemoryPackReader.ReadUnmanaged<long,long>(long&,long&)
// System.Void MemoryPack.MemoryPackReader.ReadUnmanaged<long>(long&)
// System.Void MemoryPack.MemoryPackReader.ReadUnmanaged<uint>(uint&)
// System.Void MemoryPack.MemoryPackReader.ReadUnmanagedArray<byte>(byte[]&)
// byte[] MemoryPack.MemoryPackReader.ReadUnmanagedArray<byte>()
// System.Void MemoryPack.MemoryPackReader.ReadValue<object>(object&)
// byte MemoryPack.MemoryPackReader.ReadValue<byte>()
// long MemoryPack.MemoryPackReader.ReadValue<long>()
// object MemoryPack.MemoryPackReader.ReadValue<object>()
// System.Void MemoryPack.MemoryPackWriter.DangerousWriteUnmanagedArray<byte>(byte[])
// MemoryPack.IMemoryPackFormatter<byte> MemoryPack.MemoryPackWriter.GetFormatter<byte>()
// MemoryPack.IMemoryPackFormatter<long> MemoryPack.MemoryPackWriter.GetFormatter<long>()
// MemoryPack.IMemoryPackFormatter<object> MemoryPack.MemoryPackWriter.GetFormatter<object>()
// System.Void MemoryPack.MemoryPackWriter.WritePackable<object>(object&)
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanaged<ET.LSInput>(ET.LSInput&)
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanaged<Unity.Mathematics.quaternion,int>(Unity.Mathematics.quaternion&,int&)
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanaged<int>(int&)
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanaged<long,ET.LSInput>(long&,ET.LSInput&)
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanaged<long,TrueSync.TSVector,TrueSync.TSQuaternion>(long&,TrueSync.TSVector&,TrueSync.TSQuaternion&)
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanaged<long,long>(long&,long&)
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanaged<long>(long&)
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanagedArray<byte>(byte[])
Expand All @@ -624,6 +637,8 @@ public void RefMethods()
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanagedWithObjectHeader<byte,long>(byte,byte&,long&)
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanagedWithObjectHeader<byte,uint>(byte,byte&,uint&)
// System.Void MemoryPack.MemoryPackWriter.WriteUnmanagedWithObjectHeader<byte>(byte,byte&)
// System.Void MemoryPack.MemoryPackWriter.WriteValue<byte>(byte&)
// System.Void MemoryPack.MemoryPackWriter.WriteValue<long>(long&)
// System.Void MemoryPack.MemoryPackWriter.WriteValue<object>(object&)
// object MongoDB.Bson.Serialization.BsonSerializer.Deserialize<object>(MongoDB.Bson.IO.IBsonReader,System.Action<MongoDB.Bson.Serialization.BsonDeserializationContext.Builder>)
// object MongoDB.Bson.Serialization.BsonSerializer.Deserialize<object>(string,System.Action<MongoDB.Bson.Serialization.BsonDeserializationContext.Builder>)
Expand All @@ -640,6 +655,9 @@ public void RefMethods()
// System.Span<byte> System.MemoryExtensions.AsSpan<byte>(byte[])
// byte& System.Runtime.CompilerServices.Unsafe.Add<byte>(byte&,int)
// byte& System.Runtime.CompilerServices.Unsafe.As<byte,byte>(byte&)
// object& System.Runtime.CompilerServices.Unsafe.As<object,object>(object&)
// byte& System.Runtime.CompilerServices.Unsafe.AsRef<byte>(byte&)
// long& System.Runtime.CompilerServices.Unsafe.AsRef<long>(long&)
// object& System.Runtime.CompilerServices.Unsafe.AsRef<object>(object&)
// ET.ActorId System.Runtime.CompilerServices.Unsafe.ReadUnaligned<ET.ActorId>(byte&)
// ET.LSInput System.Runtime.CompilerServices.Unsafe.ReadUnaligned<ET.LSInput>(byte&)
Expand Down
2 changes: 1 addition & 1 deletion Unity/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"com.code-philosophy.hybridclr": "https://gitee.com/focus-creative-games/hybridclr_unity.git",
"com.cysharp.memorypack": "https://github.com/Cysharp/MemoryPack.git?path=src/MemoryPack.Unity/Assets/Plugins/MemoryPack",
"com.gwiazdorrr.betterstreamingassets": "https://github.com/gwiazdorrr/BetterStreamingAssets.git",
"com.unity.ide.rider": "3.0.26",
"com.unity.ide.rider": "3.0.27",
"com.unity.ide.visualstudio": "2.0.22",
"com.unity.ide.vscode": "1.2.5",
"com.unity.render-pipelines.universal": "12.1.12",
Expand Down
2 changes: 1 addition & 1 deletion Unity/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"url": "https://packages.unity.com"
},
"com.unity.ide.rider": {
"version": "3.0.26",
"version": "3.0.27",
"depth": 0,
"source": "registry",
"dependencies": {
Expand Down
8 changes: 4 additions & 4 deletions Unity/ProjectSettings/HybridCLRSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ MonoBehaviour:
il2cppPlusRepoURL: https://gitee.com/focus-creative-games/il2cpp_plus
hotUpdateAssemblyDefinitions: []
hotUpdateAssemblies:
- Model
- Hotfix
- Unity.AllHotfix
- Unity.AllModel
- Unity.Model
- Unity.Hotfix
- Unity.ModelView
- Unity.HotfixView
preserveHotUpdateAssemblies: []
hotUpdateDllCompileOutputRootDir: HybridCLRData/HotUpdateDlls
externalHotUpdateAssembliyDirs:
Expand Down

0 comments on commit b151275

Please sign in to comment.