2025-09-25 19:04:05 +08:00
|
|
|
|
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; }
|
2025-11-24 18:02:57 +08:00
|
|
|
|
void LoadSpline(string path);
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class DependOnSplineCoreUtility
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string LoadSplineDescription = @"
|
|
|
|
|
|
<summary>
|
|
|
|
|
|
加载并绑定到新样条线
|
|
|
|
|
|
</summary>
|
|
|
|
|
|
<param name=""path"">对象相对路径,若对象不存在则作为脚本相对路径加载</param>
|
|
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载并绑定到新样条线
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path">对象相对路径,若对象不存在则作为脚本相对路径加载</param>
|
2025-11-24 18:02:57 +08:00
|
|
|
|
public static SplineCore LoadSplineTool(this IDependOnSplineCore self, string path)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
|
|
|
|
|
var spline = self.SharedInterfaceScriptObject.FindWithPath(path, false);
|
|
|
|
|
|
if (spline == null)
|
2025-11-25 17:04:19 +08:00
|
|
|
|
spline = self.SharedInterfaceScriptObject.LoadSubScript(nameof(SplineCore), new ToolFile(path).GetFilename(true), path);
|
2025-11-24 18:02:57 +08:00
|
|
|
|
self.MySplineCore = (SplineCore)spline;
|
|
|
|
|
|
return self.MySplineCore;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class SplineCore : ScriptableObject
|
|
|
|
|
|
{
|
2025-12-11 18:03:57 +08:00
|
|
|
|
protected override bool IsSelfEnableUpdate => false;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
public static SplineCore Make()
|
|
|
|
|
|
{
|
2025-12-02 15:57:04 +08:00
|
|
|
|
SplineCore result = new GameObject("").AddComponent<SplineCore>();
|
|
|
|
|
|
var core = result.GetOrAddComponent<SplineComputer>();
|
2025-12-11 18:03:57 +08:00
|
|
|
|
result.m_MySplineComputer = core;
|
2025-12-02 15:57:04 +08:00
|
|
|
|
core.multithreaded = true;
|
|
|
|
|
|
return result;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-24 18:02:57 +08:00
|
|
|
|
[Content] private SplineComputer m_MySplineComputer;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
[Content] public int NodeContent = 0;
|
2025-12-11 18:03:57 +08:00
|
|
|
|
[Content] public List<SplineNode> MySplineNodes = new();
|
2025-09-25 19:04:05 +08:00
|
|
|
|
[Content] public SplineComputer.SampleMode MySampleMode = default;
|
|
|
|
|
|
[Content] public Spline.Type MyType = default;
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsClose = false;
|
|
|
|
|
|
|
2025-12-11 18:03:57 +08:00
|
|
|
|
public SplineComputer MySplineComputer => m_MySplineComputer;
|
|
|
|
|
|
//{
|
|
|
|
|
|
// get
|
|
|
|
|
|
// {
|
|
|
|
|
|
// if (m_MySplineComputer == null)
|
|
|
|
|
|
// m_MySplineComputer = this.GetComponent<SplineComputer>();
|
|
|
|
|
|
// return m_MySplineComputer;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
2025-11-24 18:02:57 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// <see cref="SplineCore"/>需要在子<see cref="SplineNode"/>都添加后再应用脚本才能使得节点生效
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected override IEnumerator DoSomethingDuringApplyScript()
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
2025-11-24 18:02:57 +08:00
|
|
|
|
yield return base.DoSomethingDuringApplyScript();
|
2025-09-25 19:04:05 +08:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-11-24 18:02:57 +08:00
|
|
|
|
if (tickType != TickType.Update)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
MySplineComputer.Rebuild();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IEnumerator UnloadScript()
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return base.UnloadScript();
|
|
|
|
|
|
NodeContent = 0;
|
|
|
|
|
|
MySplineNodes.Clear();
|
|
|
|
|
|
IsClose = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置样条线类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mode">CatmullRom, BSpline, Bezier, Linear </param>
|
2025-10-27 20:24:15 +08:00
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
2025-11-24 18:02:57 +08:00
|
|
|
|
public void SetType(Spline.Type mode)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
2025-11-24 18:02:57 +08:00
|
|
|
|
MyType = mode;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置采样类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mode">Default, Uniform, Optimized</param>
|
2025-10-27 20:24:15 +08:00
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
2025-11-24 18:02:57 +08:00
|
|
|
|
public void SetSampleMode(SplineComputer.SampleMode mode)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
2025-11-24 18:02:57 +08:00
|
|
|
|
MySampleMode = mode;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 闭环曲线
|
|
|
|
|
|
/// </summary>
|
2025-10-27 20:24:15 +08:00
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
2025-09-25 19:04:05 +08:00
|
|
|
|
public void SetClose()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsClose = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-11-24 18:02:57 +08:00
|
|
|
|
/// 加入节点或者添加节点组件后加入节点
|
2025-09-25 19:04:05 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path">脚本位置</param>
|
2025-10-27 20:24:15 +08:00
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
2025-11-25 17:04:19 +08:00
|
|
|
|
public void LoadNode(SplineNode node)
|
2025-11-12 16:12:01 +08:00
|
|
|
|
{
|
2025-11-25 17:04:19 +08:00
|
|
|
|
MySplineNodes.Add(node);
|
2025-11-12 16:12:01 +08:00
|
|
|
|
}
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|