- Home /
How to create custom editor component to set z-position from dropdown list of values
I want to create an editor component that can be added to any GameObject. The component would show a simple dropdown list of possible z-position values (-.02, -.01, 0, .01, .02 for example.)
When I select a value I want the transform's z value to be updated with the selected value. I'd like the value to be updated within the editor just as if I set the z value directly.
What is the best/easiest way to achieve this?
Answer by jeffweber · Mar 27, 2013 at 10:44 AM
Here is the final solution I went with using whydoidoit's suggestions. Works perfect!
//DepthSetter Component
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[System.Serializable]
public class DepthSetter : MonoBehaviour {
[SerializeField]
public int DepthIndex = 2;
public string[] Depths = {"-.02", "-.01", "0", ".01", ".02"};
}
//DepthSetter Custom Editor - This needs to go in editor folder
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor (typeof (DepthSetter))]
public class DepthSetterEditor : Editor {
DepthSetter depthSetter;
private Transform _transform;
public override void OnInspectorGUI ()
{
depthSetter = target as DepthSetter;
_transform = depthSetter.gameObject.transform;
depthSetter.DepthIndex = EditorGUILayout.Popup("Depth",depthSetter.DepthIndex,depthSetter.Depths);
if (GUI.changed)
{
float z = float.Parse(depthSetter.Depths[depthSetter.DepthIndex]);
_transform.localPosition = this.FixIfNaN(new Vector3(_transform.localPosition.x, _transform.localPosition.y, z));
}
}
private Vector3 FixIfNaN (Vector3 v)
{
if (float.IsNaN(v.x))
{
v.x = 0;
}
if (float.IsNaN(v.y))
{
v.y = 0;
}
if (float.IsNaN(v.z))
{
v.z = 0;
}
return v;
}
}
Here is what it looks like in the inspector:
Thanks for the help everyone.
You need to work out the index or store it in the actual object, not the editor.
$$anonymous$$g.
var index = -1;
for(var i = 0; i < depth.Length;i++)
{
if($$anonymous$$athf.Approximately(depth[i], t.localPosition.z)) index = i;
}
Answer by keely · Mar 26, 2013 at 02:12 PM
It might not be the best way, but the way for example NGUI does stuff like this is custom inspector for Transform component.
See: http://wiki.unity3d.com/index.php?title=TransformInspector
The bad thing about this is that if Transform inspector is changed internally at Unity or you have plugins like NGUI running their own custom inspector, these custom editors will "collide".
Hmm, this does provide me some insight into how thing work, but I'm really looking for something that can be applied to only "certain" GameObjects. $$anonymous$$y preference would be that it work similar to a normal unity component but update the transform while in editor mode.
I would add this "editor" component to any gameobject that I want to mofify in this manner then I'd just select a value from the drop down and the Z value would change instantly. Not sure this is even possible but that is what I desire. :-)
All you need then is to have a "marker" non-editor script that you attach to those objects and write an inspector for that class that modifies the transform.
Your answer
Follow this Question
Related Questions
Scripting a "Unity Editor"-like object transformation control 1 Answer
Unity Script Editor Not Working 1 Answer
Script Editors for Unity 3 Answers
Transform override 3 Answers
Dynamic jump? 1 Answer