One, the search for the component under the Canvas, button, text;
1, defining the component name
public class Infornew:monobehaviour {
Public Button _homel, _menu, _jia, _jian;
Public Text _caidan, _fenshu;
public int _adds;
Public Gameobject _camera;
Public Gameobject _list;
Public Gameobject _destroy;
2, find the component under the Star method
void Start ()
{//Find the component under itself (the path to the component). Find his generic type < type name > (): This method returns a generic type.
Formula: This.transform.fan ("path under its own location, without its own position"). Type of getcomponent< components > ();
_homel = This.transform.Find ("Panel_tu/panel_top/button_home"). Getcomponent <button > ();
_menu = This.transform.Find ("Panel_tu/panel_bottom/panel_0/button_menu"). Getcomponent<button > ();
_jia = This.transform.Find ("Panel_tu/panel_bottom/panel_1/panel_2/button_jia"). Getcomponent<button > ();
_jian =this.transform.find ("Panel_tu/panel_bottom/panel_1/panel_2/button_jian"). Getcomponent<button > ();
_fenshu =this.transform.find ("Panel_tu/panel_bottom/panel_1/panel_2/textv"). Getcomponent<text > ();//Text type
To add a button event
This represents itself as if there are multiple buttons that can be directly defined by the name. Getcomponent<button> (). Onclick.addlistener (ONCLICKBTN);
Onclick.addlistener (); Add event (the method name of the event);
Formula: this. Getcomponent<button> (). Onclick.addlistener (onclickbut);
_homel.onclick.addlistener (onclickbut); _menu.onclick.addlistener (Onclickmut); _jia.onclick.addlistener (onclickads);
_jian.onclick.addlistener (Onclickjian);
The text initialization assignment under this component.
_fenshu.text= "0";
}
public void onclickbut()// method of event
{
Instantiate (_camera);//Create an Object
Destroy (_destroy);//Destroy his own
}//ui switching over the same session
public void Onclickmut ()
{
Instantiate (_list);
Destroy (_destroy);
}
void Onclickads ()
{
if (_adds<9)
{
_adds++;
_fenshu.text = _adds. ToString ();
}
}
void Onclickjian ()
{
if (_adds>0)
{
_adds--;
_fenshu.text = _adds. ToString ();
}
}
}
Unity UI C # programming app UI interface switching