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

62 lines
1.7 KiB
C#
Raw Normal View History

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