From eebf283e12e6bdb1ec5f0853ae1ccc22c62dcae8 Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Tue, 9 Dec 2025 15:12:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96=E4=B8=8E?= =?UTF-8?q?=E6=A3=80=E6=B5=8B,=20=E9=98=B2=E6=AD=A2=E5=81=87=E6=AD=BB?= =?UTF-8?q?=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Editor/SOEditor.cs | 2 +- Assets/Scripts/Framework/DDT.cs | 8 +++--- .../Framework/GameContent/GameController.cs | 25 ++++++++++++++++- Assets/Scripts/Framework/ScriptableObject.cs | 28 ++++++++++++++++++- .../DefaultScriptableObjectInstantiate.cs | 8 ------ .../PointBaseRenderer/BasicSplineRenderer.cs | 13 +++++++-- 6 files changed, 66 insertions(+), 18 deletions(-) diff --git a/Assets/Scripts/Editor/SOEditor.cs b/Assets/Scripts/Editor/SOEditor.cs index f9275a7..cdc74d8 100644 --- a/Assets/Scripts/Editor/SOEditor.cs +++ b/Assets/Scripts/Editor/SOEditor.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using UnityEditor; using UnityEngine; -//[CustomEditor(typeof(Demo.ScriptableObject), true)] +[CustomEditor(typeof(Demo.ScriptableObject), true)] public class SOEditor : Convention.AbstractCustomEditor { diff --git a/Assets/Scripts/Framework/DDT.cs b/Assets/Scripts/Framework/DDT.cs index 8b26bd6..87be7f4 100644 --- a/Assets/Scripts/Framework/DDT.cs +++ b/Assets/Scripts/Framework/DDT.cs @@ -77,11 +77,11 @@ namespace Demo.Game } protected override IEnumerator LoadFromImptCacheFile(ToolFile cacheFile) { - using var stream = File.OpenWrite(cacheFile.GetFullPath()); + using var stream = File.OpenRead(cacheFile.GetFullPath()); using var reader = new BinaryReader(stream); - int length = reader.ReadInt32(); - Datas = new NativeArray(length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); - for (int i = 0; i < length; i++) + Count = reader.ReadInt32(); + Datas = new NativeArray(Count, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); + for (int i = 0; i < Count; i++) { Datas[i] = reader.ReadSingle(); } diff --git a/Assets/Scripts/Framework/GameContent/GameController.cs b/Assets/Scripts/Framework/GameContent/GameController.cs index 844ded7..0a47dfa 100644 --- a/Assets/Scripts/Framework/GameContent/GameController.cs +++ b/Assets/Scripts/Framework/GameContent/GameController.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Collections.Generic; using System.IO; using Unity.Cinemachine; +using Unity.VisualScripting; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; @@ -200,7 +201,29 @@ namespace Demo.Game rootGameObject.SetContent(nameof(SongOffset), SongOffset); rootGameObject.SetContent(nameof(IsAutoPlay), IsAutoPlay ? 1 : 0); rootGameObject.SetContent("SongLength", MainAudio.CurrentClip.length); - yield return rootGameObject.ParseFromScriptFile2Expr(rootObject); + Stack loadingTask = new(); + loadingTask.Push(rootGameObject.ParseFromScriptFile2Expr(rootObject)); + float waitClock = Time.realtimeSinceStartup; + Dictionary DebugCounter = new(); + while (loadingTask.Count > 0) + { + // 防止大量无延迟函数如NewSubObject的使用导致的假死机 + if (Time.realtimeSinceStartup - waitClock > 0.1f) + { + yield return null; + waitClock = Time.realtimeSinceStartup; + } + if (loadingTask.Peek().MoveNext()) + { + if (loadingTask.Peek().Current is IEnumerator next) + loadingTask.Push(next); + } + else + { + loadingTask.Pop(); + } + yield return null; + } int NDFSCount = 0; IEnumerator NDFS(ScriptableObject current) { diff --git a/Assets/Scripts/Framework/ScriptableObject.cs b/Assets/Scripts/Framework/ScriptableObject.cs index bf22b8f..5799ca1 100644 --- a/Assets/Scripts/Framework/ScriptableObject.cs +++ b/Assets/Scripts/Framework/ScriptableObject.cs @@ -309,6 +309,11 @@ namespace Demo #region LoadSubScript + internal static int LoadingTaskCoroutineCount { get; set; } = 0; + internal readonly static LinkedList WaitingTaskForLoadingCoroutine = new(); + private readonly static WaitForEndOfFrame WaitForEndOfFrameInstance = new(); + private const int MaxLoadingCoroutine = 100; + /// /// 创建基于子脚本的实例对象 /// @@ -345,7 +350,22 @@ namespace Demo Childs.Add(child); // Load Child Script - ConventionUtility.StartCoroutine(child.ParseFromScriptFile2Expr(file)); + IEnumerator Foo() + { + // 缓存并延迟运行 + yield return WaitForEndOfFrameInstance; + yield return child.ParseFromScriptFile2Expr(file); + LoadingTaskCoroutineCount--; + + } + child.IsParseScript2Expr = true; + WaitingTaskForLoadingCoroutine.AddLast(Foo()); + while (LoadingTaskCoroutineCount < MaxLoadingCoroutine && WaitingTaskForLoadingCoroutine.Count > 0) + { + LoadingTaskCoroutineCount++; + ConventionUtility.StartCoroutine(WaitingTaskForLoadingCoroutine.First.Value); + WaitingTaskForLoadingCoroutine.RemoveFirst(); + } return child; } @@ -481,6 +501,12 @@ namespace Demo while (this.IsParseScript2Expr) { yield return null; + while (LoadingTaskCoroutineCount < MaxLoadingCoroutine && WaitingTaskForLoadingCoroutine.Count > 0) + { + LoadingTaskCoroutineCount++; + ConventionUtility.StartCoroutine(WaitingTaskForLoadingCoroutine.First.Value); + WaitingTaskForLoadingCoroutine.RemoveFirst(); + } } yield return DoSomethingDuringApplyScript(); // 增数 diff --git a/Assets/Scripts/Framework/ScriptableObjectInstantiate/DefaultScriptableObjectInstantiate.cs b/Assets/Scripts/Framework/ScriptableObjectInstantiate/DefaultScriptableObjectInstantiate.cs index ca6e66d..25b058c 100644 --- a/Assets/Scripts/Framework/ScriptableObjectInstantiate/DefaultScriptableObjectInstantiate.cs +++ b/Assets/Scripts/Framework/ScriptableObjectInstantiate/DefaultScriptableObjectInstantiate.cs @@ -251,7 +251,6 @@ namespace Demo public IEnumerator ParseFromScriptFile2Expr(ToolFile file) { - float parseStartTime = Time.realtimeSinceStartup; IsParseScript2Expr = true; try { @@ -342,15 +341,11 @@ namespace Demo finally { IsParseScript2Expr = false; - float parseEndTime = Time.realtimeSinceStartup; - float parseElapsed = (parseEndTime - parseStartTime) * 1000f; - Debug.Log($"[{nameof(ParseFromScriptFile2Expr)}] {file.GetFullPath()} 耗时: {parseElapsed:F2} ms", this); } } public IEnumerator ParseScript2Expr(string script) { - float parseStartTime = Time.realtimeSinceStartup; IsParseScript2Expr = true; try { @@ -364,9 +359,6 @@ namespace Demo finally { IsParseScript2Expr = false; - float parseEndTime = Time.realtimeSinceStartup; - float parseElapsed = (parseEndTime - parseStartTime) * 1000f; - Debug.Log($"[{nameof(ParseScript2Expr)}] {this.ScriptName} 耗时: {parseElapsed:F2} ms", this); } } } diff --git a/Assets/Scripts/MoreSpline/PointBaseRenderer/BasicSplineRenderer.cs b/Assets/Scripts/MoreSpline/PointBaseRenderer/BasicSplineRenderer.cs index db252ea..8ea837e 100644 --- a/Assets/Scripts/MoreSpline/PointBaseRenderer/BasicSplineRenderer.cs +++ b/Assets/Scripts/MoreSpline/PointBaseRenderer/BasicSplineRenderer.cs @@ -137,9 +137,16 @@ namespace Demo.Game { yield return base.DoSomethingDuringApplyScript(); MyMeshGenerator = this.GetOrAddComponent(); - MyMeshGenerator.spline = MySplineCore.MySplineComputer; - SetupMeshGenerator(MyMeshGenerator); - MyMeshGenerator.RebuildImmediate(); + if (MySplineCore) + { + MyMeshGenerator.spline = MySplineCore.MySplineComputer; + SetupMeshGenerator(MyMeshGenerator); + MyMeshGenerator.RebuildImmediate(); + } + else + { + Debug.LogError($"{nameof(MySplineCore)} not setup", this); + } } #region SetupMeshGenerator