A game about forced loneliness, made by TACStudios
at master 125 lines 4.2 kB view raw
1using UnityEngine; 2using UnityEditor.EditorTools; 3using UnityEditor; 4 5[EditorTool("Light Placement Tool", typeof(Light))] 6class LightPlacementTool : EditorTool 7{ 8 private const float k_DefaultZoom = 0.05f; 9 private bool initialPositionSet; 10 11 private static Vector3 initialPivot; 12 private static Quaternion initialRotation; 13 14 private static Vector3 originalPivot; 15 private static Quaternion originalRotation; 16 17 private static GameObject currentGameObject; 18 private float previousZoom; 19 private static SceneView.SceneViewState previousSceneViewState; 20 private static SceneView lastSceneView; 21 22 public override GUIContent toolbarIcon => EditorGUIUtility.TrIconContent(UnityEditor.Rendering.CoreEditorUtils.LoadIcon(@"Packages/com.unity.render-pipelines.core/Editor/Lighting/Icons/", "LightPlacement_Icon", ".png", false), "The Light Placement Tool temporarily changes the Scene Camera to look-through the currently selected Light. Use the Scene View Navigation controls to move it around the Scene."); 23 24 public override void OnActivated() 25 { 26 base.OnActivated(); 27 if (lastSceneView == null) 28 { 29 lastSceneView = SceneView.lastActiveSceneView; 30 lastSceneView.orthographic = false; 31 previousSceneViewState = lastSceneView.sceneViewState; 32 lastSceneView.sceneViewState.alwaysRefresh = true; 33 } 34 TransitionCameraToLight(); 35 Undo.undoRedoPerformed += OnUndoRedo; 36 } 37 38 public override void OnWillBeDeactivated() 39 { 40 base.OnWillBeDeactivated(); 41 if (lastSceneView != null) 42 { 43 lastSceneView.sceneViewState = previousSceneViewState; 44 } 45 Undo.undoRedoPerformed -= OnUndoRedo; 46 ResetCamera(); 47 } 48 49 public override void OnToolGUI(EditorWindow window) 50 { 51 base.OnToolGUI(window); 52 SceneView view = SceneView.lastActiveSceneView; 53 54 //View size getting smaller than 0.0001f causes the camera to get stuck 55 if (view.size <= 0) 56 view.FixNegativeSize(); 57 58 if (lastSceneView != view) 59 return; 60 61 if (currentGameObject == null) 62 return; 63 64 if (!initialPositionSet) 65 { 66 SetInitialPosition(view); 67 return; 68 } 69 70 Event e = Event.current; 71 72 if (e.type == EventType.KeyDown && e.keyCode == KeyCode.Escape) 73 { 74 ToolManager.RestorePreviousTool(); 75 e.Use(); 76 view.Repaint(); 77 return; 78 } 79 80 Undo.RecordObject(currentGameObject.transform, "Move Light"); 81 Vector3 lookDir = (view.pivot - view.camera.transform.position).normalized; 82 currentGameObject.transform.SetPositionAndRotation(view.camera.transform.position + lookDir * k_DefaultZoom * 2f, view.camera.transform.rotation); 83 } 84 85 private void OnUndoRedo() 86 { 87 if (currentGameObject != null && lastSceneView != null) 88 { 89 lastSceneView.pivot = currentGameObject.transform.position; 90 lastSceneView.rotation = currentGameObject.transform.rotation; 91 } 92 } 93 94 private void TransitionCameraToLight() 95 { 96 initialPivot = SceneView.lastActiveSceneView.pivot; 97 initialRotation = SceneView.lastActiveSceneView.camera.transform.rotation; 98 99 var targetComponent = target as Light; 100 if (targetComponent != null) 101 { 102 currentGameObject = targetComponent.gameObject; 103 originalPivot = currentGameObject.transform.position; 104 originalRotation = currentGameObject.transform.rotation; 105 initialPositionSet = false; 106 } 107 } 108 109 private void SetInitialPosition(SceneView view) 110 { 111 previousZoom = view.size; 112 view.pivot = originalPivot; 113 view.rotation = originalRotation; 114 view.size = k_DefaultZoom; 115 view.Repaint(); 116 initialPositionSet = true; 117 } 118 119 private void ResetCamera() 120 { 121 SceneView.lastActiveSceneView.pivot = initialPivot; 122 SceneView.lastActiveSceneView.rotation = initialRotation; 123 SceneView.lastActiveSceneView.size = previousZoom; 124 } 125}