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 { protected override ScriptLoadableConfig MakeConfig() { return new UpdatementIntConfig(); } public static LookAtAnchor Make() { return new GameObject().AddComponent(); } protected override int Lerp(int begin, int end, float t) { return begin; } public int LookAtObjectCache; public bool IsEnableUpdateEveryTick { get => GetConfig().IsEnableUpdateEveryTick; set => GetConfig().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); } } /// /// 在指定时刻切换面向的物体,并尝试一次更新 /// [Convention.RScript.Variable.Attr.Method] public void Add(float time, ScriptableObject target) { ManualAddEntry(time, GetRoot().FindIndex(target), default); } /// /// 启动自动更新,将持续锁定面向的物体并更新 /// [Convention.RScript.Variable.Attr.Method] public void EnableUpdateEveryTick() { IsEnableUpdateEveryTick = true; } } }