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