Advertisement
Guest User

Untitled

a guest
May 24th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.69 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class ImprovedArrayModifier : MonoBehaviour
  4. {
  5.  
  6.     [SerializeField] private Vector3Int count = Vector3Int.one;
  7.     [SerializeField] private Vector3 offset = Vector3.zero;
  8.     [SerializeField, HideInInspector] private GameObject PreviousSource;
  9.     public GameObject Source;
  10.     [SerializeField]
  11.     private bool useRandomPos = false;
  12.  
  13.     private Vector3 posVec3;
  14.  
  15.     [SerializeField]
  16.     private Vector2 posRangeX;
  17.     [SerializeField]
  18.     private Vector2 posRangeY;
  19.     [SerializeField]
  20.     private Vector2 posRangeZ;
  21.  
  22.     public int posSeed;
  23.  
  24.     Vector3 bounds = Vector3.zero;
  25.  
  26.     //Wrap in this because we have editor calls that we cannot include in the build.
  27. #if UNITY_EDITOR
  28.     private void OnValidate()
  29.     {
  30.         //Ensure that count is positive
  31.         count = new Vector3Int(Mathf.Max(0, count.x), Mathf.Max(0, count.y), Mathf.Max(0, count.z));
  32.  
  33.         Random.InitState(posSeed);
  34.  
  35.         Apply();
  36.     }
  37.  
  38.     private void Apply()
  39.     {
  40.         /*
  41.         if (useRandomPos)
  42.         {
  43.             offset = GenerateRandomV3();
  44.         }
  45.         */
  46.         //only exe in editor, leave
  47.         if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
  48.         {
  49.             Debug.Log("not supported while playing");
  50.             return;
  51.         }
  52.  
  53.         if (Source == null)
  54.         {
  55.             Debug.Log("add a GameObject");
  56.             return;
  57.         }
  58.  
  59.         Renderer rend = Source.GetComponent<Renderer>();
  60.         //Cache transform so it's not constantly accessed
  61.         Transform transform = this.transform;
  62.  
  63.        
  64.         if (rend != null)
  65.         {
  66.             bounds = rend.bounds.size;
  67.         }
  68.  
  69.  
  70.         int childCount = transform.childCount;
  71.         if (Source != PreviousSource)
  72.         {
  73.             foreach (Transform child in transform)
  74.             {
  75.                 //Destroy all children
  76.                 UnityEditor.EditorApplication.delayCall += () => { DestroyImmediate(child.gameObject); };
  77.             }
  78.             PreviousSource = Source;
  79.  
  80.             childCount = 0;
  81.         }
  82.         else
  83.         {
  84.             //Destroy extra children
  85.             int maxchildCount = count.x * count.y * count.z;
  86.             for (int i = transform.childCount - 1; i >= maxchildCount; i--)
  87.             {
  88.                 GameObject destroy = transform.GetChild(i).gameObject;
  89.                 UnityEditor.EditorApplication.delayCall += () => { DestroyImmediate(destroy); };
  90.             }
  91.  
  92.             childCount = Mathf.Min(maxchildCount, childCount);
  93.         }
  94.  
  95.         int childIndex = 0;
  96.         Vector3 parentPosition = transform.localPosition;
  97.  
  98.         for (int i = 0; i < count.z; i++)
  99.         {
  100.             float z = i * (bounds.z + offset.z);
  101.             for (int i2 = 0; i2 < count.y; i2++)
  102.             {
  103.                 float y = i2 * (bounds.y + offset.y);
  104.                 for (int i3 = 0; i3 < count.x; i3++)
  105.                 {
  106.                     float x = i3 * (bounds.x + offset.x);
  107.  
  108.                     //Position of the object to create
  109.                     Vector3 childPos = new Vector3(x, y, z);
  110.  
  111.  
  112.                     if (childIndex < childCount)
  113.                     {
  114.                         //If we already have a child then we should use it.
  115.                         Transform child = transform.GetChild(childIndex);
  116.  
  117.                         if (useRandomPos)
  118.                         {
  119.                             child.localPosition = GenerateRandomV3();
  120.                         }
  121.                         else
  122.                         {
  123.                             child.localPosition = childPos;
  124.                         }
  125.                        
  126.                         child.localRotation = Quaternion.identity;
  127.                         child.name = $"{Source.name} ({i},{i2},{i3})";
  128.                     }
  129.                     else
  130.                     {
  131.                             //Otherwise there is no child for this index and we need to create a new one
  132.                             GameObject go = Instantiate(Source, parentPosition + childPos, Quaternion.identity, transform);
  133.                             go.name = $"{Source.name} ({i},{i2},{i3})";
  134.                     }
  135.  
  136.  
  137.                     childIndex++;
  138.                 }//end i3
  139.  
  140.             }//end i2
  141.  
  142.         }//end i
  143.     }//end apply
  144. #endif
  145.  
  146.     public Vector3 GenerateRandomV3()
  147.     {
  148.         float RandomX = Random.Range(posRangeX.x, posRangeX.y);
  149.         float RandomY = Random.Range(posRangeY.x, posRangeY.y);
  150.         float RandomZ = Random.Range(posRangeZ.x, posRangeZ.y);
  151.  
  152.         posVec3 = new Vector3(RandomX, RandomY, RandomZ);
  153.  
  154.         print(posVec3);
  155.  
  156.         return posVec3;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement