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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by aliakbo · Jun 20, 2013 at 10:02 AM · editorcustom editor

Slideable Float Field in Custom Editor

Hey,

I am creating a custom editor and wanted to include a FloatField. This works fine but I wanted the float field to change by clicking and sliding it, as most default unity components do. As shown in the image bellow.

alt text

I wasn't abel to achieve this and would appriciate it if someone could point me in the right direction.

Thank you for your time

slider.png (11.4 kB)
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

3 Replies

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

Answer by Bunny83 · Jun 20, 2013 at 10:24 AM

The FloatField does this by default. The internal function DoFloatField has a parameter for the dragHotZone. The rect of this zone is automatically calculated from the PrefixLabel size of the FloatField. When you don't display a label the dragHotZone will be emtpy.

Unfortunately the DoFloatField function is an internal function so you can't call it manually unless you use reflection.

Comment
Add comment · Show 9 · 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 aliakbo · Jun 20, 2013 at 10:41 AM 0
Share

Thanks, problem solved

avatar image MaT227 · Aug 31, 2015 at 08:09 PM 0
Share

@aliakbo, How did you manage to do it ?

avatar image Mikilo · Sep 01, 2015 at 08:01 AM 1
Share

@$$anonymous$$aT. Bunny83 told you. You need to use reflection to use those internal stuff.

avatar image Mikilo · Sep 01, 2015 at 10:36 AM 2
Share

This was a bit huge.

         Type    editorGUIType = typeof(EditorGUI);
         Type    RecycledTextEditorType = Assembly.GetAssembly(editorGUIType).GetType("UnityEditor.EditorGUI+RecycledTextEditor");
         Type[]    argumentTypes = new Type[] { RecycledTextEditorType, typeof(Rect), typeof(Rect), typeof(int), typeof(float), typeof(string), typeof(GUIStyle), typeof(bool), typeof(float) };
         $$anonymous$$ethodInfo doFloatField$$anonymous$$ethod = editorGUIType.Get$$anonymous$$ethod("DoFloatField", BindingFlags.NonPublic | BindingFlags.Static, null, argumentTypes, null)
avatar image Bunny83 · Sep 01, 2015 at 12:32 PM 1
Share

Right, "DoFloatField" has two overloads with different parameters ^^. If you don't specify the parameter types it doesn't know which method you're looking for.

Another way would be to use "Get$$anonymous$$ethods" ins$$anonymous$$d to get an array of all methods that match the given binding flags. Then you just have to iterate through that array to find the method you want. Of course besides checking the methods name you would need to check it's parameters as well.

In the end it's probably not shorter / easier than what @$$anonymous$$ikilo did. At least you don't need the "RecycledTextEditor" type^^.

Of course to invoke that method you need to pass an instance of that class, but you can find one in "s_RecycledEditor" which is a static internal field of the EditorGUI class. That field is also used by "FloatField"

Show more comments
avatar image
2

Answer by MaT227 · Sep 01, 2015 at 03:01 PM

Here is a version of the FloatFieldInternal method of EditorGUI which allows you to define the position of the dragHotZone.

 private float MyFloatFieldInternal(Rect position, Rect dragHotZone, float value, [DefaultValue("EditorStyles.numberField")]GUIStyle style)
 {
     int controlID = GUIUtility.GetControlID("EditorTextField".GetHashCode(), FocusType.Keyboard, position);
     Type editorGUIType = typeof(EditorGUI);
 
     Type RecycledTextEditorType = Assembly.GetAssembly(editorGUIType).GetType("UnityEditor.EditorGUI+RecycledTextEditor");
     Type[] argumentTypes = new Type[] { RecycledTextEditorType, typeof(Rect), typeof(Rect), typeof(int), typeof(float), typeof(string), typeof(GUIStyle), typeof(bool) };
     MethodInfo doFloatFieldMethod    = editorGUIType.GetMethod("DoFloatField", BindingFlags.NonPublic | BindingFlags.Static, null, argumentTypes, null);
 
     FieldInfo fieldInfo = editorGUIType.GetField("s_RecycledEditor", BindingFlags.NonPublic | BindingFlags.Static);
     object recycledEditor = fieldInfo.GetValue(null);
 
     object[] parameters = new object[] { recycledEditor, position, dragHotZone, controlID, value, "g7", style, true };
 
     return (float)doFloatFieldMethod.Invoke(null, parameters);
 }

As suggested, don't forget to cache the data, you don't need to assign the values everytime. This is purely for example purpose.

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 Mikilo · Sep 01, 2015 at 05:34 PM 0
Share

Obviously, if this utility is called on a window requiring mouse mouve, it will burn the CPU.

A little bit of cache would be necessary before using it.

avatar image MaT227 · Sep 02, 2015 at 07:51 AM 0
Share

Of course, assigning all those variables every time is a bad idea but this is for the example :) I'll add an edit to the answer.

avatar image
0

Answer by KeithKong · Dec 11, 2017 at 01:01 AM

For anyone wanting to do the same for int fields, you have to pass the drag sensitivity float as well. You could probably reflect EditorGUI.CalculateIntDragSensitivity() but you wouldn't be able to cache it so I decided to just pass 1f which seems to work fine. Here's the cached implementation (you could probably cache the params array as well and just update the indexes but *shrug*):

         private static MethodInfo sIntFieldMethod = null;
         private static object sRecycledEditor = null;
 
         private int MyIntFieldInternal (Rect displayRect, Rect dragHotZone, int value, GUIStyle style)
         {
             if (sIntFieldMethod == null || sRecycledEditor == null) {
                 Type editorGUIType = typeof(EditorGUI);
 
                 Type RecycledTextEditorType = Assembly.GetAssembly (editorGUIType).GetType ("UnityEditor.EditorGUI+RecycledTextEditor");
                 Type[] argumentTypes = new Type[] {
                     RecycledTextEditorType,
                     typeof(Rect),
                     typeof(Rect),
                     typeof(int),
                     typeof(int),
                     typeof(string),
                     typeof(GUIStyle),
                     typeof(bool),
                     typeof(float)
                 };
                 sIntFieldMethod = editorGUIType.GetMethod ("DoIntField", BindingFlags.NonPublic | BindingFlags.Static, null, argumentTypes, null);
 
                 FieldInfo fieldInfo = editorGUIType.GetField ("s_RecycledEditor", BindingFlags.NonPublic | BindingFlags.Static);
                 sRecycledEditor = fieldInfo.GetValue (null);
             }
 
             int controlID = GUIUtility.GetControlID ("EditorTextField".GetHashCode (), FocusType.Keyboard, displayRect);
             object[] fieldParams = new object[] { sRecycledEditor, displayRect, dragHotZone, controlID, value, "#######0", style, true, 1f };
 
             return (int)sIntFieldMethod.Invoke(null, fieldParams);
         }
Comment
Add comment · Show 1 · 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 GamerBrody · Sep 13, 2019 at 01:33 PM 0
Share

This will calculate your drag sensitivity, where 'value' is the int value you are displaying. The formula comes from Unity's ParticleSystem source code. float dragSensitity = $$anonymous$$athf.$$anonymous$$ax(1, $$anonymous$$athf.Pow($$anonymous$$athf.Abs((float)value), 0.5f) * 0.03f);

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How could I merge 2 components' inspectors through a custom editor? 2 Answers

UnityEventBase editor changed callback 0 Answers

List of CustomEditor inside CustomEditor 1 Answer

How do I cache or save a scene and object name so it can be reloaded similar to the Avatar Configuration window? 1 Answer

Field Attribute to auto insert in custom inspector 0 Answers


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