- https://www.gamereplays.org/overwatch/portals.php?show=page&name=overwatch-a-guide-to-understanding-netcode
- https://zhuanlan.zhihu.com/p/141555559
- https://www.bilibili.com/video/BV1ox411s7Fd?t=1043
- https://github.com/QXSoftware/qxsoftware.github.io/blob/e9061b0c72c3d0ad53dd924bded5cd39751e0875/_posts/2020-05-31-Game-Netcode.MD
字典GC
当枚举和结构体为字典索引时,调用Contain
方法是会拆箱装箱,则会出现GC。解决方案:
- 把枚举转成int类型
- 结构体继承IEquatable接口,然后在创建一个继承IEqualityComparer的类用来对结构体作比较。把比较类实例化一个全局变量,在字典初始化时把全局比较工具对象传入字典即可。
https://blog.csdn.net/qq_36576410/article/details/87909947
https://answer.uwa4d.com/question/59f716c0727b4a5d10c6dfef
https://stackoverflow.com/questions/50303424/why-is-dictionary-containskey-tostring-causing-gc-alloc
https://forum.unity.com/threads/solved-question-about-dictionary-any-vs-dictionary-containskey.589939/
Obtaining components using the fastest mehod
最好的方式获得组件:
GetComponent<T>()
我们不把C#放在unity工程中,单独建立一个c#的解决方案,在这其中分别实现多个项目,每个项目导出一个动态库,也就是dll。
实现:
- 设置unity官方库引用
- 调试,自动生成mdb
- 自动复制dll到unity工程中
- 加密mono
TODO:
- 编译IL2CPP
- 加密IL2CPP
C#项目props
与targets
文件
props文件
C#的编译过程
C#->IL->机器码
什么是IL代码
IL-> Intermediate Language,它是一个部分编译的代码
什么是JIT
JIT-> Just in time compiler,使用JIT来编译IL代码到机器码
为啥么要用IL
因为开发环境和运行环境会有不同,根据运行环境JIT会来编译不同平带的机器码。同样在开发时,速度会更加的快了。
什么是CLR
CLR-> Common Language Runtime
基本可以放弃使用
https://www.c-sharpcorner.com/UploadFile/efa3cf/parallel-foreach-vs-foreach-loop-in-C-Sharp/
我们知道使用字符串优化的方式,就是尽量使用StringBuilder
来处理需要经常修改的字符串。但是我们平时用到字符串格式化的时候呢?我做了一个测试。
测试代码
我们使用 StringBuilder
、Static StringBuilder
、$
、string.Format
,分别调用100000 Format
次查看profile。
测试代码
public class ProfileTest : MonoBehaviour
{
private System.Text.StringBuilder _sb = new System.Text.StringBuilder();
private const string kFormator = "2222 {0}";
private string _test = string.Empty;
private void OnGUI()
{
if (GUILayout.Button("GC Collect"))
{
System.GC.Collect();
}
if (GUILayout.Button("StringBuilder"))
{
for (int i = 0; i < 100000; i++)
{
_sb.Clear();
_sb.Length = 0;
_sb.AppendFormat("in {0}", i);
_test = _sb.ToString();
}
UnityEditor.EditorApplication.isPaused = true;
}
if (GUILayout.Button("StringBuilder Const"))
{
for (int i = 0; i < 100000; i++)
{
_sb.Clear();
_sb.Length = 0;
_sb.AppendFormat(kFormator, i);
_test = _sb.ToString();
}
UnityEditor.EditorApplication.isPaused = true;
}
if (GUILayout.Button("Insert"))
{
for (int i = 0; i < 100000; i++)
{
_test = $"sssss{i}";
}
UnityEditor.EditorApplication.isPaused = true;
}
if (GUILayout.Button("Format"))
{
for (int i = 0; i < 100000; i++)
{
_test = string.Format("sss {0}", i);
}
UnityEditor.EditorApplication.isPaused = true;
}
if (GUILayout.Button("Format Const"))
{
for (int i = 0; i < 100000; i++)
{
_test = string.Format(kFormator, i);
}
UnityEditor.EditorApplication.isPaused = true;
}
}
}
文件
文件名字和类名字应该一致
MyClass.cs public class MyClass { … }