123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 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<StatsComponent>();
- if(statsComponent == null)
- {
- statsComponent = searchTarget.GetComponentInChildren<StatsComponent>();
- }
- 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>(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<GameActionContextStatsController>(data);
- }
- public override void OnBeforeSerialize(GameActionObjectSerializer serializer)
- {
- serializer.SerializeGameObject($"{name}-searchTarget", searchTarget);
- }
- public override void OnBeforeDeserialize(GameActionObjectSerializer serializer)
- {
- searchTarget = serializer.DeserializeGameObject($"{name}-searchTarget");
- //UpdateSearchTarget();
- }
- }
- }
|