Files
Convention-Unity-Demo/Assets/Scripts/LookAtAnchor.cs

60 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Convention;
using UnityEngine;
namespace Demo.Game
{
public class LookAtAnchor : Updatement<int>
{
public static LookAtAnchor Make()
{
return new GameObject().AddComponent<LookAtAnchor>();
}
protected override int Lerp(int begin, int end, float t)
{
return begin;
}
private readonly List<ScriptableObject> LookAtCache = new();
[Content] public int LookAtObject;
[Content] public bool IsEnableUpdateEveryTick = false;
protected override void UpdateData(int data)
{
ScriptableObject target = LookAtCache[LookAtObject];
if (data != LookAtObject)
{
LookAtObject = data;
if (target != null)
transform.LookAt(target.transform);
}
else if (IsEnableUpdateEveryTick)
{
if (target != null)
transform.LookAt(target.transform);
}
}
/// <summary>
/// 在指定时刻切换面向的物体,并尝试一次更新
/// </summary>
[Convention.RScript.Variable.Attr.Method]
public void Add(float time, ScriptableObject target)
{
LookAtCache.Add(target);
ManualAddEntry(time, LookAtCache.Count - 1, default);
}
/// <summary>
/// 启动自动更新,将持续锁定面向的物体并更新
/// </summary>
[Convention.RScript.Variable.Attr.Method]
public void EnableUpdateEveryTick()
{
IsEnableUpdateEveryTick = true;
}
}
}