Recent attempts at Unity 4.6 new version of Ugui. Unity many operations are to be specified in the inspector, this way is very easy to get started, even some planning, art students can also do something, very good. But there are situations that are not appropriate for a program. For example, I have 10 skill buttons that trigger their corresponding skills when clicked on to a button. Wouldn't it be a hassle if each button was manually bound to a function? In addition, the binding of this function is still no parameter, do you want to write 10 functions to handle the same logic? Instantly feel very egg ache, is there?
In response to this situation, a solution is given, assuming we have edited N button objects:
public Class Uiskillpanel:monobehaviour {//Use this for initialization void Start () {//Fetch The player skill data list<spellinfo> spelllist = LocalPlayerData.Inst.SpellList; for (int i = 0; i < Spelllist.count; i++) {Spellinfo spell = spelllist[i]; Gameobject Btngo = Gameobject.find (string. Format ("skillbutton{0}", i)); --Use anonymous delegate to handle button event Buttons btn = btngo.getcomponent<button> (); Btn.onClick.AddListener (Delegate () {This.onskillbuttonclick (spell); } ); }} void Onskillbuttonclick (Spellinfo spell) {Debug.Log (string. Format ("Spell button Clicked: {0}.", Spell. Name)); } }
This notation dynamically passes in the spell parameter, and also resolves the relationship between the button and the skill. :)
Unity 4.6 uses anonymous delegate to handle Ugui control event bindings