The GUI starting at line 65 doesn’t show. I have no idea why. I feel like I’m missing something obvious though
class TransformInspector extends Editor
{
//Make a function that replaces the Transform Component in the Inpector
public override function OnInspectorGUI ()
{
//target is the object you have selected in the editor. We are making a variable that is equal to the target's transform
var t : Transform = target.transform;
//Below we are creating new buttons on the Transform component
GUILayout.BeginHorizontal();
if (GUILayout.Button("P"))
{
//Here we are telling Unity that the stuff that's being done by this button (changing the transform) needs to be undo-able
Undo.RegisterUndo (t, "Reset Position " + t.name);
t.transform.position = Vector3(0,0,0);
}
if (GUILayout.Button("R"))
{
Undo.RegisterUndo (t, "Reset Rotation " + t.name);
t.transform.localEulerAngles = Vector3(0,0,0);
}
if (GUILayout.Button("S"))
{
Undo.RegisterUndo (t, "Reset Scale " + t.name);
t.transform.localScale = Vector3 (1,1,1);
}
if (GUILayout.Button("PRS"))
{
Undo.RegisterUndo (t, "Reset All " + t.name);
t.transform.position = Vector3(0,0,0);
t.transform.localEulerAngles = Vector3(0,0,0);
t.transform.localScale = Vector3 (1,1,1);
}
if (GUILayout.Button("Values To Integers"))
{
Undo.RegisterUndo (t, "Make To Whole Numbers " + t.name);
var xPosition : int = t.localPosition.x;
var yPosition : int = t.localPosition.y;
var zPosition : int = t.localPosition.z;
var xRotation : int = t.localEulerAngles.x;
var yRotation : int = t.localEulerAngles.y;
var zRotation : int = t.localEulerAngles.z;
var xScale : int = t.localScale.x;
var yScale : int = t.localScale.y;
var zScale : int = t.localScale.z;
t.localPosition.x = xPosition;
t.localPosition.y = yPosition;
t.localPosition.z = zPosition;
t.localEulerAngles.x = xRotation;
t.localEulerAngles.y = yRotation;
t.localEulerAngles.z = zRotation;
t.localScale.x = xScale;
t.localScale.y = yScale;
t.localScale.z = zScale;
}
GUILayout.EndHorizontal();
//Because we are overriding the transform components GUI in the Inspector, we have to recreate it below
//Here we are recreating the part of the Transform component that gave you acces to the position.
var position : Vector3 = EditorGUILayout.Vector3Field("Position", t.localPosition);
//Here we are recreating the part of the Transform component that gave you acces to the rotation.
var rotation : Vector3 = EditorGUILayout.Vector3Field("Rotation", t.localEulerAngles);
//Here we are recreating the part of the Transform component that gave you acces to the rotation.
var scale : Vector3 = EditorGUILayout.Vector3Field("Scale", t.localScale);
var foldoutOpen : boolean = false;
foldoutOpen = EditorGUILayout.Foldout(foldoutOpen,"Quick Buttons")
GUILayout.Label ("Position");
GUILayout.BeginHorizontal();
if (GUILayout.Button("X-5"))
{
position.x -= 5.0;
}
if (GUILayout.Button("X+5"))
{
position.x += 5.0;
}
if (GUILayout.Button("Y-5"))
{
position.y -= 5.0;
}
if (GUILayout.Button("Y+5"))
{
position.y += 5.0;
}
if (GUILayout.Button("Z-5"))
{
position.z -= 5.0;
}
if (GUILayout.Button("Z+5"))
{
position.z += 5.0;
}
GUILayout.EndHorizontal();
GUILayout.Label ("Rotation");
GUILayout.BeginHorizontal();
if (GUILayout.Button("X-5"))
{
rotation.x -= 5.0;
}
if (GUILayout.Button("X+5"))
{
rotation.x += 5.0;
}
if (GUILayout.Button("Y-5"))
{
rotation.y -= 5.0;
}
if (GUILayout.Button("Y+5"))
{
rotation.y += 5.0;
}
if (GUILayout.Button("Z-5"))
{
rotation.z -= 5.0;
}
if (GUILayout.Button("Z+5"))
{
rotation.z += 5.0;
}
GUILayout.EndHorizontal();
//Here we are making a vector field that lets us offset the handles in the editor
if (GUI.changed)
{
Undo.RegisterUndo (t, "Transform Change");
t.localPosition = FixIfNaN(position);
t.localEulerAngles = FixIfNaN(rotation);
t.localScale = FixIfNaN(scale);
}
}
private function FixIfNaN(v : Vector3)
{
if (v.x == null)
{
v.x = 0;
}
if (v.y == null)
{
v.y = 0;
}
if (v.z == null)
{
v.z = 0;
}
return v;
}
}
Are you saying that the GUI up to line 65 does show? Or, is none of it showing at all? I’d be surprised if anything was showing at all - in order to have this act as an Inspector, you need to have the CustomEditor attribute on the class:
[CustomEditor(typeof(Transform))]
class TransformInspector extends Editor
{
So now the toggle shows, but I can’t figure out a way to declare the boolean that sets the toggle without making the toggle always active or always deactivated.
var foldoutOpen : boolean;
//foldoutOpen = EditorGUILayout.Toggle("Quick Buttons", foldoutOpen);
foldoutOpen = GUILayout.Toggle(foldoutOpen, "Quick Buttons");
if (foldoutOpen)
{
GUILayout.Label ("Position");
GUILayout.BeginHorizontal();
if (GUILayout.Button("X-5"))
{
position.x -= 5.0;
}
if (GUILayout.Button("X+5"))
{
position.x += 5.0;
}
if (GUILayout.Button("Y-5"))
{
position.y -= 5.0;
}
if (GUILayout.Button("Y+5"))
{
position.y += 5.0;
}
if (GUILayout.Button("Z-5"))
{
position.z -= 5.0;
}
if (GUILayout.Button("Z+5"))
{
position.z += 5.0;
}
GUILayout.EndHorizontal();
GUILayout.Label ("Rotation");
GUILayout.BeginHorizontal();
if (GUILayout.Button("X-5"))
{
rotation.x -= 5.0;
}
if (GUILayout.Button("X+5"))
{
rotation.x += 5.0;
}
if (GUILayout.Button("Y-5"))
{
rotation.y -= 5.0;
}
if (GUILayout.Button("Y+5"))
{
rotation.y += 5.0;
}
if (GUILayout.Button("Z-5"))
{
rotation.z -= 5.0;
}
if (GUILayout.Button("Z+5"))
{
rotation.z += 5.0;
}
GUILayout.EndHorizontal();
}
It would really make sense if you could post a script that actually compiles. Like that you are not wasting your own or our time. Post exactly what you have!
You defined foldoutOpen as a local variable which is always lost. You have to declare it as an instance variable that is preserved.
@CustomEditor(Transform)
class TransformInspector extends Editor
{
var foldoutOpen : boolean;
//Make a function that replaces the Transform Component in the Inpector
public override function OnInspectorGUI ()
{
//target is the object you have selected in the editor. We are making a variable that is equal to the target's transform
var t : Transform = target.transform;
//Below we are creating new buttons on the Transform component
GUILayout.BeginHorizontal();
if (GUILayout.Button("P"))
{
//Here we are telling Unity that the stuff that's being done by this button (changing the transform) needs to be undo-able
Undo.RegisterUndo (t, "Reset Position " + t.name);
t.transform.position = Vector3(0,0,0);
}
if (GUILayout.Button("R"))
{
Undo.RegisterUndo (t, "Reset Rotation " + t.name);
t.transform.localEulerAngles = Vector3(0,0,0);
}
if (GUILayout.Button("S"))
{
Undo.RegisterUndo (t, "Reset Scale " + t.name);
t.transform.localScale = Vector3 (1,1,1);
}
if (GUILayout.Button("PRS"))
{
Undo.RegisterUndo (t, "Reset All " + t.name);
t.transform.position = Vector3(0,0,0);
t.transform.localEulerAngles = Vector3(0,0,0);
t.transform.localScale = Vector3 (1,1,1);
}
if (GUILayout.Button("Values To Integers"))
{
Undo.RegisterUndo (t, "Make To Whole Numbers " + t.name);
var xPosition : int = t.localPosition.x;
var yPosition : int = t.localPosition.y;
var zPosition : int = t.localPosition.z;
var xRotation : int = t.localEulerAngles.x;
var yRotation : int = t.localEulerAngles.y;
var zRotation : int = t.localEulerAngles.z;
var xScale : int = t.localScale.x;
var yScale : int = t.localScale.y;
var zScale : int = t.localScale.z;
t.localPosition.x = xPosition;
t.localPosition.y = yPosition;
t.localPosition.z = zPosition;
t.localEulerAngles.x = xRotation;
t.localEulerAngles.y = yRotation;
t.localEulerAngles.z = zRotation;
t.localScale.x = xScale;
t.localScale.y = yScale;
t.localScale.z = zScale;
}
GUILayout.EndHorizontal();
//Because we are overriding the transform components GUI in the Inspector, we have to recreate it below
//Here we are recreating the part of the Transform component that gave you acces to the position.
var position : Vector3 = EditorGUILayout.Vector3Field("Position", t.localPosition);
//Here we are recreating the part of the Transform component that gave you acces to the rotation.
var rotation : Vector3 = EditorGUILayout.Vector3Field("Rotation", t.localEulerAngles);
//Here we are recreating the part of the Transform component that gave you acces to the rotation.
var scale : Vector3 = EditorGUILayout.Vector3Field("Scale", t.localScale);
foldoutOpen = EditorGUILayout.Foldout(foldoutOpen,"Quick Buttons");
if (foldoutOpen)
{
GUILayout.Label ("Position");
GUILayout.BeginHorizontal();
if (GUILayout.Button("X-5"))
{
position.x -= 5.0;
}
if (GUILayout.Button("X+5"))
{
position.x += 5.0;
}
if (GUILayout.Button("Y-5"))
{
position.y -= 5.0;
}
if (GUILayout.Button("Y+5"))
{
position.y += 5.0;
}
if (GUILayout.Button("Z-5"))
{
position.z -= 5.0;
}
if (GUILayout.Button("Z+5"))
{
position.z += 5.0;
}
GUILayout.EndHorizontal();
GUILayout.Label ("Rotation");
GUILayout.BeginHorizontal();
if (GUILayout.Button("X-5"))
{
rotation.x -= 5.0;
}
if (GUILayout.Button("X+5"))
{
rotation.x += 5.0;
}
if (GUILayout.Button("Y-5"))
{
rotation.y -= 5.0;
}
if (GUILayout.Button("Y+5"))
{
rotation.y += 5.0;
}
if (GUILayout.Button("Z-5"))
{
rotation.z -= 5.0;
}
if (GUILayout.Button("Z+5"))
{
rotation.z += 5.0;
}
GUILayout.EndHorizontal();
}
//Here we are making a vector field that lets us offset the handles in the editor
if (GUI.changed)
{
Undo.RegisterUndo (t, "Transform Change");
t.localPosition = FixIfNaN(position);
t.localEulerAngles = FixIfNaN(rotation);
t.localScale = FixIfNaN(scale);
}
}
private function FixIfNaN(v : Vector3)
{
if (v.x == null)
{
v.x = 0;
}
if (v.y == null)
{
v.y = 0;
}
if (v.z == null)
{
v.z = 0;
}
return v;
}
}
Whenever I declare it somewhere else (anywhere that it wont be a local variable, constantly changed to its declared state), it gives me an error and doesn’t recognize the variable when I instance it at the toggle
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// //
// // //
// This script was created by Keenan Woodall, for personal use and is now available for anyone to use, //
// modify, share, or tweak. If you share this script, give credit to Keenan Woodall. //
// Keenan made this script and made it free, to help others out. //
// You are welcome to use this commercially. You don't have to put me in the credits or anything. //
// I would love it if you email me at keenanwoodall@gmail.com if you do use this in making a game. //
// I'd love to check it out! I hope you enjoy this asset, and if you have any feedback, you can email me, //
// or comment on the asset store. This is a continous project that will be updated as I add features/content. //
// //
// //
// //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@CustomEditor (Transform)
class TransformInspector extends Editor
{
//Make a function that replaces the Transform Component in the Inpector
public override function OnInspectorGUI ()
{
//target is the object you have selected in the editor. We are making a variable that is equal to the target's transform
var t : Transform = target.transform;
//Below we are creating new buttons on the Transform component
GUILayout.BeginHorizontal();
if (GUILayout.Button("P"))
{
//Here we are telling Unity that the stuff that's being done by this button (changing the transform) needs to be undo-able
Undo.RegisterUndo (t, "Reset Position " + t.name);
t.transform.position = Vector3(0,0,0);
}
if (GUILayout.Button("R"))
{
Undo.RegisterUndo (t, "Reset Rotation " + t.name);
t.transform.localEulerAngles = Vector3(0,0,0);
}
if (GUILayout.Button("S"))
{
Undo.RegisterUndo (t, "Reset Scale " + t.name);
t.transform.localScale = Vector3 (1,1,1);
}
if (GUILayout.Button("PRS"))
{
Undo.RegisterUndo (t, "Reset All " + t.name);
t.transform.position = Vector3(0,0,0);
t.transform.localEulerAngles = Vector3(0,0,0);
t.transform.localScale = Vector3 (1,1,1);
}
if (GUILayout.Button("Values To Integers"))
{
Undo.RegisterUndo (t, "Make To Whole Numbers " + t.name);
var xPosition : int = t.localPosition.x;
var yPosition : int = t.localPosition.y;
var zPosition : int = t.localPosition.z;
var xRotation : int = t.localEulerAngles.x;
var yRotation : int = t.localEulerAngles.y;
var zRotation : int = t.localEulerAngles.z;
var xScale : int = t.localScale.x;
var yScale : int = t.localScale.y;
var zScale : int = t.localScale.z;
t.localPosition.x = xPosition;
t.localPosition.y = yPosition;
t.localPosition.z = zPosition;
t.localEulerAngles.x = xRotation;
t.localEulerAngles.y = yRotation;
t.localEulerAngles.z = zRotation;
t.localScale.x = xScale;
t.localScale.y = yScale;
t.localScale.z = zScale;
}
GUILayout.EndHorizontal();
//Because we are overriding the transform components GUI in the Inspector, we have to recreate it below
EditorGUIUtility.LookLikeControls();
EditorGUI.indentLevel = 0;
//Here we are recreating the part of the Transform component that gave you acces to the position.
var position : Vector3 = EditorGUILayout.Vector3Field("Position", t.localPosition);
//Here we are recreating the part of the Transform component that gave you acces to the rotation.
var rotation : Vector3 = EditorGUILayout.Vector3Field("Rotation", t.localEulerAngles);
//Here we are recreating the part of the Transform component that gave you acces to the rotation.
var scale : Vector3 = EditorGUILayout.Vector3Field("Scale", t.localScale);
var foldoutOpen : boolean;
//foldoutOpen = EditorGUILayout.Toggle("Quick Buttons", foldoutOpen);
foldoutOpen = GUILayout.Toggle(foldoutOpen, "Quick Buttons");
if (foldoutOpen)
{
GUILayout.Label ("Position");
GUILayout.BeginHorizontal();
if (GUILayout.Button("X-5"))
{
position.x -= 5.0;
}
if (GUILayout.Button("X+5"))
{
position.x += 5.0;
}
if (GUILayout.Button("Y-5"))
{
position.y -= 5.0;
}
if (GUILayout.Button("Y+5"))
{
position.y += 5.0;
}
if (GUILayout.Button("Z-5"))
{
position.z -= 5.0;
}
if (GUILayout.Button("Z+5"))
{
position.z += 5.0;
}
GUILayout.EndHorizontal();
GUILayout.Label ("Rotation");
GUILayout.BeginHorizontal();
if (GUILayout.Button("X-5"))
{
rotation.x -= 5.0;
}
if (GUILayout.Button("X+5"))
{
rotation.x += 5.0;
}
if (GUILayout.Button("Y-5"))
{
rotation.y -= 5.0;
}
if (GUILayout.Button("Y+5"))
{
rotation.y += 5.0;
}
if (GUILayout.Button("Z-5"))
{
rotation.z -= 5.0;
}
if (GUILayout.Button("Z+5"))
{
rotation.z += 5.0;
}
GUILayout.EndHorizontal();
}
//Here we are making a vector field that lets us offset the handles in the editor
EditorGUIUtility.LookLikeInspector();
if (GUI.changed)
{
Undo.RegisterUndo (t, "Transform Change");
t.localPosition = FixIfNaN(position);
t.localEulerAngles = FixIfNaN(rotation);
t.localScale = FixIfNaN(scale);
}
}
private function FixIfNaN(v : Vector3)
{
if (v.x == null)
{
v.x = 0;
}
if (v.y == null)
{
v.y = 0;
}
if (v.z == null)
{
v.z = 0;
}
return v;
}
}