Pro_Unit

PrefabPlanePlacer

Oct 28th, 2020
748
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. public class PrefabPlanePlacer : MonoBehaviour
  4. {
  5.     [SerializeField] private GameObject _prefab;
  6.     [SerializeField] Vector2Int _rowsColums = Vector2Int.one;
  7.     [SerializeField] Vector2 _offset = Vector2.one;
  8.  
  9.     [ContextMenu("InstatiateAsPrefab")]
  10.     void InstatiateAsPrefab()
  11.     {
  12.  
  13. #if UNITY_EDITOR
  14.         for (int x = 0; x < _rowsColums.x; x++)
  15.         {
  16.             for (int y = 0; y < _rowsColums.y; y++)
  17.             {
  18.                 var instance = (UnityEditor.PrefabUtility.InstantiatePrefab(_prefab) as GameObject).transform;
  19.  
  20.                 instance.SetParent(transform);
  21.  
  22.                 Vector3 offset = new Vector3(_offset.x, 0, _offset.y);
  23.  
  24.                 instance.localPosition = Vector3.Scale(new Vector3(x, 0, y), offset);
  25.             }
  26.         }
  27. #endif
  28.     }
  29. }
  30.  
RAW Paste Data