Stupid question and it’s kinda’ rediculous, but I have public variables in my code. So their showing up in the inspector. But I want to change hot I can modify those variables in the inspector. Like I have a float variable that needs to stay between 0 and 1. I would like in the inspector for this to have a slider bar that won’t let you go past 0 or 1. I’ve been looking everywhere for just a refresher on how to do this, and can’t find anything. If anyone can point me in the correct direction, it would be greatly appreciated.
Havn’t played with it myself but there is an Editor.OnInspectorGUI event
you can add your GUI code for the slider in there. It will replace the automatic Inspector that is generated but you can invoke the DrawDefaultInspector method to invoke the inspector generator to populate if you only want to add a couple of controls.
There is some code in the above DrawDefaultInspector that almost looks exactly like what you need.
// This example shows a custom inspector for an
// object "MyPlayerEditor", which has two variables, vel (velocity) and ammo.
@CustomEditor(MyPlayerEditor)
class MyPlayerEditor extends Editor {
function OnInspectorGUI() {
EditorGUILayout.LabelField ("Some help", "Some other text");
EditorGUILayout.BeginHorizontal ();
EditorGUILayout.PrefixLabel ("Speed");
target.vel = EditorGUILayout.Slider (target.vel, 0, 100);
EditorGUILayout.EndHorizontal ();
// Show default inspector property editor
DrawDefaultInspector ();
}
}
I think I get how to do it. My only thing at this point is where in the code it says @CustomEditor it gives me an error. Now, I’ve kind of realized that this is not written for C# which is what I program in. But I’m also not sure what the equivalent of that is in C#, wondering if someone could help me out with that. Other then that, I think this will work nicely. Thank you.
you may have found the answer by following the links I gave you before. But if not here’s another pointer.
Customer Editor Tells an Editor class which run-time type it’s an editor for. Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder. Editor classes are in the UnityEditor namespace so for C# scripts you need to add “using UnityEditor;” at the beginning of the script.
It’s an Attribute. What you want is this:
[Range(MinValue, MaxValue)]
public float MyFloat;
Probably too late for this topic but here’s the answer for who’s still looking for it.