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
1
Question by Kiwasi · Nov 29, 2014 at 08:42 AM · variableinspectorreflection

Accessing an arbitary float defined via the inspector

Yes, this I am asking the 'How do I access a variable on another script' question. :)

I'm building a gauge type indicator. Think pressure gauge or temperature indicator. I want to have a script that can be assigned a float variable on another monobehaviour via the inspector. The gauge will then update according to the float.

I can make the gauge update fine. But I'm having trouble figuring out how to assign the variable to 'watch' in the inspector.

I'd originally looked at going down the interface path. But this doesn't allow the flexibility I'm after. It also requires a significant amount of boilerplate code. Using an event/subscription system from the float properties also suffers from similar drawbacks.

I'm thinking it can be done with reflection. Perhaps ending up with something that looks like the 4.6 event trigger box in the inspector, but picking a float instead of a method. Or using the get method.

Either way I have no idea where to start, or even the correct terms to Google. Any help would be much appreciated.

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
2
Best Answer

Answer by Bunny83 · Nov 30, 2014 at 12:03 AM

Since variables can't be directly referenced and you don't want to use an interface, the only solution left over is using reflection. You could use delegates and use something like this to serialize them in the editor. However, the delegates also have to be created in some way and i'm not sure if lambda expressions and closures will work.

So the easiest and straight forward solution is to use a variables which holds a MonoBehaviour reference and a string variable to define which field you want to access. This functionality can be wrapped in a custom class and implement a property drawer to show a drop-down list of available fields to select from if you like.

The important thing for anything "editable in the inspector" is, that the data in question can be serialized by Unity. Here's an example helper class:

 [System.Serializable]
 public class RemoteField<T>
 {
     public MonoBehaviour targetScript;
     public string fieldName;
     private System.Reflection.FieldInfo GetFieldInfo()
     {
         if (targetScript == null)
             return null;
         var type = targetScript.GetType();
         return type.GetField(fieldName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
     }
     public T Value
     {
         get
         {
             var field = GetFieldInfo();
             if (field != null)
             {
                 var val = field.GetValue(targetScript);
                 return (T)val;
             }
             return default(T);
         }
         set
         {
             var field = GetFieldInfo();
             if (field != null)
             {
                 field.SetValue(targetScript, value);
             }
         }
     }
 }
 
 [System.Serializable]
 public class RemoteFloatField : RemoteField<float> { };

Keep in mind that Unity can't serialitze generic types but a type derived from a generic type. So the RemoteFloatField class should be serialized by Unity. It contains a reference to a script and a string containing the field in question. As said above you could write a property drawer so you don't let the user enter the field name manually (which is error prone due to misspelling) but instead create a dropdown field with the available fields.

So in a class you could declare a field like this:

 public class SomeMonoBehaviour : MonoBehaviour
 {
     public RemoteFloatField removeField;
     void Update()
     {
         float v = removeField.Value;
         // ...
     }
 }

Comment
Add comment · Show 3 · 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 Kiwasi · Nov 30, 2014 at 01:06 AM 0
Share

That sounds like what I'm after. Will let you know how implementation goes.

Thanks.

avatar image Kiwasi · Nov 30, 2014 at 01:22 AM 0
Share

Works perfectly as written. Thanks again.

avatar image Kiwasi · Nov 30, 2014 at 05:03 AM 1
Share

Here is the property drawer to go with. Relatively ugly, I'm still learning both reflection and editor scripting

 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;

 [CustomPropertyDrawer(typeof(RemoteFloatField))]
 public class RemoteFloatFieldDrawer : PropertyDrawer {
 
     bool isOut;
     List<string> options;
     int selectedIndex;
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
         isOut = EditorGUILayout.Foldout (isOut, label);
         if(isOut){
             EditorGUILayout.PropertyField(property.FindPropertyRelative("targetScript"));
             object targetScript = property.FindPropertyRelative("targetScript").objectReferenceValue;
             if (targetScript != null){
                 options = new List<string>();
                 System.Reflection.FieldInfo[] fields = targetScript.GetType ().GetFields (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                 foreach(System.Reflection.FieldInfo field in fields){
                     if(field.FieldType == typeof(float)){
                         options.Add (field.Name);
                     }
                 }
                 EditorGUILayout.BeginHorizontal();
                 EditorGUILayout.LabelField("Float");
             selectedIndex = options.IndexOf (property.FindPropertyRelative("fieldName").stringValue);
             if (selectedIndex ==-1) selectedIndex = 0;
                       selectedIndex = EditorGUILayout.Popup(selectedIndex,options.ToArray ());
                 EditorGUILayout.EndHorizontal();
                 property.FindPropertyRelative("fieldName").stringValue = options[selectedIndex];
             }
         }
     }
 }
avatar image
0

Answer by tanoshimi · Nov 29, 2014 at 10:42 AM

I might have missed the subtlety in your problem here, but can't you simply have a public variable in your first script of type "otherScript", and assigning it via the inspector? Then you'd simply be watching otherScript.floatValue?

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 Kiwasi · Nov 29, 2014 at 06:35 PM 0
Share

While this will work, it won't give me the flexibility I'm after. Unless I'm missing something obvious and making this more complex then it needs to be. Some pseudo code may help demonstrate my case

 public class Example1: $$anonymous$$onoBehaviour {
     public float myFloat;
     public float myOtherFloat;
     //...
 }
 
 public class AnotherExample: $$anonymous$$onoBehaviour {
     public float myFavoriteFloats;
     public float myLeastFavoriteFloat;
     //...
 }
 
 // $$anonymous$$ultiple other classes
 
 public class Gauge : $$anonymous$$onoBehaviour {
     public float floatToWatch;
 
     void Update (){
         // Some code to set floatToWatch to any of the other floats
         // I'd like to choose the float via the inspector
         // I'd like to avoid hard coding in all the possible variables
     }
 
 }

Thoughts?

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

27 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

Related Questions

How do I assign script as a variable in the inspector? 1 Answer

Avoid public variable to get overwritten by the Inspector 1 Answer

How can I create variables for each materials to show in inspector (Using Editor) ? 0 Answers

My Javascript variables are not visible in the inspector 1 Answer

How do I show a variable in my base class in the inspector? 3 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