结论
先说结论,在高频场景下,可以使用以下方式优化属性读取性能:
#if UNITY_EDITOR
private int health;
public int Health { get => health; }
#else
public int Health;
#endif
代码解释:
- 开发阶段用只读属性,防止外部随意修改
- 发布后,用字段,获得更好的性能
- 优化后,读取速度是原来的2-3倍,但都在ns级别
- 理论上,如果项目是用更老版本的Unity或C#,能取得更好的优化效果
性能测试
using System;
using System.Diagnostics;
using System.Text;
using UnityEngine;
/// <summary>
/// Unity 内性能基准测试:字段 vs 属性(模拟 DEVELOPMENT_BUILD 条件编译)
/// 挂载到任意 GameObject,点击 Play 后按空格键运行测试
/// </summary>
public class ProperVsFieldTest : MonoBehaviour
{
[Header("测试配置")]
[SerializeField] private int warmupIterations = 100; // 预热次数
[SerializeField] private int testIterations = 10000; // 正式测试次数
[SerializeField] private int unitsPerFrame = 100; // 模拟每帧遍历单位数
// 模拟 DEVELOPMENT_BUILD:私有字段 + 只读属性
public class PropertyUnit
{
private int health;
public int Health { get { return health; } }
public PropertyUnit(int h) => health = h;
public void TakeDamage(int dmg) => health -= dmg;
}
// 模拟 RELEASE:直接 public 字段
public class FieldUnit
{
public int Health;
public FieldUnit(int h) => Health = h;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
RunBenchmark();
}
}
void RunBenchmark()
{
var sb = new StringBuilder();
sb.AppendLine("=== Unity Health Access Benchmark ===");
sb.AppendLine($"Iterations: {testIterations:N0} | Units/Frame: {unitsPerFrame}");
sb.AppendLine("----------------------------------------");
// 1. 字段读取
var fieldUnits = new FieldUnit[unitsPerFrame];
for (var i = 0; i < unitsPerFrame; i++) fieldUnits[i] = new FieldUnit(100);
var fieldReadTime = Measure(() =>
{
var sum = 0;
for (var i = 0; i < testIterations; i++)
{
for (var u = 0; u < unitsPerFrame; u++)
{
sum += fieldUnits[u].Health;
}
}
return sum;
}, warmupIterations);
sb.AppendLine($"[字段读取] {fieldReadTime,8} ms | 单次: {fieldReadTime * 1000000.0 / (testIterations * unitsPerFrame):F2} ns");
// 2. 属性读取
var propUnits = new PropertyUnit[unitsPerFrame];
for (var i = 0; i < unitsPerFrame; i++) propUnits[i] = new PropertyUnit(100);
var propReadTime = Measure(() =>
{
var sum = 0;
for (var i = 0; i < testIterations; i++)
{
for (var u = 0; u < unitsPerFrame; u++)
{
sum += propUnits[u].Health;
}
}
return sum;
}, warmupIterations);
var ratio = (double)propReadTime / fieldReadTime;
sb.AppendLine($"[属性读取] {propReadTime,8} ms | 单次: {propReadTime * 1000000.0 / (testIterations * unitsPerFrame):F2} ns | 倍率: {ratio:F2}x");
// 3. 字段写入
var fieldWriteTime = Measure(() =>
{
for (var i = 0; i < testIterations; i++)
{
for (var u = 0; u < unitsPerFrame; u++)
{
fieldUnits[u].Health = i;
}
}
}, warmupIterations);
sb.AppendLine($"[字段写入] {fieldWriteTime,8} ms | 单次: {fieldWriteTime * 1000000.0 / (testIterations * unitsPerFrame):F2} ns");
// 4. 属性内部修改(通过方法)
var propModifyTime = Measure(() =>
{
for (var i = 0; i < testIterations; i++)
{
for (var u = 0; u < unitsPerFrame; u++)
{
propUnits[u].TakeDamage(1);
}
}
}, warmupIterations);
sb.AppendLine($"[属性修改] {propModifyTime,8} ms | 单次: {propModifyTime * 1000000.0 / (testIterations * unitsPerFrame):F2} ns");
// 5. 模拟真实游戏:每帧读取所有单位血量更新 UI
var frameSimField = Measure(() =>
{
for (var frame = 0; frame < testIterations; frame++)
{
var totalHp = 0;
for (var u = 0; u < unitsPerFrame; u++)
totalHp += fieldUnits[u].Health;
}
}, 100);
var frameSimProp = Measure(() =>
{
for (var frame = 0; frame < testIterations; frame++)
{
var totalHp = 0;
for (var u = 0; u < unitsPerFrame; u++)
totalHp += propUnits[u].Health;
}
}, 100);
sb.AppendLine("----------------------------------------");
sb.AppendLine($"[帧模拟-字段] {frameSimField,6} ms / {testIterations}帧");
sb.AppendLine($"[帧模拟-属性] {frameSimProp,6} ms / {testIterations} | 倍率: {(double)frameSimProp / frameSimField:F2}x");
sb.AppendLine("=== End ===");
UnityEngine.Debug.Log(sb.ToString());
}
/// <summary>
/// 测量 Action 的执行时间,自动预热并取最佳值
/// </summary>
long Measure(Func<int> action, int warmup)
{
// 预热
for (var i = 0; i < warmup; i++) action();
GC.Collect();
GC.WaitForPendingFinalizers();
var sw = Stopwatch.StartNew();
var result = action(); // 使用返回值防止编译器优化掉整个循环
sw.Stop();
// 防止 result 被优化掉
if (result == -999999) UnityEngine.Debug.Log("unlikely");
return sw.ElapsedMilliseconds;
}
long Measure(Action action, int warmup)
{
for (var i = 0; i < warmup; i++) action();
GC.Collect();
GC.WaitForPendingFinalizers();
var sw = Stopwatch.StartNew();
action();
sw.Stop();
return sw.ElapsedMilliseconds;
}
}
使用方式:
- 挂载到场景中的任意 GameObject
- 运行后, 按空格键
- 等待数秒后,查看Console的输出结果
测试结果
=== Unity Health Access Benchmark ===
Iterations: 10,000 | Units/Frame: 100
[字段读取] 3 ms | 单次: 3.00 ns
[属性读取] 7 ms | 单次: 7.00 ns | 倍率: 2.33x
[字段写入] 3 ms | 单次: 3.00 ns
[属性修改] 6 ms | 单次: 6.00 ns
[帧模拟-字段] 2 ms / 10000帧
[帧模拟-属性] 6 ms / 10000 | 倍率: 3.00x
197

被折叠的 条评论
为什么被折叠?



