Files
Convention-Unity-Demo/Assets/Scripts/MoreSpline/Updatement/SplineMovement.cs

107 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using Convention;
using Convention.WindowsUI.Variant;
using Dreamteck.Splines;
using UnityEngine;
namespace Demo.Game
{
public class SplineMovement : BasicSplineJustFollow
{
public static SplineMovement Make()
{
return new GameObject().AddComponent<SplineMovement>();
}
private SplineRenderer MySplineRenderer;
/// <summary>
/// 设置为仅跟随将会被动的跟随spline运动
/// 这在多个脚本都绑定在同一个spline计算核心上时非常有用
/// </summary>
[Content, SerializeField] private bool IsJustFollow = false;
[Content, SerializeField] private double Cache = 0;
protected override void UpdateData(float data)
{
Cache = MySplineCore.MySplineComputer.Evaluate(data).percent;
UpdateTarget.transform.position = MySplineCore.MySplineComputer.EvaluatePosition(data);
}
private SceneGameWindow SceneStats;
public override IEnumerator LoadScript(string script)
{
SceneStats = FindObjectOfType<SceneGameWindow>();
yield return base.LoadScript(script);
if (GetRoot().RootGameController.IsHideTrackRender == false)
{
if (MySplineCore == null)
yield break;
var splineGameObject = MySplineCore.gameObject;
splineGameObject.GetOrAddComponent<MeshFilter>();
var meshRenderer = splineGameObject.GetOrAddComponent<MeshRenderer>();
meshRenderer.enabled = true;
meshRenderer.material = Resources.Load<Material>("Line/Dash");
MySplineRenderer = splineGameObject.AddComponent<SplineRenderer>();
MySplineRenderer.spline = MySplineCore.MySplineComputer;
MySplineRenderer.uvMode = MeshGenerator.UVMode.UniformClip;
MySplineRenderer.Rebuild();
}
}
protected override void UpdateTicks(float currentTime, float deltaTime, TickType tickType)
{
base.UpdateTicks(currentTime, deltaTime, tickType);
if (GetRoot().RootGameController.IsHideTrackRender == false)
{
if (IsJustFollow == false)
{
if (SceneStats.IsSelectSceneCamera || tickType == TickType.Pause || tickType == TickType.Reset)
{
MySplineCore.gameObject.GetComponent<MeshRenderer>().enabled = true;
MySplineCore.gameObject.GetComponent<SplineRenderer>().Rebuild();
}
else if (tickType == TickType.Start)
{
MySplineCore.gameObject.GetComponent<MeshRenderer>().enabled = false;
}
//不为跟随模式时需要自己更新clip
if (tickType == TickType.Update || tickType == TickType.Reset)
{
MySplineRenderer.clipFrom = Cache;
}
}
else
{
}
}
}
public override IEnumerator UnloadScript()
{
if (GetRoot().RootGameController.IsHideTrackRender == false)
{
var splineGameObject = MySplineCore.gameObject;
Destroy(splineGameObject.GetComponent<SplineRenderer>());
Destroy(splineGameObject.GetComponent<MeshRenderer>());
Destroy(splineGameObject.GetComponent<MeshFilter>());
MySplineCore = null;
MySplineRenderer = null;
}
yield return base.UnloadScript();
}
/// <summary>
/// 设置为仅跟随将会被动的跟随spline运动
/// 这在多个脚本都绑定在同一个spline计算核心上时非常有用
/// </summary>
[Convention.RScript.Variable.Attr.Method]
public void JustFollow()
{
IsJustFollow = true;
}
}
}