Unity 4.6 uses anonymous delegate to handle Ugui control event bindings

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.