/// <summary> /// Constructs a new editor application. Called at editor start-up by the runtime, and any time assembly refresh /// occurrs. /// </summary> internal EditorApplication() { instance = this; codeManager = new ScriptCodeManager(); const string soName = "EditorPersistentData"; SceneObject so = Scene.Root.FindChild(soName); if (so == null) so = new SceneObject(soName, true); persistentData = so.GetComponent<EditorPersistentData>(); if (persistentData == null) persistentData = so.AddComponent<EditorPersistentData>(); // Register controls InputConfiguration inputConfig = VirtualInput.KeyConfig; inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W); inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S); inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A); inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D); inputConfig.RegisterButton(SceneCamera.MoveUpBinding, ButtonCode.E); inputConfig.RegisterButton(SceneCamera.MoveDownBinding, ButtonCode.Q); inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up); inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down); inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left); inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right); inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift); inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight); inputConfig.RegisterButton(SceneCamera.PanBinding, ButtonCode.MouseMiddle); inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX); inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY); inputConfig.RegisterAxis(SceneCamera.ScrollAxisBinding, InputAxis.MouseZ); inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt); inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q); inputConfig.RegisterButton(SceneWindow.FrameBinding, ButtonCode.F); inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W); inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E); inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R); inputConfig.RegisterButton(CutBinding, ButtonCode.X, ButtonModifier.Ctrl); inputConfig.RegisterButton(CopyBinding, ButtonCode.C, ButtonModifier.Ctrl); inputConfig.RegisterButton(PasteBinding, ButtonCode.V, ButtonModifier.Ctrl); inputConfig.RegisterButton(DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl); inputConfig.RegisterButton(DeleteBinding, ButtonCode.Delete); inputConfig.RegisterButton(RenameBinding, ButtonCode.F2); if (IsProjectLoaded) { monitor = new FolderMonitor(ProjectLibrary.ResourceFolder); monitor.OnAdded += OnAssetModified; monitor.OnRemoved += OnAssetModified; monitor.OnModified += OnAssetModified; } }
private void OnEditorUpdate() { if (currentType == InspectorType.SceneObject) { Component[] allComponents = activeSO.GetComponents(); bool requiresRebuild = allComponents.Length != inspectorComponents.Count; if (!requiresRebuild) { for (int i = 0; i < inspectorComponents.Count; i++) { if (inspectorComponents[i].instanceId != allComponents[i].InstanceId) { requiresRebuild = true; break; } } } if (requiresRebuild) { SceneObject so = activeSO; Clear(); SetObjectToInspect(so); } else { RefreshSceneObjectFields(false); InspectableState componentModifyState = InspectableState.NotModified; for (int i = 0; i < inspectorComponents.Count; i++) { componentModifyState |= inspectorComponents[i].inspector.Refresh(); } if (componentModifyState.HasFlag(InspectableState.ModifyInProgress)) { EditorApplication.SetSceneDirty(); } modifyState |= componentModifyState; } } else if (currentType == InspectorType.Resource) { inspectorResource.inspector.Refresh(); } // Detect drag and drop bool isValidDrag = false; if (activeSO != null) { if ((DragDrop.DragInProgress || DragDrop.DropInProgress) && DragDrop.Type == DragDropType.Resource) { Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition); Vector2I scrollPos = windowPos; Rect2I contentBounds = inspectorLayout.Bounds; scrollPos.x -= contentBounds.x; scrollPos.y -= contentBounds.y; bool isInBounds = false; Rect2I dropArea = new Rect2I(); foreach (var bounds in dropAreas) { if (bounds.Contains(scrollPos)) { isInBounds = true; dropArea = bounds; break; } } Type draggedComponentType = null; if (isInBounds) { ResourceDragDropData dragData = DragDrop.Data as ResourceDragDropData; if (dragData != null) { foreach (var resPath in dragData.Paths) { ResourceMeta meta = ProjectLibrary.GetMeta(resPath); if (meta != null) { if (meta.ResType == ResourceType.ScriptCode) { ScriptCode scriptFile = ProjectLibrary.Load <ScriptCode>(resPath); if (scriptFile != null) { Type[] scriptTypes = scriptFile.Types; foreach (var type in scriptTypes) { if (type.IsSubclassOf(typeof(Component))) { draggedComponentType = type; isValidDrag = true; break; } } if (draggedComponentType != null) { break; } } } } } } } if (isValidDrag) { scrollAreaHighlight.Bounds = dropArea; if (DragDrop.DropInProgress) { activeSO.AddComponent(draggedComponentType); modifyState = InspectableState.Modified; EditorApplication.SetSceneDirty(); } } } } if (scrollAreaHighlight != null) { scrollAreaHighlight.Active = isValidDrag; } }
/// <summary> /// Called by the runtime whenever the editor assembly is loaded. This means initially when editor is started /// and every time assembly refresh occurs. /// </summary> static void OnInitialize() { app = new EditorApplication(); }
/// <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; Vector3 angles; if (EditorApplication.ActiveCoordinateMode == HandleCoordinateMode.World) { position = activeSO.Position; angles = activeSO.Rotation.ToEuler(); } else { position = activeSO.LocalPosition; angles = activeSO.LocalRotation.ToEuler(); } Vector3 scale = activeSO.LocalScale; if (!soPosX.HasInputFocus) { soPosX.Value = position.x; } if (!soPosY.HasInputFocus) { soPosY.Value = position.y; } if (!soPosZ.HasInputFocus) { soPosZ.Value = position.z; } if (!soRotX.HasInputFocus) { soRotX.Value = angles.x; } if (!soRotY.HasInputFocus) { soRotY.Value = angles.y; } if (!soRotZ.HasInputFocus) { soRotZ.Value = angles.z; } if (!soScaleX.HasInputFocus) { soScaleX.Value = scale.x; } if (!soScaleY.HasInputFocus) { soScaleY.Value = scale.y; } if (!soScaleZ.HasInputFocus) { soScaleZ.Value = scale.z; } }