Unity 3D: Automatic Event Connections a’la Qt

xmas_tree You will find a new script in my UnityAssets repository at GitHub. It is called AutoConnector.cs and serves the same purpose as Qt’s auto-connection mechanism for signals and slots. You will find a usage example below. Hope it makes your life easier 🙂

This is probably the last entry on the blog this year so I’ll take this opportunity to wish you all Merry Christmas and fruitful programming in the New Year 2017! :mrgreen:

public class AutoConnectTest : MonoBehaviour {

	class Ui {
		public GameObject MySlider;
	};

	Ui ui = new Ui();

	// Use this for initialization
	void Start () {
		AutoConnector.autoFind(ui);
		AutoConnector.autoConnect (this);
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void on_MySlider_ValueChanged(float f) {
		Debug.Log (string.Format ("Current value is {0}", f));
	}

	void on_MyButton_Click() {
		Debug.Log ("Button clicked!");
		ui.MySlider.SetActive (!ui.MySlider.activeSelf);
	}

	void on_MyInputField_EndEdit(string s) {
		Debug.Log (string.Format ("Ended editing input field, value: {0}", s));
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

thirty eight − = thirty two