-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathGOCreationCommands.cs
More file actions
275 lines (237 loc) · 10.9 KB
/
GOCreationCommands.cs
File metadata and controls
275 lines (237 loc) · 10.9 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Linq;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.Video;
namespace UnityEditor
{
static class GOCreationCommands
{
internal static void Place(GameObject go, GameObject parent)
{
if (parent != null)
{
var transform = go.transform;
Undo.SetTransformParent(transform, parent.transform, "Reparenting");
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
transform.localScale = Vector3.one;
go.layer = parent.layer;
if (parent.GetComponent<RectTransform>())
ObjectFactory.AddComponent<RectTransform>(go);
}
else
{
SceneView.PlaceGameObjectInFrontOfSceneView(go);
StageUtility.PlaceGameObjectInCurrentStage(go); // may change parent
}
// Only at this point do we know the actual parent of the object and can mopdify its name accordingly.
GameObjectUtility.EnsureUniqueNameForSibling(go);
Undo.SetCurrentGroupName("Create " + go.name);
EditorWindow.FocusWindowIfItsOpen<SceneHierarchyWindow>();
Selection.activeGameObject = go;
}
[MenuItem("GameObject/Create Empty %#n", priority = 0)]
static void CreateEmpty(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
Place(ObjectFactory.CreateGameObject("GameObject"), parent);
}
[MenuItem("GameObject/Create Empty Child &#n", priority = 0)]
static void CreateEmptyChild(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
if (parent == null)
{
var activeGO = Selection.activeGameObject;
if (activeGO != null && !EditorUtility.IsPersistent(activeGO))
parent = activeGO;
}
var go = ObjectFactory.CreateGameObject("GameObject");
Place(go, parent);
}
static void CreateAndPlacePrimitive(PrimitiveType type, GameObject parent)
{
var primitive = ObjectFactory.CreatePrimitive(type);
primitive.name = type.ToString();
Place(primitive, parent);
}
[MenuItem("GameObject/3D Object/Cube", priority = 1)]
static void CreateCube(MenuCommand menuCommand)
{
CreateAndPlacePrimitive(PrimitiveType.Cube, menuCommand.context as GameObject);
}
[MenuItem("GameObject/3D Object/Sphere", priority = 2)]
static void CreateSphere(MenuCommand menuCommand)
{
CreateAndPlacePrimitive(PrimitiveType.Sphere, menuCommand.context as GameObject);
}
[MenuItem("GameObject/3D Object/Capsule", priority = 3)]
static void CreateCapsule(MenuCommand menuCommand)
{
CreateAndPlacePrimitive(PrimitiveType.Capsule, menuCommand.context as GameObject);
}
[MenuItem("GameObject/3D Object/Cylinder", priority = 4)]
static void CreateCylinder(MenuCommand menuCommand)
{
CreateAndPlacePrimitive(PrimitiveType.Cylinder, menuCommand.context as GameObject);
}
[MenuItem("GameObject/3D Object/Plane", priority = 5)]
static void CreatePlane(MenuCommand menuCommand)
{
CreateAndPlacePrimitive(PrimitiveType.Plane, menuCommand.context as GameObject);
}
[MenuItem("GameObject/3D Object/Quad", priority = 6)]
static void CreateQuad(MenuCommand menuCommand)
{
CreateAndPlacePrimitive(PrimitiveType.Quad, menuCommand.context as GameObject);
}
[MenuItem("GameObject/2D Object/Sprite", priority = 1)]
static void CreateSprite(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
var go = ObjectFactory.CreateGameObject("New Sprite", typeof(SpriteRenderer));
var sprite = Selection.activeObject as Sprite;
if (sprite == null)
{
var texture = Selection.activeObject as Texture2D;
if (texture)
{
string path = AssetDatabase.GetAssetPath(texture);
sprite = AssetDatabase.LoadAllAssetsAtPath(path)
.OfType<Sprite>()
.FirstOrDefault();
if (sprite == null)
{
var importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer != null && importer.textureType != TextureImporterType.Sprite)
{
EditorUtility.DisplayDialog(
"Sprite could not be assigned!",
"Can not assign a Sprite to the new SpriteRenderer because the selected Texture is not configured to generate Sprites.",
"OK");
}
}
}
}
if (sprite != null)
{
go.GetComponent<SpriteRenderer>().sprite = sprite;
}
else
{
// TODO: assign a default sprite
}
Place(go, parent);
}
[MenuItem("GameObject/Light/Directional Light", priority = 1)]
static void CreateDirectionalLight(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
var go = ObjectFactory.CreateGameObject("Directional Light", typeof(Light));
go.GetComponent<Light>().type = LightType.Directional;
go.GetComponent<Light>().intensity = 1f;
go.GetComponent<Transform>().SetLocalEulerAngles(new Vector3(50, -30, 0), RotationOrder.OrderZXY);
Place(go, parent);
}
[MenuItem("GameObject/Light/Point Light", priority = 2)]
static void CreatePointLight(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
var go = ObjectFactory.CreateGameObject("Point Light", typeof(Light));
go.GetComponent<Light>().type = LightType.Point;
Place(go, parent);
}
[MenuItem("GameObject/Light/Spotlight", priority = 3)]
static void CreateSpotLight(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
var go = ObjectFactory.CreateGameObject("Spot Light", typeof(Light));
go.GetComponent<Light>().type = LightType.Spot;
go.GetComponent<Transform>().SetLocalEulerAngles(new Vector3(90, 0, 0), RotationOrder.OrderZXY);
Place(go, parent);
}
[MenuItem("GameObject/Light/Area Light", priority = 4)]
static void CreateAreaLight(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
var go = ObjectFactory.CreateGameObject("Area Light", typeof(Light));
go.GetComponent<Light>().type = LightType.Rectangle;
go.GetComponent<Light>().shadows = LightShadows.Soft;
go.GetComponent<Transform>().SetLocalEulerAngles(new Vector3(90, 0, 0), RotationOrder.OrderZXY);
Place(go, parent);
}
[MenuItem("GameObject/Light/Reflection Probe", priority = 20)]
static void CreateReflectionProbe(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
Place(ObjectFactory.CreateGameObject("Reflection Probe", typeof(ReflectionProbe)), parent);
}
[MenuItem("GameObject/Light/Light Probe Group", priority = 21)]
static void CreateLightProbeGroup(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
Place(ObjectFactory.CreateGameObject("Light Probe Group", typeof(LightProbeGroup)), parent);
}
[MenuItem("GameObject/Audio/Audio Source", priority = 1)]
static void CreateAudioSource(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
Place(ObjectFactory.CreateGameObject("Audio Source", typeof(AudioSource)), parent);
}
[MenuItem("GameObject/Audio/Audio Reverb Zone", priority = 2)]
static void CreateAudioReverbZone(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
Place(ObjectFactory.CreateGameObject("Audio Reverb Zone", typeof(AudioReverbZone)), parent);
}
[MenuItem("GameObject/Video/Video Player", priority = 1)]
static void CreateVideoPlayer(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
Place(ObjectFactory.CreateGameObject("Video Player", typeof(VideoPlayer)), parent);
}
[MenuItem("GameObject/Effects/Particle System", priority = 1)]
static void CreateParticleSystem(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
var go = ObjectFactory.CreateGameObject("Particle System", typeof(ParticleSystem));
go.GetComponent<Transform>().SetLocalEulerAngles(new Vector3(-90, 0, 0), RotationOrder.OrderZXY);
var renderer = go.GetComponent<ParticleSystemRenderer>();
renderer.materials = new Material[]
{
Material.GetDefaultParticleMaterial(),
null
};
Place(go, parent);
}
[MenuItem("GameObject/Effects/Trail", priority = 2)]
static void CreateTrail(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
var go = ObjectFactory.CreateGameObject("Trail", typeof(TrailRenderer));
go.GetComponent<TrailRenderer>().material = Material.GetDefaultLineMaterial();
Place(go, parent);
}
[MenuItem("GameObject/Effects/Line", priority = 3)]
static void CreateLine(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
var go = ObjectFactory.CreateGameObject("Line", typeof(LineRenderer));
var line = go.GetComponent<LineRenderer>();
line.material = Material.GetDefaultLineMaterial();
line.widthMultiplier = 0.1f;
line.useWorldSpace = false;
Place(go, parent);
}
[MenuItem("GameObject/Camera", priority = 11)]
static void CreateCamera(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
Place(ObjectFactory.CreateGameObject("Camera", typeof(Camera), typeof(AudioListener)), parent);
}
}
}