ISSUE:
I’m not pleased with the way data is populated into and read from UI controls, or at least my understanding of it. Here is how I understand data-exchange between PROCESSES and the UI works.
To populate UI controls with process output data, it appears one must either
-
Lookup the value during the UI element’s Update() function, by manually creating a script for this individual control. In here one must find the value (in your “processes”) and use it to change the displayed value.
-
Whenever your value changes in your process (or at the end of your process), find ALL the UI elements that show it, and change the value they display.
To use data from the UI as input for a process, it appears one must either
-
Find that control when you need the value, and extract it’s display value.
-
Update everything affected by specifying a function(s) in the control’s OnValueChanged component.
Not only does this prove cumbersome if a variable is displayed in more than one control or used in various processes, but I found myself writing nearly identical code, quite a few times.
QUESTION(s):
I tried the following (see “attempt” below), which works ok, but still, I’d like to know if there is a better way to do this. Are there any major problems with doing it this way? My primary goal is to reduce and reuse code and objects as much as possible, and to decrease development time, but will this reduce performance significantly? Is there anything similar already built into Unity, that I should try instead? How many programming rules am I breaking (no type-casting, using “global variables “, etc…)? Or is this, against all odds, a good idea?
ATTEMPT:
First I created this static class to hold ALL the data I wanted to exchange between ui elements and other processes. With this class we can Get or Set any of these values, anywhere in the program, simply by calling ValueDisplayExchange.setValue and ValueDisplayExchange.getValue. Each value stored/retrieved is referenced by a unique (case sensitive) string.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class ValueDisplayExchange
{
private static Dictionary <string,string> values;
static ValueDisplayExchange()
{
values=new Dictionary<string, string>();
values["Default"] = "No Value";
}
public static string getValue(string value_name)
//looks up the value, based on the valueName string
{
string value="";
values.TryGetValue(value_name,out value);
return value;
}
public static void setValue(string value_name,string value)
//adjusts or creates the value, under the name value_name
{
values[value_name] = value;
}
}
Then I created a couple of Premade UI Elements, adding a script component to each, and converted them to PreFabs.
The Textbox Prefab’s code looks like this:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DataExchangeTextScript : MonoBehaviour {
public string preValueText;
public string dataName;
private Text textbox;
// Use this for initialization
void Start () {
textbox = GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
textbox.text = preValueText + ValueDisplayExchange.getValue (dataName);
Debug.Log ("updated de text");
}
}
The important thing to note is that the preValueText and valueName are global variables, which means that they can be set in the editor. This allows us to change the ValueName (which specifies which value we want to exchange) for each instance of the prefab, WITHOUT LEAVING THE EDITOR.

The code for a DataExchangeSlider PreFab, which has input capabilities, is a little more complex. Also, since the ValueDisplayExchange class works only on strings, and a slider works only on floats, conversion is also need.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DataExchangeSlider : MonoBehaviour {
public string preValueText;
public string valueName;
private Slider slide;
// Use this for initialization
void Start () {
slide = GetComponent<Slider> ();
}
// Update is called once per frame
void Update () {
float value;
if (!float.TryParse (ValueDisplayExchange.getValue (valueName), out value))
value = 0;
slide.value = value;
Debug.Log ("updated de slide");
}
public void SetValue(float fvalue)
{
ValueDisplayExchange.setValue (valueName, fvalue.ToString ());
}
}
The OnValueChanged component of the Prefab is setup to reference the SetValueFunction defined above.

Usage Examples:
-
Create an Instance of a DataExchangeSliderPreFab, and a DataExchangeTextPreFab. Give them the same Value Name in the editor. Now play, and you will see the text box show whatever value you move the slider to.
-
The “players score” can be updated from several locations (within OnCollision functions, on a GameObject’s update function, when a cheat-code is entered, etc…) and is shown in multiple places (the game screen, the “player details” screen, etc…). In each place the score is updated, the same, single line of code, can be called to update the “players score”. All the various UI elements that display the score, will simply set their Data Name value to “players score”.
