Always thankful for your support, Lotte💖
Last active
November 2, 2023 21:04
-
-
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!
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 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a very cool demonstration, thank you! It will be super useful for learning.