using System; using System.Collections; using System.Linq; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; namespace KairoEngine.Core.GameActions { [System.Serializable, HideReferenceObjectPicker] public class InstantiateGameAction : GameActionBase { public override string name { get { return $"Instantiate {GetObjName(prefab)}"; } } public override GameActionsController controller { get => _controller; set { _controller = value; typeName = "InstantiateGameAction"; } } public override string GetTypeName() => "InstantiateGameAction"; public override string GetActionName() => "Core/Instantiate GameObject"; [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/InstantiateGameActionIcon.png")] [NonSerialized, ShowInInspector, AssetsOnly, LabelText("Prefab")] public GameObject prefab; public InstantiateGameAction(GameActionsController controller) : base(controller) { this.controller = controller; className = this.GetType().AssemblyQualifiedName; } private string GetObjName(GameObject obj) { if(obj == null) return "NULL"; else return $"\'{obj.name}\'"; } public override void Start() { GameObject.Instantiate(prefab); _done = true; _started = true; } public override void Update() { } public override void Restart() { _done = false; _started = false; } public static InstantiateGameAction JSONToInstantiateGameAction(string data) { return JsonUtility.FromJson(data); } private InstantiateGameAction Duplicate(GameActionsController controller) { InstantiateGameAction action = new InstantiateGameAction(controller == null ? this.controller : controller); action.controller = controller; return action; } private T GetVariable(string title, T defaultValue) { for (int i = 0; i < controller.context.variables.Count; i++) { if(controller.context.variables[i].name == title) { return controller.context.variables[i].GetValue(defaultValue); } } return defaultValue; } public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth) { serializer.SerializeGameObject($"{depth}-{n}-Prefab", prefab); } public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth) { prefab = serializer.DeserializeGameObject($"{depth}-{n}-Prefab"); } } }