private static void AddParticleSystemSO() { SceneObject so = new SceneObject("Particle system"); GameObjectUndo.RecordNewSceneObject(so); so.AddComponent <ParticleSystem>(); GameObjectUndo.ResolveDiffs(); FocusOnHierarchyOrScene(); EditorApplication.SetSceneDirty(); }
private static void AddRenderableSO() { SceneObject so = new SceneObject("Renderable"); GameObjectUndo.RecordNewSceneObject(so); so.AddComponent <Renderable>(); GameObjectUndo.ResolveDiffs(); FocusOnHierarchyOrScene(); EditorApplication.SetSceneDirty(); }
private static void AddGUIWidgetSO() { SceneObject so = new SceneObject("GUI widget"); GameObjectUndo.RecordNewSceneObject(so); so.AddComponent <GUIWidget>(); GameObjectUndo.ResolveDiffs(); FocusOnHierarchyOrScene(); EditorApplication.SetSceneDirty(); }
/// <summary> /// Triggered when the user clicks the component remove button. Removes that component from the active scene object. /// </summary> /// <param name="componentType">Type of the component to remove.</param> private void OnComponentRemoveClicked(Type componentType) { if (activeSO != null) { GameObjectUndo.RecordSceneObject(activeSO, false, $"Removed component \"{componentType.Name}\" from \"{activeSO.Name}\""); activeSO.RemoveComponent(componentType); modifyState = InspectableState.Modified; EditorApplication.SetSceneDirty(); GameObjectUndo.ResolveDiffs(); } }
private static void AddDirectionalLightSO() { SceneObject so = new SceneObject("Directional light"); GameObjectUndo.RecordNewSceneObject(so); Light light = so.AddComponent <Light>(); light.Type = LightType.Directional; GameObjectUndo.ResolveDiffs(); FocusOnHierarchyOrScene(); EditorApplication.SetSceneDirty(); }
private static void Add3DDisc() { SceneObject so = new SceneObject("Disc"); GameObjectUndo.RecordNewSceneObject(so); Renderable renderable = so.AddComponent <Renderable>(); renderable.Mesh = Builtin.Disc; GameObjectUndo.ResolveDiffs(); FocusOnHierarchyOrScene(); EditorApplication.SetSceneDirty(); }
private static void AddDecal() { SceneObject so = Selection.SceneObject; if (so == null) { return; } GameObjectUndo.RecordSceneObject(so, false, "Added a Decal component"); so.AddComponent <Decal>(); GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }
private static void AddLightProbeVolume() { SceneObject so = Selection.SceneObject; if (so == null) { return; } GameObjectUndo.RecordSceneObject(so, false, "Added a Light Probe Volume component"); so.AddComponent <LightProbeVolume>(); GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }
private static void RevertToPrefab() { SceneObject so = Selection.SceneObject; if (so == null) { return; } GameObjectUndo.RecordSceneObject(so, true, "Reverting \"" + so.Name + "\" to prefab."); PrefabUtility.RevertPrefab(so); GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }
private static void AddCameraSO() { SceneObject so = new SceneObject("Camera"); GameObjectUndo.RecordNewSceneObject(so); Camera cam = so.AddComponent <Camera>(); cam.Main = true; GameObjectUndo.ResolveDiffs(); FocusOnHierarchyOrScene(); EditorApplication.SetSceneDirty(); }
private static void AddCamera() { SceneObject so = Selection.SceneObject; if (so == null) { return; } GameObjectUndo.RecordSceneObject(so, false, "Added a Camera component"); Camera cam = so.AddComponent <Camera>(); cam.Main = true; GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }
private static void AddDirectionalLight() { SceneObject so = Selection.SceneObject; if (so == null) { return; } GameObjectUndo.RecordSceneObject(so, false, "Added a Light component"); Light light = so.AddComponent <Light>(); light.Type = LightType.Directional; GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }
private static void AddAudioSource() { SceneObject so = Selection.SceneObject; if (so == null) { so = UndoRedo.CreateSO("AudioSource", "New scene object"); Selection.SceneObject = so; FocusOnHierarchyOrScene(); } GameObjectUndo.RecordSceneObject(so, false, "Added a AudioSource component"); so.AddComponent <AudioSource>(); GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }
private static void AddCharacterController() { SceneObject so = Selection.SceneObject; if (so == null) { so = UndoRedo.CreateSO("CharacterController", "New scene object"); Selection.SceneObject = so; FocusOnHierarchyOrScene(); } GameObjectUndo.RecordSceneObject(so, false, "Added a CharacterController component"); so.AddComponent <CharacterController>(); GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }
private static void AddPlaneCollider() { SceneObject so = Selection.SceneObject; if (so == null) { so = new SceneObject("PlaneCollider"); GameObjectUndo.RecordNewSceneObject(so); so.AddComponent <PlaneCollider>(); FocusOnHierarchyOrScene(); } else { GameObjectUndo.RecordSceneObject(so, false, "Added a PlaneCollider component"); so.AddComponent <PlaneCollider>(); } GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }
private static void AddSphericalJoint() { SceneObject so = Selection.SceneObject; if (so == null) { so = new SceneObject("SphericalJoint"); GameObjectUndo.RecordNewSceneObject(so); so.AddComponent <SphericalJoint>(); FocusOnHierarchyOrScene(); } else { GameObjectUndo.RecordSceneObject(so, false, "Added a SphericalJoint component"); so.AddComponent <SphericalJoint>(); } GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }
private static void AddAudioSource() { SceneObject so = Selection.SceneObject; if (so == null) { so = new SceneObject("AudioSource"); GameObjectUndo.RecordNewSceneObject(so); so.AddComponent <AudioSource>(); FocusOnHierarchyOrScene(); } else { GameObjectUndo.RecordSceneObject(so, false, "Added a AudioSource component"); so.AddComponent <AudioSource>(); } GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }
public static void Undo() { EditorWindow[] allWindows = EditorWindow.AllWindows; foreach (var window in allWindows) { if (!window.HasFocus) { continue; } UndoRedo localStack = window.UndoRedo; if (localStack == null) { continue; } localStack.Undo(); return; } GameObjectUndo.ResolveDiffs(); UndoRedo.Global.Undo(); }
/// <summary> /// Finishes recording an undo command started via <see cref="StartUndo"/>. If any changes are detected on the /// field an undo command is recorded onto the undo-redo stack, otherwise nothing is done. /// </summary> protected void EndUndo() { GameObjectUndo.ResolveDiffs(); }
/// <inheritdoc/> protected internal override void PostInput() { if (activeHandle != null) { if (activeHandle.IsDragged()) { if (!isDragged) { isDragged = true; SceneObject[] selectedSceneObjects = Selection.SceneObjects; GameObjectUndo.RecordSceneObjectHeader(selectedSceneObjects); activeSelection = new HandledObject[selectedSceneObjects.Length]; for (int i = 0; i < selectedSceneObjects.Length; i++) { activeSelection[i] = new HandledObject(selectedSceneObjects[i]); } initialHandlePosition = activeHandle.Position; initialHandleRotation = activeHandle.Rotation; } } else { if (isDragged) { GameObjectUndo.ResolveDiffs(); } isDragged = false; activeSelection = null; } activeHandle.PostInput(); if (activeHandle.IsDragged()) { switch (activeHandleType) { case SceneViewTool.Move: MoveHandle moveHandle = (MoveHandle)activeHandle; foreach (var selectedObj in activeSelection) { SceneObject parentSO = selectedObj.so.Parent; if (parentSO == null) { selectedObj.so.LocalPosition = selectedObj.initialPosition + moveHandle.Delta; } else { Vector3 parentRelativeDelta = parentSO.Rotation.Inverse.Rotate(moveHandle.Delta); selectedObj.so.LocalPosition = selectedObj.initialPosition + parentRelativeDelta; } } break; case SceneViewTool.Rotate: { RotateHandle rotateHandle = (RotateHandle)activeHandle; // Make sure we transform relative to the handle position SceneObject temporarySO = new SceneObject("Temp"); temporarySO.Position = initialHandlePosition; temporarySO.LocalRotation = initialHandleRotation; SceneObject[] originalParents = new SceneObject[activeSelection.Length]; for (int i = 0; i < activeSelection.Length; i++) { originalParents[i] = activeSelection[i].so.Parent; activeSelection[i].so.LocalPosition = activeSelection[i].initialPosition; activeSelection[i].so.LocalRotation = activeSelection[i].initialRotation; activeSelection[i].so.Parent = temporarySO; } temporarySO.LocalRotation *= rotateHandle.Delta; for (int i = 0; i < activeSelection.Length; i++) { activeSelection[i].so.Parent = originalParents[i]; } temporarySO.Destroy(); } break; case SceneViewTool.Scale: { ScaleHandle scaleHandle = (ScaleHandle)activeHandle; Vector3 origin = activeHandle.Position; for (int i = 0; i < activeSelection.Length; i++) { Vector3 posDiff = activeSelection[i].initialPosition - origin; Vector3 scale = activeSelection[i].initialScale; Vector3 newScale = scale + scaleHandle.Delta; Vector3 pctScale = new Vector3( newScale.x / scale.x, newScale.y / scale.y, newScale.z / scale.z); activeSelection[i].so.LocalScale = newScale; activeSelection[i].so.Position = origin + posDiff * pctScale; } } break; } SceneObject[] selectedSceneObjects = new SceneObject[activeSelection.Length]; for (int i = 0; i < activeSelection.Length; i++) { selectedSceneObjects[i] = activeSelection[i].so; } // Make sure to update handle positions for the drawing method (otherwise they lag one frame) UpdateActiveHandleTransform(selectedSceneObjects); EditorApplication.SetSceneDirty(); } } else { isDragged = false; activeSelection = null; } }
/// <summary> /// Updates contents of the scene object specific fields (name, position, rotation, etc.) /// </summary> /// <param name="forceUpdate">If true, the GUI elements will be updated regardless of whether a change was /// detected or not.</param> internal void RefreshSceneObjectFields(bool forceUpdate) { if (activeSO == null) { return; } soNameInput.Text = activeSO.Name; soActiveToggle.Value = activeSO.Active; soMobility.Value = (ulong)activeSO.Mobility; SceneObject prefabParent = PrefabUtility.GetPrefabParent(activeSO); // Ignore prefab parent if scene root, we only care for non-root prefab instances bool hasPrefab = prefabParent != null && prefabParent.Parent != null; if (soHasPrefab != hasPrefab || forceUpdate) { int numChildren = soPrefabLayout.ChildCount; for (int i = 0; i < numChildren; i++) { soPrefabLayout.GetChild(0).Destroy(); } GUILabel prefabLabel = new GUILabel(new LocEdString("Prefab"), GUIOption.FixedWidth(50)); soPrefabLayout.AddElement(prefabLabel); if (hasPrefab) { GUIButton btnApplyPrefab = new GUIButton(new LocEdString("Apply"), GUIOption.FixedWidth(60)); GUIButton btnRevertPrefab = new GUIButton(new LocEdString("Revert"), GUIOption.FixedWidth(60)); GUIButton btnBreakPrefab = new GUIButton(new LocEdString("Break"), GUIOption.FixedWidth(60)); btnApplyPrefab.OnClick += () => { PrefabUtility.ApplyPrefab(activeSO); }; btnRevertPrefab.OnClick += () => { GameObjectUndo.RecordSceneObject(activeSO, true, "Reverting \"" + activeSO.Name + "\" to prefab."); PrefabUtility.RevertPrefab(activeSO); GameObjectUndo.ResolveDiffs(); EditorApplication.SetSceneDirty(); }; btnBreakPrefab.OnClick += () => { UndoRedo.BreakPrefab(activeSO, "Breaking prefab link for " + activeSO.Name); EditorApplication.SetSceneDirty(); }; soPrefabLayout.AddElement(btnApplyPrefab); soPrefabLayout.AddElement(btnRevertPrefab); soPrefabLayout.AddElement(btnBreakPrefab); } else { GUILabel noPrefabLabel = new GUILabel("None"); soPrefabLayout.AddElement(noPrefabLabel); } soHasPrefab = hasPrefab; } Vector3 position; Quaternion rotation; if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World) { position = activeSO.Position; rotation = activeSO.Rotation; } else { position = activeSO.LocalPosition; rotation = activeSO.LocalRotation; } Vector3 scale = activeSO.LocalScale; if (!soPos.HasInputFocus || forceUpdate) { soPos.Value = position; } // Avoid updating the rotation unless actually changed externally, since switching back and forth between // quaternion and euler angles can cause weird behavior if ((!soRot.HasInputFocus && rotation != lastRotation) || forceUpdate) { soRot.Value = rotation.ToEuler(); lastRotation = rotation; } if (!soScale.HasInputFocus || forceUpdate) { soScale.Value = scale; } }
/// <summary> /// Finishes recording an undo command started via <see cref="StartUndo(string)"/>. If any changes are detected on /// the field an undo command is recorded onto the undo-redo stack, otherwise nothing is done. /// </summary> private void EndUndo() { GameObjectUndo.ResolveDiffs(); }
/// <summary> /// Triggered by the runtime when a resource is dropped on the scene tree view. /// </summary> private void Internal_DoOnResourceDropped(SceneObject parent, string[] resourcePaths) { if (resourcePaths == null) { return; } List <SceneObject> addedObjects = new List <SceneObject>(); for (int i = 0; i < resourcePaths.Length; i++) { ResourceMeta meta = ProjectLibrary.GetMeta(resourcePaths[i]); if (meta == null) { continue; } if (meta.ResType == ResourceType.Mesh) { if (!string.IsNullOrEmpty(resourcePaths[i])) { string meshName = Path.GetFileNameWithoutExtension(resourcePaths[i]); Mesh mesh = ProjectLibrary.Load <Mesh>(resourcePaths[i]); if (mesh == null) { continue; } SceneObject so = new SceneObject(meshName); GameObjectUndo.RecordNewSceneObject(so); so.Parent = parent; Renderable renderable = so.AddComponent <Renderable>(); renderable.Mesh = mesh; GameObjectUndo.ResolveDiffs(); addedObjects.Add(so); } } else if (meta.ResType == ResourceType.Prefab) { if (!string.IsNullOrEmpty(resourcePaths[i])) { Prefab prefab = ProjectLibrary.Load <Prefab>(resourcePaths[i]); SceneObject so = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name); so.Parent = parent; addedObjects.Add(so); } } } if (addedObjects.Count > 0) { EditorApplication.SetSceneDirty(); } Selection.SceneObjects = addedObjects.ToArray(); }