A game about forced loneliness, made by TACStudios
1using System; 2using UnityEngine; 3using UnityObject = UnityEngine.Object; 4 5namespace Unity.VisualScripting 6{ 7 public static class ComponentHolderProtocol 8 { 9 public static bool IsComponentHolderType(Type type) 10 { 11 Ensure.That(nameof(type)).IsNotNull(type); 12 13 return typeof(GameObject).IsAssignableFrom(type) || typeof(Component).IsAssignableFrom(type); 14 } 15 16 public static bool IsComponentHolder(this UnityObject uo) 17 { 18 return uo is GameObject || uo is Component; 19 } 20 21 public static GameObject GameObject(this UnityObject uo) 22 { 23 if (uo is GameObject) 24 { 25 return (GameObject)uo; 26 } 27 else if (uo is Component) 28 { 29 return ((Component)uo).gameObject; 30 } 31 else 32 { 33 return null; 34 } 35 } 36 37 public static T AddComponent<T>(this UnityObject uo) where T : Component 38 { 39 if (uo is GameObject) 40 { 41 return ((GameObject)uo).AddComponent<T>(); 42 } 43 else if (uo is Component) 44 { 45 return ((Component)uo).gameObject.AddComponent<T>(); 46 } 47 else 48 { 49 throw new NotSupportedException(); 50 } 51 } 52 53 public static T GetOrAddComponent<T>(this UnityObject uo) where T : Component 54 { 55 return uo.GetComponent<T>() ?? uo.AddComponent<T>(); 56 } 57 58 public static T GetComponent<T>(this UnityObject uo) 59 { 60 if (uo is GameObject) 61 { 62 return ((GameObject)uo).GetComponent<T>(); 63 } 64 else if (uo is Component) 65 { 66 return ((Component)uo).GetComponent<T>(); 67 } 68 else 69 { 70 throw new NotSupportedException(); 71 } 72 } 73 74 public static T GetComponentInChildren<T>(this UnityObject uo) 75 { 76 if (uo is GameObject) 77 { 78 return ((GameObject)uo).GetComponentInChildren<T>(); 79 } 80 else if (uo is Component) 81 { 82 return ((Component)uo).GetComponentInChildren<T>(); 83 } 84 else 85 { 86 throw new NotSupportedException(); 87 } 88 } 89 90 public static T GetComponentInParent<T>(this UnityObject uo) 91 { 92 if (uo is GameObject) 93 { 94 return ((GameObject)uo).GetComponentInParent<T>(); 95 } 96 else if (uo is Component) 97 { 98 return ((Component)uo).GetComponentInParent<T>(); 99 } 100 else 101 { 102 throw new NotSupportedException(); 103 } 104 } 105 106 public static T[] GetComponents<T>(this UnityObject uo) 107 { 108 if (uo is GameObject) 109 { 110 return ((GameObject)uo).GetComponents<T>(); 111 } 112 else if (uo is Component) 113 { 114 return ((Component)uo).GetComponents<T>(); 115 } 116 else 117 { 118 throw new NotSupportedException(); 119 } 120 } 121 122 public static T[] GetComponentsInChildren<T>(this UnityObject uo) 123 { 124 if (uo is GameObject) 125 { 126 return ((GameObject)uo).GetComponentsInChildren<T>(); 127 } 128 else if (uo is Component) 129 { 130 return ((Component)uo).GetComponentsInChildren<T>(); 131 } 132 else 133 { 134 throw new NotSupportedException(); 135 } 136 } 137 138 public static T[] GetComponentsInParent<T>(this UnityObject uo) 139 { 140 if (uo is GameObject) 141 { 142 return ((GameObject)uo).GetComponentsInParent<T>(); 143 } 144 else if (uo is Component) 145 { 146 return ((Component)uo).GetComponentsInParent<T>(); 147 } 148 else 149 { 150 throw new NotSupportedException(); 151 } 152 } 153 154 public static Component GetComponent(this UnityObject uo, Type type) 155 { 156 if (uo is GameObject) 157 { 158 return ((GameObject)uo).GetComponent(type); 159 } 160 else if (uo is Component) 161 { 162 return ((Component)uo).GetComponent(type); 163 } 164 else 165 { 166 throw new NotSupportedException(); 167 } 168 } 169 170 public static Component GetComponentInChildren(this UnityObject uo, Type type) 171 { 172 if (uo is GameObject) 173 { 174 return ((GameObject)uo).GetComponentInChildren(type); 175 } 176 else if (uo is Component) 177 { 178 return ((Component)uo).GetComponentInChildren(type); 179 } 180 else 181 { 182 throw new NotSupportedException(); 183 } 184 } 185 186 public static Component GetComponentInParent(this UnityObject uo, Type type) 187 { 188 if (uo is GameObject) 189 { 190 return ((GameObject)uo).GetComponentInParent(type); 191 } 192 else if (uo is Component) 193 { 194 return ((Component)uo).GetComponentInParent(type); 195 } 196 else 197 { 198 throw new NotSupportedException(); 199 } 200 } 201 202 public static Component[] GetComponents(this UnityObject uo, Type type) 203 { 204 if (uo is GameObject) 205 { 206 return ((GameObject)uo).GetComponents(type); 207 } 208 else if (uo is Component) 209 { 210 return ((Component)uo).GetComponents(type); 211 } 212 else 213 { 214 throw new NotSupportedException(); 215 } 216 } 217 218 public static Component[] GetComponentsInChildren(this UnityObject uo, Type type) 219 { 220 if (uo is GameObject) 221 { 222 return ((GameObject)uo).GetComponentsInChildren(type); 223 } 224 else if (uo is Component) 225 { 226 return ((Component)uo).GetComponentsInChildren(type); 227 } 228 else 229 { 230 throw new NotSupportedException(); 231 } 232 } 233 234 public static Component[] GetComponentsInParent(this UnityObject uo, Type type) 235 { 236 if (uo is GameObject) 237 { 238 return ((GameObject)uo).GetComponentsInParent(type); 239 } 240 else if (uo is Component) 241 { 242 return ((Component)uo).GetComponentsInParent(type); 243 } 244 else 245 { 246 throw new NotSupportedException(); 247 } 248 } 249 } 250}