Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class ImprovedArrayModifier : MonoBehaviour
- {
- [SerializeField] private Vector3Int count = Vector3Int.one;
- [SerializeField] private Vector3 offset = Vector3.zero;
- [SerializeField, HideInInspector] private GameObject PreviousSource;
- public GameObject Source;
- [SerializeField]
- private bool useRandomPos = false;
- private Vector3 posVec3;
- [SerializeField]
- private Vector2 posRangeX;
- [SerializeField]
- private Vector2 posRangeY;
- [SerializeField]
- private Vector2 posRangeZ;
- public int posSeed;
- Vector3 bounds = Vector3.zero;
- //Wrap in this because we have editor calls that we cannot include in the build.
- #if UNITY_EDITOR
- private void OnValidate()
- {
- //Ensure that count is positive
- count = new Vector3Int(Mathf.Max(0, count.x), Mathf.Max(0, count.y), Mathf.Max(0, count.z));
- Random.InitState(posSeed);
- Apply();
- }
- private void Apply()
- {
- /*
- if (useRandomPos)
- {
- offset = GenerateRandomV3();
- }
- */
- //only exe in editor, leave
- if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
- {
- Debug.Log("not supported while playing");
- return;
- }
- if (Source == null)
- {
- Debug.Log("add a GameObject");
- return;
- }
- Renderer rend = Source.GetComponent<Renderer>();
- //Cache transform so it's not constantly accessed
- Transform transform = this.transform;
- if (rend != null)
- {
- bounds = rend.bounds.size;
- }
- int childCount = transform.childCount;
- if (Source != PreviousSource)
- {
- foreach (Transform child in transform)
- {
- //Destroy all children
- UnityEditor.EditorApplication.delayCall += () => { DestroyImmediate(child.gameObject); };
- }
- PreviousSource = Source;
- childCount = 0;
- }
- else
- {
- //Destroy extra children
- int maxchildCount = count.x * count.y * count.z;
- for (int i = transform.childCount - 1; i >= maxchildCount; i--)
- {
- GameObject destroy = transform.GetChild(i).gameObject;
- UnityEditor.EditorApplication.delayCall += () => { DestroyImmediate(destroy); };
- }
- childCount = Mathf.Min(maxchildCount, childCount);
- }
- int childIndex = 0;
- Vector3 parentPosition = transform.localPosition;
- for (int i = 0; i < count.z; i++)
- {
- float z = i * (bounds.z + offset.z);
- for (int i2 = 0; i2 < count.y; i2++)
- {
- float y = i2 * (bounds.y + offset.y);
- for (int i3 = 0; i3 < count.x; i3++)
- {
- float x = i3 * (bounds.x + offset.x);
- //Position of the object to create
- Vector3 childPos = new Vector3(x, y, z);
- if (childIndex < childCount)
- {
- //If we already have a child then we should use it.
- Transform child = transform.GetChild(childIndex);
- if (useRandomPos)
- {
- child.localPosition = GenerateRandomV3();
- }
- else
- {
- child.localPosition = childPos;
- }
- child.localRotation = Quaternion.identity;
- child.name = $"{Source.name} ({i},{i2},{i3})";
- }
- else
- {
- //Otherwise there is no child for this index and we need to create a new one
- GameObject go = Instantiate(Source, parentPosition + childPos, Quaternion.identity, transform);
- go.name = $"{Source.name} ({i},{i2},{i3})";
- }
- childIndex++;
- }//end i3
- }//end i2
- }//end i
- }//end apply
- #endif
- public Vector3 GenerateRandomV3()
- {
- float RandomX = Random.Range(posRangeX.x, posRangeX.y);
- float RandomY = Random.Range(posRangeY.x, posRangeY.y);
- float RandomZ = Random.Range(posRangeZ.x, posRangeZ.y);
- posVec3 = new Vector3(RandomX, RandomY, RandomZ);
- print(posVec3);
- return posVec3;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement