Skip to content

Instantly share code, notes, and snippets.

@LotteMakesStuff
Last active November 2, 2023 21:04
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LotteMakesStuff/629b49b612ab3faccf3257a8a47577de to your computer and use it in GitHub Desktop.
Save LotteMakesStuff/629b49b612ab3faccf3257a8a47577de to your computer and use it in GitHub Desktop.
In Unity 2019.1 Unity added a new custom EditorTool API, heres some example code showing some tools we could make with it!
using UnityEditor;
using UnityEditor.EditorTools;
using UnityEngine;
[EditorTool("LookAt Tool")]
public class LookatTool : EditorTool
{
GUIContent cachedIcon;
// NOTE: as were caching this, unity will serialize it between compiles! so if we want to test out new looks,
// just return the new GUIContent and bypass the cache until were happy with the icon...
public override GUIContent toolbarIcon
{
get
{
if (cachedIcon == null)
cachedIcon = EditorGUIUtility.IconContent("ViewToolOrbit", "|LookAt Tool");
return cachedIcon;
}
}
public override void OnToolGUI(EditorWindow window)
{
var view = SceneView.lastActiveSceneView;
// If there are multiple selected objects, we want to focus the look position somewhere in the middle..
if (Selection.transforms.Length > 1)
{
Vector3 center = Vector3.zero;
foreach (var transform in Selection.transforms)
{
center += transform.position;
}
center = center / Selection.transforms.Length;
view.LookAt(center);
// Lets draw a tiny sphere at that center point, so we can verify it works
Handles.SphereHandleCap(0,center,Quaternion.identity, 0.1f, EventType.Repaint);
}
// If we only have one selected object, thats way easier, we just look at it
else if (Selection.activeTransform != null)
{
view.LookAt(Selection.activeTransform.position);
}
}
}

Always thankful for your support, Lotte💖

Buy Me a Coffee at ko-fi.com Become a Patron!

@ALapidis
Copy link

This is a very cool demonstration, thank you! It will be super useful for learning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment