using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UGFramework;
using UnityEngine.Events;
using System.Diagnostics;
namespace GamePlay
{
public class NewBehaviourScript : MonoBehaviour
{
public WeaponPropertySObj weaponP;
public int damage = 30;
public int damage1 = 0;
public int damage2 = 0;
public int TIMES = 100000000;
void Start()
{
Normal();
SO();
}
public void ExcuteTime(UnityAction method)
{
Stopwatch sw = new Stopwatch();
sw.Start();
method();
sw.Stop();
UnityEngine.Debug.Log(string.Format("测试方法总共耗时: {0} ms(毫秒)", sw.ElapsedMilliseconds));
}
private void Normal()
{
print("普通赋值");
ToolKit.ExcuteTime(() =>
{
for (int i = 0; i < TIMES; i++)
{
damage1 = damage;
}
});
}
private void SO()
{
print("so赋值");
ToolKit.ExcuteTime(() =>
{
for (int i = 0; i < TIMES; i++)
{
damage2 = weaponP.damage;
}
});
}
}
}
经过多次对结果进行对比,如下图:
从结果可以看到两种方式的执行效率都差不多。



这篇博客通过一个Unity游戏场景展示了两种不同的赋值方式——普通赋值和使用SO(SerializedObject)赋值,并通过Stopwatch类进行了性能测试。结果显示,两种方式在执行效率上几乎没有显著差异,这对于游戏开发中的性能优化提供了参考。
1245

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



