using System; using UnityEngine; public static class GameObjectExtension { public static T GetOrAddComponent(this GameObject self) where T : Component { T t = self.GetComponent(); if (t == null) { t = self.AddComponent(); } return t; } public static T GetOrAddComponent(this Component self) where T : Component { T t = self.GetComponent(); if (t == null) { t = self.gameObject.AddComponent(); } return t; } }