Skip to content

Instantly share code, notes, and snippets.

@AsyncOperator
Created January 21, 2023 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AsyncOperator/cdc1eb253f50d579c571d1cf6838e8e2 to your computer and use it in GitHub Desktop.
Save AsyncOperator/cdc1eb253f50d579c571d1cf6838e8e2 to your computer and use it in GitHub Desktop.
Custom editor tool
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