diff options
author | chai <215380520@qq.com> | 2023-10-27 11:05:14 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-10-27 11:05:14 +0800 |
commit | 766cdff5ffa72b65d7f106658d1603f47739b2ba (patch) | |
tree | 34d7799a94dfa9be182825577583c0fa6dc935f7 /GameCode/Populate.cs |
+ init
Diffstat (limited to 'GameCode/Populate.cs')
-rw-r--r-- | GameCode/Populate.cs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/GameCode/Populate.cs b/GameCode/Populate.cs new file mode 100644 index 0000000..b59ce59 --- /dev/null +++ b/GameCode/Populate.cs @@ -0,0 +1,71 @@ +using System.Collections.Generic; +using UnityEngine; + +public class Populate : MonoBehaviour +{ + public GameObject target; + + public bool setTargetsActive; + + public bool includeTargetInList = true; + + public int times = 5; + + public List<GameObject> DoPopulate() + { + List<GameObject> list = new List<GameObject>(); + if (includeTargetInList) + { + list.Add(target); + } + for (int i = 0; i < times; i++) + { + GameObject gameObject = Object.Instantiate(target, target.transform.position, base.transform.transform.rotation, base.transform); + gameObject.transform.localScale = target.transform.localScale; + list.Add(gameObject); + if (setTargetsActive) + { + gameObject.SetActive(value: true); + } + } + return list; + } + + public List<T> DoPopulate<T>(bool addComponentIfMissing = true) where T : MonoBehaviour + { + List<T> list = new List<T>(); + if (includeTargetInList && target != null) + { + T val = target.GetComponent<T>(); + if ((Object)val == (Object)null && addComponentIfMissing) + { + val = target.AddComponent<T>(); + } + if (!((Object)val != (Object)null)) + { + Debug.LogError("Could not find component"); + return null; + } + list.Add(val); + } + for (int i = 0; i < times; i++) + { + GameObject gameObject = Object.Instantiate(target, target.transform.position, base.transform.transform.rotation, target.transform.transform.parent); + gameObject.transform.localScale = target.transform.localScale; + T val2 = gameObject.GetComponent<T>(); + if ((Object)val2 == (Object)null && addComponentIfMissing) + { + val2 = gameObject.AddComponent<T>(); + } + if ((Object)val2 != (Object)null) + { + list.Add(val2); + } + if (setTargetsActive) + { + gameObject.SetActive(value: true); + } + } + return list; + } +} |