using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Stats; using KairoEngine.Core.GameActions; namespace KairoEngine.Stats.GameActions { public class GameActionContextStatsController : GameActionContextVariableBase { public StatsController value { get { if(_value == null && searchTarget != null) UpdateSearchTarget(); return _value; } set => _value = value; } [HorizontalGroup("value"), LabelText("@name"), ShowInInspector, PropertyOrder(1), ReadOnly, OnInspectorInit("UpdateSearchTarget")] private string result = ""; [HorizontalGroup("value", 0.14f), Button("@editButtonName"), ShowIf("@canEdit"), PropertyOrder(2)] private void EditValue() { if(showEdit == false) { showEdit = true; editButtonName = "Done"; } else { showEdit = false; editButtonName = "Edit"; } } private string editButtonName = "Edit"; [NonSerialized] internal StatsController _value; [NonSerialized, ShowInInspector, ShowIf("@showEdit && canEdit"), PropertyOrder(4), LabelText("Search in"), OnValueChanged("UpdateSearchTarget")] internal GameObject searchTarget; private void UpdateSearchTarget() { if(searchTarget == null) return; var statsComponent = searchTarget.GetComponent(); if(statsComponent == null) { statsComponent = searchTarget.GetComponentInChildren(); } if(statsComponent != null) { _value = statsComponent.statsController; result = $"{statsComponent.gameObject.name} StatsComponent"; } else { if(_value == null) result = ""; else result = "StatController"; } } public override string GetTypeName() => "GameActionContextStatsController"; public override string GetVariableName() => "StatsController"; public override T GetValue(T defaultValue) { var result = value; try { return (T)System.Convert.ChangeType(value, typeof(T)); } catch { return defaultValue; } } public static GameActionContextStatsController JSONToGameActionContextStatsController(string data) { return JsonUtility.FromJson(data); } public override void OnBeforeSerialize(GameActionObjectSerializer serializer) { serializer.SerializeGameObject($"{name}-searchTarget", searchTarget); } public override void OnBeforeDeserialize(GameActionObjectSerializer serializer) { searchTarget = serializer.DeserializeGameObject($"{name}-searchTarget"); //UpdateSearchTarget(); } } }