Skip to content

Instantly share code, notes, and snippets.

@Petethegoat
Last active February 4, 2024 05:20
Show Gist options
  • Select an option

  • Save Petethegoat/e2d4c9e4704aa5a237b33fb1dcc481bc to your computer and use it in GitHub Desktop.

Select an option

Save Petethegoat/e2d4c9e4704aa5a237b33fb1dcc481bc to your computer and use it in GitHub Desktop.
Replacement for the stupid 2022 pivot/center and global/local button changes.
using UnityEngine.UIElements;
using UnityEditor;
using UnityEditor.Overlays;
using UnityEditor.Toolbars;
using System.Collections.Generic;
[Overlay(typeof(SceneView), "Pivot & Space Selection", defaultDisplay = true)]
public class PivotSpaceButtons : Overlay, ICreateToolbar
{
IEnumerable<string> ICreateToolbar.toolbarElements => k_ToolbarItems;
static readonly string[] k_ToolbarItems = new[]
{
"Toggle Tool Handle Rotation",
"Toggle Tool Handle Position"
};
public override VisualElement CreatePanelContent() => new Label() { text = "Please dock me to a toolbar! I'm your pivot and space controls!" };
}
[EditorToolbarElement("Toggle Tool Handle Rotation", typeof(SceneView))]
class GlobalLocalToggle : EditorToolbarToggle
{
const string global = " Global";
const string local = " Local";
PivotRotation expectedRotation = PivotRotation.Global;
public GlobalLocalToggle()
{
offIcon = EditorGUIUtility.FindTexture("d_ToolHandleGlobal");
onIcon = EditorGUIUtility.FindTexture("d_ToolHandleLocal");
this.RegisterValueChangedCallback(OnChange);
EditorApplication.update += CheckChange;
text = Tools.pivotRotation == PivotRotation.Local ? local : global;
}
void OnChange(ChangeEvent<bool> e)
{
text = e.newValue ? local : global;
Tools.pivotRotation = e.newValue ? PivotRotation.Local : PivotRotation.Global;
}
void CheckChange()
{
if(Tools.pivotRotation != expectedRotation)
{
ToggleValue();
expectedRotation = Tools.pivotRotation;
}
}
}
[EditorToolbarElement("Toggle Tool Handle Position", typeof(SceneView))]
class PivotCenterToggle : EditorToolbarToggle
{
const string pivot = " Pivot";
const string center = " Center";
PivotMode expectedMode = PivotMode.Center;
public PivotCenterToggle()
{
offIcon = EditorGUIUtility.FindTexture("d_ToolHandleCenter");
onIcon = EditorGUIUtility.FindTexture("d_ToolHandlePivot");
this.RegisterValueChangedCallback(OnChange);
EditorApplication.update += CheckChange;
text = Tools.pivotMode == PivotMode.Pivot ? pivot : center;
}
void OnChange(ChangeEvent<bool> e)
{
text = e.newValue ? pivot : center;
Tools.pivotMode = e.newValue ? PivotMode.Pivot : PivotMode.Center;
}
void CheckChange()
{
if(Tools.pivotMode != expectedMode)
{
ToggleValue();
expectedMode = Tools.pivotMode;
}
}
}
@Petethegoat
Copy link
Copy Markdown
Author

Updated to also reflect changes via hotkey.

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