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