- Home /
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.
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;
// ...
}
}
That sounds like what I'm after. Will let you know how implementation goes.
Thanks.
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];
}
}
}
}
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?
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
Follow this Question
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