Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jeffweber · Mar 26, 2013 at 03:19 AM · editortransform

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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:

alt text

Thanks for the help everyone.


screen shot 2013-03-28 at 5.36.31 am.png (7.7 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 27, 2013 at 10:48 AM 0
Share

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;
  }
avatar image jeffweber · Mar 27, 2013 at 12:14 PM 0
Share

Great, will try that tonight. Thanks.

avatar image
1

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".

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jeffweber · Mar 26, 2013 at 02:19 PM 0
Share

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. :-)

avatar image whydoidoit · Mar 26, 2013 at 02:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges