SHARE
TWEET

Untitled

a guest May 24th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // add some randomization features
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class ArrayModifierWithZ : MonoBehaviour
  7. {
  8.  
  9.    
  10.     [SerializeField]
  11.     private int countX;
  12.     [SerializeField]
  13.     private int countY;
  14.     [SerializeField]
  15.     private int countZ;
  16.  
  17.     [SerializeField]
  18.     private int offsetX;
  19.     [SerializeField]
  20.     private int offsetY;
  21.     [SerializeField]
  22.     private int offsetZ;
  23.  
  24.     [SerializeField]
  25.     public GameObject gObject;
  26.  
  27.     private void OnValidate()
  28.     {
  29.         Apply();
  30.     }
  31.  
  32.     private void Apply()
  33.     {
  34.         //only exe in editor, leave
  35.         if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
  36.         {
  37.             print("not supported while playing");
  38.             return;
  39.         }
  40.  
  41.         if(gObject == null)
  42.         {
  43.             print("add a gameobject");
  44.             return;
  45.         }
  46.  
  47.         if (gObject.GetComponent<MeshRenderer>() != null)
  48.         {
  49.             MeshRenderer rend = gObject.GetComponent<MeshRenderer>();
  50.             print("Object uses MeshRenderer");
  51.         }
  52.         else if(gObject.GetComponent<Renderer>() != null)
  53.         {
  54.             Renderer rend = gObject.GetComponent<Renderer>();
  55.             print("Object uses Renderer");
  56.         }
  57.         else
  58.         {
  59.             print("Error no renderers");
  60.         }
  61.  
  62.          
  63.         if(rend != null)
  64.         {
  65.             foreach (Transform _tran in transform)
  66.             {
  67.                 //Destroy all children
  68.                 UnityEditor.EditorApplication.delayCall += () =>
  69.                 {
  70.                     DestroyImmediate(_tran.gameObject);
  71.                 };
  72.             }
  73.         }//end if
  74.  
  75.             float lastX = 0;
  76.             float lastY = 0;
  77.             float lastZ = 0;
  78.  
  79.             for(int i = 0; i < countZ; i++)
  80.             {
  81.                 for (int i2 = 0; i2 < countY; i2++)
  82.                 {
  83.                     for (int i3 = 0; i3 < countX; i3++)
  84.                     {
  85.                         //Position of the object to create
  86.                         Vector3 pos = new Vector3(lastX + transform.localPosition.x,
  87.                                                   lastY + transform.localPosition.y,
  88.                                                   lastZ + transform.localPosition.z);
  89.  
  90.                         // Create object
  91.                         GameObject go = Instantiate(gObject, pos, Quaternion.identity, transform) as GameObject;
  92.                         go.name = gObject.name + "_" + i + "_" + i2 + "_" + i3;
  93.  
  94.                         //add offset to this
  95.                         lastX += rend.bounds.size.x + offsetX;
  96.  
  97.                     print("loop i3 working");
  98.                     }//end for i3
  99.  
  100.                     //Rest the X-Axis position
  101.                     lastX = 0;
  102.  
  103.                     lastY += rend.bounds.size.y + offsetY;
  104.  
  105.                 print("loop i2 working");
  106.                 }//end for i2
  107.  
  108.                 //Rest the X-Axis position
  109.                 lastY = 0;
  110.  
  111.                 lastZ += rend.bounds.size.z + offsetZ;
  112.  
  113.             print("loop i working");
  114.             }//end for i
  115.     }//end Apply
  116.  
  117.  
  118.    
  119. }//end class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top