Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
PrototypingGameObject.cs
Go to the documentation of this file.
1 using System;
3 using UnityEditor;
4 using UnityEngine;
5 
6 namespace droid.Runtime.Utilities.GameObjects {
10  public abstract class PrototypingGameObject : MonoBehaviour,
14  [SerializeField]
15  [Header("Naming", order = 10)]
16  protected string _Custom_Name = "";
17  #if NEODROID_DEBUG
18  [SerializeField] bool _debugging;
19  #endif
20  [Header("Development", order = 90)]
23  [SerializeField]
24  bool _disables_children = false;
25 
26  #if UNITY_EDITOR
27  [SerializeField]
30  bool _editor_reregistering = true;
31  #endif
32 
33  [SerializeField] bool _unregister_at_disable = false;
34 
35  #if NEODROID_DEBUG
36  public bool Debugging { get { return this._debugging; } set { this._debugging = value; } }
39  #endif
40  public virtual string PrototypingTypeName { get { return this.GetType().Name; } }
42 
46  public string Identifier {
47  get {
48  if (!string.IsNullOrWhiteSpace(this._Custom_Name)) {
49  return this._Custom_Name.Trim();
50  }
51 
52  return this.name + this.PrototypingTypeName;
53  }
54  }
55 
58  protected void Start() { this.ReRegister(); }
59 
60  void ReRegister() {
61  try {
62  if (this.enabled && this.isActiveAndEnabled) {
63  this.Setup();
64  this.UnRegisterComponent();
65  this.RegisterComponent();
66  }
67  } catch (ArgumentNullException e) {
68  Debug.LogWarning(e);
69  Debug.Log($"You must override RegisterComponent and UnRegisterComponent for component {this.GetType()} for gameobject {this.Identifier} in order to Re-register component on every 'OnValidate' while in edit-mode");
70  }
71  }
72 
75  protected void Awake() {
76  if (this.enabled && this.isActiveAndEnabled) {
77  this.Clear();
78  }
79  }
80 
83  void OnDisable() {
84  if (this._disables_children) {
85  var children = this.GetComponentsInChildren<PrototypingGameObject>();
86  foreach (var child in children) {
87  if (child.gameObject != this.gameObject) {
88  child.enabled = false;
89  child.gameObject.SetActive(false);
90  }
91  }
92 
93  foreach (Transform child in this.transform) {
94  if (child != this.transform) {
95  child.GetComponent<PrototypingGameObject>()?.CallOnDisable();
96  child.gameObject.SetActive(false);
97  }
98  }
99  }
100 
101  if (this._unregister_at_disable) {
102  this.UnRegisterComponent();
103  }
104  }
105 
108  void CallOnDisable() { this.OnDisable(); }
109 
112  void CallOnEnable() { this.OnEnable(); }
113 
116  void OnEnable() {
117  if (this._disables_children) {
118  foreach (Transform child in this.transform) {
119  if (child != this.transform) {
120  child.gameObject.SetActive(true);
121  child.GetComponent<PrototypingGameObject>()?.CallOnEnable();
122  }
123  }
124 
125  var children = this.GetComponentsInChildren<PrototypingGameObject>();
126  foreach (var child in children) {
127  if (child.gameObject != this.gameObject) {
128  child.enabled = true;
129  child.gameObject.SetActive(true);
130  }
131  }
132  }
133 
134  this.ReRegister();
135  }
136 
139  protected virtual void Setup() { }
140 
143  protected virtual void Clear() { }
144 
145  #if UNITY_EDITOR
146  void OnValidate() { // Only called in the editor
149  if (EditorApplication.isPlaying || !this._editor_reregistering) {
150  return;
151  }
152 
153  this.ReRegister();
154  }
155  #endif
156 
160  protected abstract void UnRegisterComponent();
161 
165  protected abstract void RegisterComponent();
166 
169  public void RefreshStart() {
170  if (this.enabled && this.isActiveAndEnabled) {
171  this.Start();
172  }
173  }
174 
177  public void RefreshAwake() {
178  if (this.enabled && this.isActiveAndEnabled) {
179  this.Awake();
180  }
181  }
182  }
183 }