Advertisement
Guest User

Untitled

a guest
May 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 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.         if (useRandomPos)
  41.         {
  42.             offset = GenerateRandomV3();
  43.         }
  44.  
  45.         //only exe in editor, leave
  46.         if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
  47.         {
  48.             Debug.Log("not supported while playing");
  49.             return;
  50.         }
  51.  
  52.         if (Source == null)
  53.         {
  54.             Debug.Log("add a GameObject");
  55.             return;
  56.         }
  57.  
  58.         Renderer rend = Source.GetComponent<Renderer>();
  59.         //Cache transform so it's not constantly accessed
  60.         Transform transform = this.transform;
  61.  
  62.        
  63.         if (rend != null)
  64.         {
  65.             bounds = rend.bounds.size;
  66.         }
  67.  
  68.  
  69.         int childCount = transform.childCount;
  70.         if (Source != PreviousSource)
  71.         {
  72.             foreach (Transform child in transform)
  73.             {
  74.                 //Destroy all children
  75.                 UnityEditor.EditorApplication.delayCall += () => { DestroyImmediate(child.gameObject); };
  76.             }
  77.             PreviousSource = Source;
  78.  
  79.             childCount = 0;
  80.         }
  81.         else
  82.         {
  83.             //Destroy extra children
  84.             int maxchildCount = count.x * count.y * count.z;
  85.             for (int i = transform.childCount - 1; i >= maxchildCount; i--)
  86.             {
  87.                 GameObject destroy = transform.GetChild(i).gameObject;
  88.                 UnityEditor.EditorApplication.delayCall += () => { DestroyImmediate(destroy); };
  89.             }
  90.  
  91.             childCount = Mathf.Min(maxchildCount, childCount);
  92.         }
  93.  
  94.         int childIndex = 0;
  95.         Vector3 parentPosition = transform.localPosition;
  96.  
  97.         for (int i = 0; i < count.z; i++)
  98.         {
  99.             float z = i * (bounds.z + offset.z);
  100.             for (int i2 = 0; i2 < count.y; i2++)
  101.             {
  102.                 float y = i2 * (bounds.y + offset.y);
  103.                 for (int i3 = 0; i3 < count.x; i3++)
  104.                 {
  105.                     float x = i3 * (bounds.x + offset.x);
  106.  
  107.                     //Position of the object to create
  108.                     Vector3 childPos = new Vector3(x, y, z);
  109.  
  110.  
  111.                     if (childIndex < childCount)
  112.                     {
  113.                         //If we already have a child then we should use it.
  114.                         Transform child = transform.GetChild(childIndex);
  115.  
  116.                         child.localPosition = childPos;
  117.                         child.localRotation = Quaternion.identity;
  118.                         child.name = $"{Source.name} ({i},{i2},{i3})";
  119.                     }
  120.                     else
  121.                     {
  122.                             //Otherwise there is no child for this index and we need to create a new one
  123.                             GameObject go = Instantiate(Source, parentPosition + childPos, Quaternion.identity, transform);
  124.                             go.name = $"{Source.name} ({i},{i2},{i3})";
  125.                     }
  126.  
  127.  
  128.                     childIndex++;
  129.                 }//end i3
  130.  
  131.             }//end i2
  132.  
  133.         }//end i
  134.     }//end apply
  135. #endif
  136.  
  137.     public Vector3 GenerateRandomV3()
  138.     {
  139.         float RandomX = Random.Range(posRangeX.x, posRangeX.y);
  140.         float RandomY = Random.Range(posRangeY.x, posRangeY.y);
  141.         float RandomZ = Random.Range(posRangeZ.x, posRangeZ.y);
  142.  
  143.         posVec3 = new Vector3(RandomX, RandomY, RandomZ);
  144.  
  145.         print(posVec3);
  146.  
  147.         return posVec3;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement