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

86 lines
2.5 KiB
C#

using Convention;
using Demo.Game.Attr;
using Demo.Game.ConfigType;
using System.IO;
using UnityEngine;
namespace Demo.Game
{
namespace ConfigType
{
public class LookAtAnchorConfig : UpdatementIntConfig
{
[Content] public bool IsEnableUpdateEveryTick = false;
public override void Deserialize(BinaryReader reader)
{
IsEnableUpdateEveryTick = BinarySerializeUtility.ReadBool(reader);
base.Deserialize(reader);
}
public override void Serialize(BinaryWriter writer)
{
BinarySerializeUtility.WriteBool(writer, IsEnableUpdateEveryTick);
base.Serialize(writer);
}
}
}
[Scriptable]
public class LookAtAnchor : Updatement<int>
{
protected override ScriptLoadableConfig MakeConfig()
{
return new UpdatementIntConfig();
}
public static LookAtAnchor Make()
{
return new GameObject().AddComponent<LookAtAnchor>();
}
protected override int Lerp(int begin, int end, float t)
{
return begin;
}
public int LookAtObjectCache;
public bool IsEnableUpdateEveryTick
{
get => GetConfig<LookAtAnchorConfig>().IsEnableUpdateEveryTick;
set => GetConfig<LookAtAnchorConfig>().IsEnableUpdateEveryTick = value;
}
protected override void UpdateData(int data)
{
ScriptableObject target = GetRoot().FindWithIndex(data);
if (data != LookAtObjectCache)
{
LookAtObjectCache = 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)
{
ManualAddEntry(time, GetRoot().FindIndex(target), default);
}
/// <summary>
/// 启动自动更新,将持续锁定面向的物体并更新
/// </summary>
[Convention.RScript.Variable.Attr.Method]
public void EnableUpdateEveryTick()
{
IsEnableUpdateEveryTick = true;
}
}
}