99 lines
3.1 KiB
C#
99 lines
3.1 KiB
C#
using System.Collections.Generic;
|
|
|
|
|
|
# if UNITY_EDITOR
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
public class ArrayGeneratorWindow : EditorWindow
|
|
{
|
|
public int X = 0; // 行数
|
|
public int Y = 0; // 列数
|
|
public int Z = 0;
|
|
public float spacing = 2f; // 物体间距
|
|
public Vector3 offset = Vector3.zero; // 阵列中心偏移
|
|
private GameObject lastActive;
|
|
private List<GameObject> lastList = new();
|
|
|
|
[MenuItem("Tool/ArrayGenerator")]
|
|
public static void ShowWindow()
|
|
{
|
|
GetWindow<ArrayGeneratorWindow>("Array Generator");
|
|
}
|
|
|
|
void OnGUI()
|
|
{
|
|
X = EditorGUILayout.IntField("X", X);
|
|
Z = EditorGUILayout.IntField("Z", Z);
|
|
Y = EditorGUILayout.IntField("Y", Y);
|
|
spacing = EditorGUILayout.FloatField("Spacing", spacing);
|
|
offset = EditorGUILayout.Vector3Field("Offset", offset);
|
|
|
|
if (GUILayout.Button("Generate Square Array"))
|
|
{
|
|
GenerateSquareArray();
|
|
}
|
|
}
|
|
|
|
void GenerateSquareArray()
|
|
{
|
|
GameObject active = Selection.activeGameObject;
|
|
if(lastActive == active)
|
|
{
|
|
foreach(var item in lastList)
|
|
{
|
|
DestroyImmediate(item);
|
|
}
|
|
lastList.Clear();
|
|
}
|
|
lastActive = active;
|
|
|
|
for (int k = 1; k < Y; k++)
|
|
{
|
|
string name = string.Format("{0}-{1}-{2}","01","01", (k + 1).ToString("D2"));
|
|
GameObject targetLayer = Create(active,name);
|
|
// 计算位置(以父对象为中心)
|
|
float y = k * spacing;
|
|
targetLayer.transform.position += new Vector3(0, y + offset.y, 0);
|
|
}
|
|
for (int i = 1; i <= X; i++)
|
|
{
|
|
string name = "";
|
|
if (active.name.Contains("-"))
|
|
{
|
|
int column = int.Parse(active.name.Split('-')[1]);
|
|
name = string.Format("{0}-{1}-{2}", "01", (i+column).ToString("D2"), "01");
|
|
|
|
}
|
|
else
|
|
{
|
|
name = string.Format("{0}-{1}-{2}", "01", i.ToString("D2"), "01");
|
|
}
|
|
GameObject targetRow = Create(active,name);
|
|
// 计算位置(以父对象为中心)
|
|
float x = i * spacing;
|
|
targetRow.transform.position += new Vector3(x + offset.x, 0,0);
|
|
}
|
|
for (int i = 1; i <= Z; i++)
|
|
{
|
|
string name = string.Format("{0}-{1}-{2}", "01", (i + 1).ToString("D2"), "01");
|
|
GameObject targetRow = Create(active, name);
|
|
// 计算位置(以父对象为中心)
|
|
float x = i * spacing;
|
|
targetRow.transform.position += new Vector3(0, 0, x + offset.z);
|
|
}
|
|
|
|
}
|
|
private GameObject Create(GameObject active,string name)
|
|
{
|
|
GameObject target = GameObject.Instantiate(active);
|
|
target.transform.parent = active.transform.parent;
|
|
target.transform.name = name;
|
|
target.transform.position = active.transform.position;
|
|
target.transform.localScale = active.transform.localScale;
|
|
target.transform.localPosition = active.transform.localPosition;
|
|
lastList.Add(target);
|
|
return target;
|
|
|
|
}
|
|
}
|
|
# endif |