From 766cdff5ffa72b65d7f106658d1603f47739b2ba Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 27 Oct 2023 11:05:14 +0800 Subject: + init --- GameCode/Populate.cs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 GameCode/Populate.cs (limited to 'GameCode/Populate.cs') 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 DoPopulate() + { + List list = new List(); + 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 DoPopulate(bool addComponentIfMissing = true) where T : MonoBehaviour + { + List list = new List(); + if (includeTargetInList && target != null) + { + T val = target.GetComponent(); + if ((Object)val == (Object)null && addComponentIfMissing) + { + val = target.AddComponent(); + } + 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(); + if ((Object)val2 == (Object)null && addComponentIfMissing) + { + val2 = gameObject.AddComponent(); + } + if ((Object)val2 != (Object)null) + { + list.Add(val2); + } + if (setTargetsActive) + { + gameObject.SetActive(value: true); + } + } + return list; + } +} -- cgit v1.1-26-g67d0