Created
January 21, 2023 10:11
-
-
Save AsyncOperator/cdc1eb253f50d579c571d1cf6838e8e2 to your computer and use it in GitHub Desktop.
Custom editor tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.EditorTools; | |
[EditorTool( "Look at Tool" )] | |
public sealed class LookAtTool : EditorTool { | |
private const string ICON_NAME = "ViewToolOrbit"; | |
private GUIContent cachedIcon; | |
public override GUIContent toolbarIcon { | |
get | |
{ | |
if ( cachedIcon == null ) | |
cachedIcon = EditorGUIUtility.IconContent( ICON_NAME, "Look at Tool" ); | |
return cachedIcon; | |
} | |
} | |
public override void OnToolGUI( EditorWindow window ) { | |
var view = SceneView.lastActiveSceneView; | |
if ( Selection.transforms.Length > 1 ) { | |
Vector3 focusPoint = default; | |
foreach ( var transform in Selection.transforms ) { | |
focusPoint += transform.position; | |
} | |
focusPoint /= Selection.transforms.Length; | |
view.LookAt( focusPoint ); | |
Handles.Label( focusPoint, "Focus point" ); | |
Handles.SphereHandleCap( 0, focusPoint, Quaternion.identity, 0.1f, EventType.Repaint ); | |
} | |
else if ( Selection.activeTransform != null ) { | |
view.LookAt( Selection.activeTransform.position ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment