- Home /
Using handles to edit custom classes
Is there a way to override Unity's handles? I mean, I want to use the rotation gizmo to modify a property other than transform.rotation. I've got this so far:
using UnityEditor; using UnityEngine;
[CustomEditor(typeof(Foo))] public class FooEditor : Editor { public override void OnInspectorGUI () { DrawDefaultInspector(); }
public void OnSceneGUI()
{
Foo foo = (Foo)target;
foo.rotation = Handles.RotationHandle(foo.rotation, foo.transform.position);
}
}
And my Foo class:
using UnityEngine;
public class Foo : MonoBehaviour { public Vector3 position; public Quaternion rotation; public Vector3 size; }
I want that when the user modify size, position or rotation through one of the Unity's default gizmos, my class get modified and not the current selected transform. If there is no way of doing that, is there a way to disable the rendering of Unity's default gizmos?
Answer by ckfinite · Nov 30, 2010 at 03:12 AM
I think that your problem is that you are accessing a field that is set in the superclass without a overriding field in your code. What the upshot of this OO-speak is that you need to rename the variable in your class to something like fooRotation, or just changing the capitalization (C# is case sensitive).
You might try
using UnityEngine;
public class Foo : MonoBehaviour { public Vector3 Position; public Quaternion Rotation; public Vector3 Size; }
and calling Foo foo = (Foo)target;
foo.Rotation= Handles.RotationHandle(foo.Rotation, foo.transform.position);
Actually what I was trying to achieve is to hide the default gizmo that appears when you select a GameObject on your scene, so the User will feel like he is moving the default Gizmo, but he is actually moving my own/custom/very specific Gizmo.
You could try something like Handles.ArrowCap to draw your own handle design. Otherwise, you might be able to draw directly into the environment. Not sure how, but I think it can be done.
Answer by Guessmyname · Jul 21, 2014 at 04:16 PM
I had this pain myself; eventually I tracked it down to the Tools class.
The trick is, in your custom inspector (ie the one that derives from Editor), add this code to the OnEnable() and OnDisable() functions:
void OnEnable(){
Tools.hidden = true; //hide default tools when activated
}
void OnDisable(){
Tools.hidden = false; //unhide default tools when deactived
}
Thus you can 'override' the default tools with your own custom handles without the default one getting in the way.
Your answer
Follow this Question
Related Questions
Handles.PositionHandle not working anymore in version 2019.3? 0 Answers
Showing Handles in the Game View 1 Answer
There is a way to check if a handle is highlighted? 1 Answer
How to serialize Field Variables on hidden Editor scripts? 0 Answers
How to create a control that allows to select position within a line? 3 Answers