Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Le_Tai · Aug 20, 2016 at 12:55 AM · editorinspectoreditor-scripting

How can I make one variable affect others in custom inspector?

For example, I have a custom inspector like this

 public override void OnInspectorGUI()
 { 
      a= EditorGUILayout.FloatField("A", a); 
      lock =EditorGUILayout.Toggle("Lock",lock); 
      b= EditorGUILayout.FloatField("B", b);
  }

How do I make adjusting a also change b and vice versa when lock is true?

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 Fornoreason1000 · Aug 20, 2016 at 01:54 AM

Hello, maybe this will help

As you should know OnInspectorGUI runs fairly often, lets say every frame or so. then you have two floats A and B? and a bool called lock. you require A & B to be adjusted at a given ratio when _lock true.

lock is a C# Keyword so you will need to rename it, _lock will do.

a= EditorGUILayout.FloatField("A", a); assisngs A to the return value of EditorGUILAyout.FloatFeild() This is the same with B. so basically all you need to do is check if lock is true then modify the return value of A so B remains the same. To know how much they adjust by just make some temporary variable that store the old values of A and B to calculate how we should modify them

  public override void OnInspectorGUI()
  { 
      //Store unmodified values of A and B
       float old_a = a;
      float old_b = b;
      
        //Modify them using Editor Functions
       a= EditorGUILayout.FloatField("A", a); 
       lock =EditorGUILayout.Toggle("Lock",lock); 
       b= EditorGUILayout.FloatField("B", b);
 
      // Check if _lock is true
      if(_lock) {
           //if A was adjusted
           if(old_a != a) {
                //modify B based on A
                b = (old_b / old_a) * a
              }
         //if B was adjusted but not A
         else if(old_b != b) {
                   //Modify A based on B
                   a = (old_a / old_b) * b;
            }
      }
   }

This code firstly assigns two new variables , old_a and old_b. which are assigned to the original counterparts. then your original code runs, changing the values of a and b. Once this is done, an If Statement run if _lock is true, if it is true. two more If statements compare the values of a,b and old_a,old_b. if a or B has changed, we calculate a new value for the unchanged one.

Hope it Helps

DISCLAIMER! the formulas used to modify A and B are ones i made on the spot. they will probably produce weird results.

Sources:

  • Lock https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx

  • If Statements https://msdn.microsoft.com/en-AU/library/5011f09h.aspx

  • Return https://msdn.microsoft.com/en-us/library/1h3swy84.aspx

  • Methods https://msdn.microsoft.com/en-us/library/ms173114.aspx

Comment
Add comment · 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
0

Answer by OsmiousH · Aug 20, 2016 at 11:02 AM

try this

  public override void OnInspectorGUI(){
  if(lock){
     a=b;
     b=a;
  }
 }

this will change a to b and b to a when lock is true

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 Fornoreason1000 · Aug 22, 2016 at 07:56 AM 0
Share

Nice, its alot simpler than my answer, however theres is a bug i can see. If you adjust a, with lock true it wont work becuade it gets reassigned back to b. Effectively breaking the ability to adjust any value at all.

A=B and B=A shouldn't really be called on the same block. You need it to respond only when one of the values changes, then adjust the other value after.

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

66 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 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 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 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

Custom Inspector (Editor) - override just one field control 1 Answer

How to access one class instance in editor script? 1 Answer

How can I recreate the Array Inspector element for a custom Inspector GUI? 7 Answers

Get default preview for GameObject Editor 2 Answers

How to record hideFlags for Undo/Redo 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