using System; using System.Collections; using System.Collections.Generic; using Convention; using Dreamteck.Splines; using UnityEngine; namespace Demo.Game { public struct SplineClipDuration { public float ClipFrom; public float ClipTo; public SplineClipDuration(float clipFrom = 0, float clipTo = 1) { this.ClipFrom = clipFrom; this.ClipTo = clipTo; } } public interface IDependOnSplineCore : IScriptableObject { SplineCore MySplineCore { get; set; } void LoadSpline(string path); } public static class DependOnSplineCoreUtility { public const string LoadSplineDescription = @" 加载并绑定到新样条线 对象相对路径,若对象不存在则作为脚本相对路径加载 "; /// /// 加载并绑定到新样条线 /// /// 对象相对路径,若对象不存在则作为脚本相对路径加载 public static SplineCore LoadSplineTool(this IDependOnSplineCore self, string path) { var spline = self.SharedInterfaceScriptObject.FindWithPath(path, false); if (spline == null) spline = self.SharedInterfaceScriptObject.LoadSubScript(nameof(SplineCore), new ToolFile(path).GetFilename(true), path); self.MySplineCore = (SplineCore)spline; return self.MySplineCore; } } public class SplineCore : ScriptableObject { protected override bool IsSelfEnableUpdate => false; public static SplineCore Make() { SplineCore result = new GameObject("").AddComponent(); var core = result.GetOrAddComponent(); result.m_MySplineComputer = core; core.multithreaded = true; return result; } [Content] private SplineComputer m_MySplineComputer; [Content] public int NodeContent = 0; [Content] public List MySplineNodes = new(); [Content] public SplineComputer.SampleMode MySampleMode = default; [Content] public Spline.Type MyType = default; public bool IsClose = false; public SplineComputer MySplineComputer => m_MySplineComputer; //{ // get // { // if (m_MySplineComputer == null) // m_MySplineComputer = this.GetComponent(); // return m_MySplineComputer; // } //} /// /// 需要在子都添加后再应用脚本才能使得节点生效 /// /// protected override IEnumerator DoSomethingDuringApplyScript() { yield return base.DoSomethingDuringApplyScript(); NodeContent = 0; MySplineComputer.SetPoints(new SplinePoint[MySplineNodes.Count]); foreach (SplineNode node in MySplineNodes) { node.AddTo(this); NodeContent++; } if (IsClose) MySplineComputer.Close(); else MySplineComputer.Break(); MySplineComputer.sampleMode = MySampleMode; MySplineComputer.type = MyType; MySplineComputer.Rebuild(); } protected override void UpdateTicks(float currentTime, float deltaTime, TickType tickType) { if (tickType != TickType.Update) MySplineComputer.Rebuild(); } public override IEnumerator UnloadScript() { yield return base.UnloadScript(); NodeContent = 0; MySplineNodes.Clear(); IsClose = false; } /// /// 设置样条线类型 /// /// CatmullRom, BSpline, Bezier, Linear [Convention.RScript.Variable.Attr.Method] public void SetType(Spline.Type mode) { MyType = mode; } /// /// 设置采样类型 /// /// Default, Uniform, Optimized [Convention.RScript.Variable.Attr.Method] public void SetSampleMode(SplineComputer.SampleMode mode) { MySampleMode = mode; } /// /// 闭环曲线 /// [Convention.RScript.Variable.Attr.Method] public void SetClose() { IsClose = true; } /// /// 加入节点或者添加节点组件后加入节点 /// /// 脚本位置 [Convention.RScript.Variable.Attr.Method] public void LoadNode(SplineNode node) { MySplineNodes.Add(node); } } }