2025-12-12 15:19:10 +08:00
|
|
|
|
using Demo.Attr;
|
2025-10-08 00:35:50 +08:00
|
|
|
|
using System;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Demo.Game
|
|
|
|
|
|
{
|
2025-12-12 15:19:10 +08:00
|
|
|
|
[Scriptable]
|
2025-10-10 23:52:27 +08:00
|
|
|
|
public class SplineAnchor : ScriptableObject, IDependOnSplineCore, IDependOnSplineRenderer
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
|
|
|
|
|
public static SplineAnchor Make()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new GameObject().AddComponent<SplineAnchor>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public SplineCore MySplineCore { get; set; }
|
|
|
|
|
|
public BasicSplineRenderer MySplineRenderer { get; set; }
|
2025-10-08 00:35:50 +08:00
|
|
|
|
public float MySplineOffset = 0;
|
|
|
|
|
|
private Action Updater = null;
|
|
|
|
|
|
|
|
|
|
|
|
protected override void UpdateTicks(float currentTime, float deltaTime, TickType tickType)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.UpdateTicks(currentTime, deltaTime, tickType);
|
2025-10-12 00:54:25 +08:00
|
|
|
|
Updater?.Invoke();
|
2025-10-08 00:35:50 +08:00
|
|
|
|
}
|
2025-09-25 19:04:05 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 加载并绑定到新样条线
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path">对象路径, 不存在时则立刻加载</param>
|
2025-10-27 20:24:15 +08:00
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
2025-11-24 18:02:57 +08:00
|
|
|
|
public void LoadSpline(string path)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
2025-11-24 18:02:57 +08:00
|
|
|
|
this.LoadSplineTool(path);
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 必须先执行LoadSpline加载样条线
|
|
|
|
|
|
/// </summary>
|
2025-11-24 18:02:57 +08:00
|
|
|
|
/// <param name="offset">百分比所在位置,取值范围是[0,1]</param>
|
2025-10-27 20:24:15 +08:00
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
2025-11-24 18:02:57 +08:00
|
|
|
|
public void EvaluatePosition(float offset)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
2025-11-24 18:02:57 +08:00
|
|
|
|
MySplineOffset = offset;
|
2025-10-08 00:35:50 +08:00
|
|
|
|
Updater = () => transform.position = MySplineCore.MySplineComputer.EvaluatePosition(MySplineOffset);
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-11-24 18:02:57 +08:00
|
|
|
|
/// 绑定到样条线渲染器上
|
2025-10-08 21:47:18 +08:00
|
|
|
|
/// 并设置跟随指定时间的时刻渲染器所生成的头部
|
2025-09-25 19:04:05 +08:00
|
|
|
|
/// </summary>
|
2025-11-24 18:02:57 +08:00
|
|
|
|
/// <param name="splineRenderer">样条线渲染器对象</param>
|
2025-09-25 19:04:05 +08:00
|
|
|
|
/// <param name="time">时刻</param>
|
2025-10-08 21:47:18 +08:00
|
|
|
|
/// <param name="isFollowPosition">是否跟随位置, 默认开启</param>
|
|
|
|
|
|
/// <param name="isFollowRotation">是否跟随旋转, 默认开启</param>
|
2025-10-27 20:24:15 +08:00
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
2025-11-24 18:02:57 +08:00
|
|
|
|
public void LoadSplineRenderer(BasicSplineRenderer splineRenderer, float time, bool isFollowPosition = true, bool isFollowRotation = true)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
2025-11-24 18:02:57 +08:00
|
|
|
|
MySplineRenderer = splineRenderer;
|
|
|
|
|
|
MySplineOffset = time;
|
|
|
|
|
|
bool bIsFollowPosition = isFollowPosition;
|
|
|
|
|
|
bool bIsFollowRotation = isFollowRotation;
|
2025-10-08 21:47:18 +08:00
|
|
|
|
if (bIsFollowPosition && bIsFollowRotation)
|
|
|
|
|
|
{
|
|
|
|
|
|
Updater = () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = MySplineRenderer.EvaluateClipTo(MySplineOffset);
|
2025-10-10 23:52:27 +08:00
|
|
|
|
transform.SetPositionAndRotation(result.position, result.rotation);
|
2025-10-08 21:47:18 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (bIsFollowPosition)
|
|
|
|
|
|
{
|
|
|
|
|
|
Updater = () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
transform.position = MySplineRenderer.EvaluateClipToPosition(MySplineOffset);
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Updater = () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = MySplineRenderer.EvaluateClipTo(MySplineOffset);
|
|
|
|
|
|
transform.rotation = result.rotation;
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|