一些性能优化

This commit is contained in:
2025-12-01 00:02:13 +08:00
parent e06f01e0f1
commit 1b7f1497b1

View File

@@ -100,7 +100,8 @@ namespace Demo
/// </summary>
public partial class ScriptableObject
{
[Content, SerializeField] private Vector3
[Content, SerializeField]
private Vector3
EnterGameLocalPosition = Vector3.zero,
EnterGameEulerAngles = Vector3.zero,
EnterGameLocalScaling = Vector3.one;
@@ -455,12 +456,12 @@ namespace Demo
public static class RandomTool
{
public static float Random(float min,float max)
public static float Random(float min, float max)
{
return UnityEngine.Random.Range(min,max);
return UnityEngine.Random.Range(min, max);
}
public static float Random(double min,double max)
public static float Random(double min, double max)
{
return UnityEngine.Random.Range((float)min, (float)max);
}
@@ -549,7 +550,8 @@ namespace Demo
if (this.IsSelfEnableUpdate && UpdatePerFrame > 0)
{
if (ScriptUpdateCounter % UpdatePerFrame == 0)
UpdateTicks(currentTime, deltaTime, tickType);
using (Profiler.BeginZone($"{this.ScriptName}.UpdateTicks"))
UpdateTicks(currentTime, deltaTime, tickType);
ScriptUpdateCounter += tickType == TickType.Update ? 1 : 0;
}
// Childs UpdateTicks
@@ -628,6 +630,15 @@ namespace Demo
}
if (IsEnableUpdate == false && Parent != null)
Parent.UpdateChilds.Remove(this);
// 当只存在一个需要更新的子物体并且自身不需要更新时, 直接简化调用
if (this.IsSelfEnableUpdate == false && this.UpdateChilds.Count == 1)
{
Parent.UpdateChilds.Remove(this);
var child = this.UpdateChilds[0];
this.UpdateChilds.Clear();
Parent.UpdateChilds.Add(child);
IsEnableUpdate = false;
}
}
IsScriptApply = true;
}
@@ -685,7 +696,7 @@ namespace Demo
}
}
#endregion
#endregion
public interface IAssetBundleLoader : IScriptableObject
{
@@ -804,7 +815,7 @@ namespace Demo
protected override IEnumerator DoSomethingDuringApplyScript()
{
yield return base.DoSomethingDuringApplyScript();
if(MyTimelineEntry==null)
if (MyTimelineEntry == null)
{
MyTimelineEntry = TimelineWindow.CreateRootItemEntries(1)[0];
MyTimelineItem = MyTimelineEntry.ref_value.GetComponent<Editor.UI.TimelineItem>();
@@ -830,7 +841,7 @@ namespace Demo
{
yield return base.UnloadScript();
// 这里的两处判空是因为如果抛出错误就会打断了逻辑, 所以这里需要判断
if (MyTimelineItem)
if (MyTimelineItem != null)
{
MyTimelineItem.RawButton.onClick.RemoveAllListeners();
MyTimelineItem = null;
@@ -847,11 +858,19 @@ namespace Demo
}
private float UIResizeOnTimelineCount = 0;
protected override void UpdateTicks(float currentTime, float deltaTime, TickType tickType)
{
base.UpdateTicks(currentTime, deltaTime, tickType);
// 存在严重的性能开销, 在解决之前将不会允许其快速自动更新
using (Profiler.BeginZone($"{nameof(TimelineScriptObject)}.{nameof(MyTimelineItem.ResizeOnTimeline)}"))
{
MyTimelineItem.ResizeOnTimeline();
if (UIResizeOnTimelineCount > 0.1 || tickType != TickType.Update)
{
UIResizeOnTimelineCount = 0;
MyTimelineItem.ResizeOnTimeline();
}
UIResizeOnTimelineCount += deltaTime;
}
}
@@ -861,7 +880,7 @@ namespace Demo
{
static float Foo(uint hash)
{
float a= (hash % 10) * 0.1f;
float a = (hash % 10) * 0.1f;
return a;
}