WuhuIslandTesting/Library/PackageCache/com.unity.splines@1.0.1/Samples~/Runtime/AnimateSplineExtrude.cs

43 lines
882 B
C#
Raw Normal View History

2025-01-07 02:06:59 +01:00
using System;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Splines;
namespace samples.Runtime
{
/// <summary>
/// Animate extruding a section of a spline.
/// </summary>
[RequireComponent(typeof(SplineExtrude))]
class AnimateSplineExtrude : MonoBehaviour
{
SplineExtrude m_Extrude;
[SerializeField, Range(.0001f, 2f)]
float m_Speed = .25f;
float m_Span;
[SerializeField]
bool m_RebuildExtrudeOnUpdate = true;
void Start()
{
m_Extrude = GetComponent<SplineExtrude>();
m_Span = (m_Extrude.range.y - m_Extrude.range.x) * .5f;
}
void Update()
{
bool closed = m_Extrude.spline.Closed;
float t = closed
? Time.time * m_Speed
: Mathf.Lerp(-m_Span, 1 + m_Span, math.frac(Time.time * m_Speed));
m_Extrude.range = new float2(t - m_Span, t + m_Span);
if (m_RebuildExtrudeOnUpdate)
m_Extrude.Rebuild();
}
}
}