InstantiateGameAction.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. namespace KairoEngine.Core.GameActions
  9. {
  10. [System.Serializable, HideReferenceObjectPicker]
  11. public class InstantiateGameAction : GameActionBase
  12. {
  13. public override string name
  14. {
  15. get
  16. {
  17. return $"Instantiate {GetObjName(prefab)}";
  18. }
  19. }
  20. public override GameActionsController controller {
  21. get => _controller;
  22. set
  23. {
  24. _controller = value;
  25. typeName = "InstantiateGameAction";
  26. }
  27. }
  28. public override string GetTypeName() => "InstantiateGameAction";
  29. public override string GetActionName() => "Core/Instantiate GameObject";
  30. [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/InstantiateGameActionIcon.png")]
  31. [NonSerialized, ShowInInspector, AssetsOnly, LabelText("Prefab")] public GameObject prefab;
  32. public InstantiateGameAction(GameActionsController controller) : base(controller)
  33. {
  34. this.controller = controller;
  35. className = this.GetType().AssemblyQualifiedName;
  36. }
  37. private string GetObjName(GameObject obj)
  38. {
  39. if(obj == null) return "NULL";
  40. else return $"\'{obj.name}\'";
  41. }
  42. public override void Start()
  43. {
  44. GameObject.Instantiate(prefab);
  45. _done = true;
  46. _started = true;
  47. }
  48. public override void Update() { }
  49. public override void Restart()
  50. {
  51. _done = false;
  52. _started = false;
  53. }
  54. public static InstantiateGameAction JSONToInstantiateGameAction(string data)
  55. {
  56. return JsonUtility.FromJson<InstantiateGameAction>(data);
  57. }
  58. private InstantiateGameAction Duplicate(GameActionsController controller)
  59. {
  60. InstantiateGameAction action = new InstantiateGameAction(controller == null ? this.controller : controller);
  61. action.controller = controller;
  62. return action;
  63. }
  64. private T GetVariable<T>(string title, T defaultValue)
  65. {
  66. for (int i = 0; i < controller.context.variables.Count; i++)
  67. {
  68. if(controller.context.variables[i].name == title)
  69. {
  70. return controller.context.variables[i].GetValue<T>(defaultValue);
  71. }
  72. }
  73. return defaultValue;
  74. }
  75. public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth)
  76. {
  77. serializer.SerializeGameObject($"{depth}-{n}-Prefab", prefab);
  78. }
  79. public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth)
  80. {
  81. prefab = serializer.DeserializeGameObject($"{depth}-{n}-Prefab");
  82. }
  83. }
  84. }