private static void AddBoxCollider() { SceneObject so = Selection.SceneObject; if (so == null) { so = UndoRedo.CreateSO("BoxCollider", "New scene object"); Selection.SceneObject = so; FocusOnHierarchyOrScene(); } UndoRedo.RecordSO(so, false, "Added a BoxCollider component"); so.AddComponent <BoxCollider>(); 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(); }
public static void Redo() { EditorWindow[] allWindows = EditorWindow.AllWindows; foreach (var window in allWindows) { if (!window.HasFocus) { continue; } UndoRedo localStack = window.UndoRedo; if (localStack == null) { continue; } localStack.Redo(); return; } UndoRedo.Global.Redo(); }
/// <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> private 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 += () => { UndoRedo.RecordSO(activeSO, true, "Reverting \"" + activeSO.Name + "\" to prefab."); PrefabUtility.RevertPrefab(activeSO); 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) { 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) { soRot.Value = rotation.ToEuler(); lastRotation = rotation; } if (!soScale.HasInputFocus) { soScale.Value = scale; } }
private void OnEditorUpdate() { UpdateLoadingProgress(); if (HasFocus) { if (!Input.IsPointerButtonHeld(PointerButton.Right)) { if (VirtualInput.IsButtonDown(EditorApplication.DuplicateKey)) { DuplicateSelection(); } else if (VirtualInput.IsButtonDown(EditorApplication.DeleteKey)) { DeleteSelection(); } else if (VirtualInput.IsButtonDown(viewToolKey)) { EditorApplication.ActiveSceneTool = SceneViewTool.View; } else if (VirtualInput.IsButtonDown(moveToolKey)) { EditorApplication.ActiveSceneTool = SceneViewTool.Move; } else if (VirtualInput.IsButtonDown(rotateToolKey)) { EditorApplication.ActiveSceneTool = SceneViewTool.Rotate; } else if (VirtualInput.IsButtonDown(scaleToolKey)) { EditorApplication.ActiveSceneTool = SceneViewTool.Scale; } } } // Refresh GUI buttons if needed (in case someones changes the values from script) if (editorSettingsHash != EditorSettings.Hash) { UpdateButtonStates(); editorSettingsHash = EditorSettings.Hash; } // Update scene view handles and selection sceneGrid.Draw(); ProjectionType currentProjType = camera.ProjectionType; if (sceneAxesLastProjectionType != currentProjType) { sceneAxesGUI.ProjectionType = currentProjType; sceneAxesLastProjectionType = currentProjType; } bool handleActive = sceneHandles.IsActive() || sceneAxesGUI.IsActive(); Vector2I scenePos; bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos); bool clearSelection = false; if (AllowViewportInput) { if (Input.IsPointerButtonUp(PointerButton.Left)) { clearSelection = true; } else if (Input.IsPointerButtonDown(PointerButton.Left)) { mouseDownPosition = scenePos; } } else { clearSelection = true; inBounds = false; } bool dragResult = false; if (clearSelection) { dragResult = EndDragSelection(); if (sceneHandles.IsActive()) { sceneHandles.ClearSelection(); } if (sceneAxesGUI.IsActive()) { sceneAxesGUI.ClearSelection(); } } bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress; draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource; if (draggedOver) { if (DragDrop.DropInProgress) { dragActive = false; if (draggedSO != null) { Selection.SceneObject = draggedSO; EditorApplication.SetSceneDirty(); } draggedSO = null; } else { if (!dragActive) { dragActive = true; ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data; string[] draggedPaths = dragData.Paths; for (int i = 0; i < draggedPaths.Length; i++) { ResourceMeta meta = ProjectLibrary.GetMeta(draggedPaths[i]); if (meta != null) { if (meta.ResType == ResourceType.Mesh) { if (!string.IsNullOrEmpty(draggedPaths[i])) { string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]); draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\""); Mesh mesh = ProjectLibrary.Load <Mesh>(draggedPaths[i]); Renderable renderable = draggedSO.AddComponent <Renderable>(); renderable.Mesh = mesh; if (mesh != null) { draggedSOOffset = mesh.Bounds.Box.Center; } else { draggedSOOffset = Vector3.Zero; } } break; } else if (meta.ResType == ResourceType.Prefab) { if (!string.IsNullOrEmpty(draggedPaths[i])) { Prefab prefab = ProjectLibrary.Load <Prefab>(draggedPaths[i]); draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name); if (draggedSO != null) { AABox draggedObjBounds = EditorUtility.CalculateBounds(draggedSO); draggedSOOffset = draggedObjBounds.Center; } else { draggedSOOffset = Vector3.Zero; } } break; } } } } if (draggedSO != null) { if (Input.IsButtonHeld(ButtonCode.Space)) { SnapData snapData; sceneSelection.Snap(scenePos, out snapData, new SceneObject[] { draggedSO }); Quaternion q = Quaternion.FromToRotation(Vector3.YAxis, snapData.normal); draggedSO.Position = snapData.position; draggedSO.Rotation = q; } else { Ray worldRay = camera.ScreenPointToRay(scenePos); draggedSO.Position = worldRay * DefaultPlacementDepth - draggedSOOffset; } } } return; } else { if (dragActive) { dragActive = false; if (draggedSO != null) { draggedSO.Destroy(); draggedSO = null; } } } if ((HasContentFocus || IsPointerHovering) && AllowViewportInput) { sceneCamera.EnableInput(true); if (inBounds && HasContentFocus) { if (Input.IsPointerButtonDown(PointerButton.Left)) { Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX, HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize); if (sceneAxesGUIBounds.Contains(scenePos)) { sceneAxesGUI.TrySelect(scenePos); } else { sceneHandles.TrySelect(scenePos); } } else if (Input.IsPointerButtonHeld(PointerButton.Left) && !handleActive && !dragActive && draggedSO == null && scenePos != mouseDownPosition) { if (isDraggingSelection) { UpdateDragSelection(scenePos); } else { StartDragSelection(scenePos); } } else if (Input.IsPointerButtonUp(PointerButton.Left)) { if (!handleActive && !dragActive && !dragResult) { bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl); sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] { draggedSO }); } } } } else { sceneCamera.EnableInput(false); } if (AllowViewportInput) { SceneHandles.BeginInput(); sceneHandles.UpdateInput(scenePos, Input.PointerDelta); sceneAxesGUI.UpdateInput(scenePos); SceneHandles.EndInput(); } sceneHandles.Draw(); sceneAxesGUI.Draw(); // Must be done after handle input is processed, in order to reflect most recent transform sceneGizmos.Draw(); sceneSelection.Draw(); UpdateGridMode(); if (VirtualInput.IsButtonDown(frameKey)) { sceneCamera.FrameSelected(); } }
internal static extern void Internal_CreateInstance(UndoRedo instance);
/// <summary> /// Used by the runtime to set the global undo/redo stack. /// </summary> /// <param name="global">Instance of the global undo/redo stack.</param> private static void Internal_SetGlobal(UndoRedo global) { // We can't set this directly through the field because there is an issue with Mono and static fields UndoRedo.global = global; }
/// <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(); }