-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlacementToolsWindow.cs
166 lines (134 loc) · 4.9 KB
/
PlacementToolsWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (C) 2021 Jerry Verhoeven - All Rights Reserved
* You may use, distribute and modify this code under the terms of the MIT license.
*/
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
[System.Serializable]
public class PlacementToolsWindow : EditorWindow
{
#region Properties
//-----------------------------------------------------------------
//public bool MyProperty {get; set;}
//public bool MyProperty { get { return x; } set { x = value; } }
//-----------------------------------------------------------------
#endregion Properties
#region Members
//-----------------------------------------------------------------
[SerializeField]
private Transform m_OriginTransform;
[SerializeField]
private float m_MaxColumnCount = 10;
[SerializeField]
private float m_OffsetColumn = 1;
[SerializeField]
private float m_OffsetRow = 1;
[SerializeField]
private int m_SingleSpawnCount = 1;
[SerializeField]
private Vector3 m_InitialRotation = Vector3.zero;
//-----------------------------------------------------------------
#endregion Members
#region Unity Overloads
//-----------------------------------------------------------------
[MenuItem("Tools/Placement Tools...", false, 0)]
private static void Init()
{
//create new window
EditorWindow.GetWindow<PlacementToolsWindow>(false, "Placement Tools", true);
}
private void OnGUI()
{
if (!this)
return;
this.titleContent = new GUIContent("Place Tools", EditorGUIUtility.FindTexture("ToolHandleLocal"));
EditorGUILayout.BeginVertical();
{
m_OriginTransform = EditorGUILayout.ObjectField("Placement Origin", m_OriginTransform, typeof(Transform), true) as Transform;
m_MaxColumnCount = EditorGUILayout.FloatField("Max Column Count", m_MaxColumnCount);
m_OffsetColumn = EditorGUILayout.FloatField("Column Offset", m_OffsetColumn);
m_OffsetRow = EditorGUILayout.FloatField("Row Offset", m_OffsetRow);
m_SingleSpawnCount = EditorGUILayout.IntField("Spawn Count", m_SingleSpawnCount);
m_InitialRotation = EditorGUILayout.Vector3Field("Initial Rotation", m_InitialRotation);
List<Object> selectedPrefabsList = new List<Object>();
{
Object[] selectedAssetsArr = Selection.GetFiltered(typeof(Object), SelectionMode.Assets | SelectionMode.TopLevel);
foreach (Object obj in selectedAssetsArr)
{
PrefabAssetType prefabAssetType = PrefabUtility.GetPrefabAssetType(obj);
if (prefabAssetType != PrefabAssetType.NotAPrefab)
{
selectedPrefabsList.Add(obj);
}
else
{
//Debug.LogWarning("Object: " + obj.name + " is not a valid prefab. Type: " + prefabType.ToString());
}
}
}
EditorGUILayout.LabelField("Selected Prefabs: " + selectedPrefabsList.Count, GUILayout.ExpandWidth(true), GUILayout.MinWidth(0));
if (selectedPrefabsList.Count <= 0)
{
GUI.enabled = false;
}
if (GUILayout.Button("Instantiate"))
{
InstantiatePrefabs(selectedPrefabsList);
}
if (selectedPrefabsList.Count <= 0)
{
GUI.enabled = true;
}
}
EditorGUILayout.EndVertical();
}
//-----------------------------------------------------------------
#endregion Unity Overloads
#region Public Interface
//-----------------------------------------------------------------
//public void MyFunction()
//{
//
//}
//-----------------------------------------------------------------
#endregion Public Interface
#region Private Interface
//-----------------------------------------------------------------
private void InstantiatePrefabs(IEnumerable<Object> selectedPrefabsList)
{
List<GameObject> selectedPrefabGameObjectsList = selectedPrefabsList.Select(x => EditorUtility.InstanceIDToObject(x.GetInstanceID()) as GameObject).Where(x => (x != null)).OrderBy(x => x.name).ToList();
int currentColumn = 0;
int currentRow = 0;
foreach (var selectedPrefabGameObject in selectedPrefabGameObjectsList)
{
for (int i = 0; i < m_SingleSpawnCount; i++)
{
Vector3 originPosition = m_OriginTransform.position;
{
originPosition.x += (currentColumn * m_OffsetColumn);
originPosition.z += (currentRow * m_OffsetRow);
}
GameObject instantiatedGameObject = PrefabUtility.InstantiatePrefab(selectedPrefabGameObject) as GameObject;
if (instantiatedGameObject != null)
{
Undo.RegisterCreatedObjectUndo(instantiatedGameObject, "Place Object");
instantiatedGameObject.transform.position = originPosition;
instantiatedGameObject.transform.rotation = Quaternion.Euler(m_InitialRotation);
//Vector3 originalScale = go.transform.localScale;
instantiatedGameObject.transform.SetParent(m_OriginTransform);
//go.transform.localScale = originalScale;
}
++currentColumn;
if (currentColumn > m_MaxColumnCount)
{
currentColumn = 0;
++currentRow;
}
}
}
}
//-----------------------------------------------------------------
#endregion Private Interface
}