【Ngui 学习系列之一:简单组件的操作】

时间:2023-11-11 14:08:56

一、Button
unity edit:
Sprite作为父对象和背景
-- Collider
-- Button script
Label 作为子对象和显示文字
代码:
private UIButton Button;
void Start ()
{
EventDelegate btnOnClickEventDelegate = new EventDelegate(this,"ButtonClick");
Button = GameObject.Find("Button").GetComponent<UIButton>();
Button.onClick.Add(btnOnClickEventDelegate);
}

private void ButtonClick()
{
Debug.Log("点击了Button");
}

二、Popup List 下拉
unity edit:
Sprite作为父对象和背景
-- Collider
-- Button script
-- Popup List script(重要)
options: 下拉选择的内容
On Value Change:值改变时的操作
notify:子对象的Label
Method:UILabel->SetCurrentSelection
代码:
private UIPopupList PopupList;
private UILabel PopupListLabel;

void Start ()
{
EventDelegate popupListChangeDelegate = new EventDelegate(this, "PopupListChange");

PopupList = GameObject.Find("Popup List").GetComponent<UIPopupList>();
PopupListLabel = PopupList.GetComponent<Transform>().FindChild("Label").GetComponent<UILabel>();

PopupList.onChange.Add(popupListChangeDelegate);
}

private void PopupListChange()
{
Debug.Log(PopupListLabel.text);
}

三、Checkbox 单选框
unity edit:
widget作为父对象
-- Collider
-- Button script
-- Toggle script(重要)
State Transition
sprite: 钩子的sprite对象
Sprite 作为框的背景
-- Sprite 作为上一个sprite的子对象,显示钩子
Label 文字说明
代码:
private UIToggle Toggle;
void Start ()
{
EventDelegate toggleChangeDelegate = new EventDelegate(this,"ToggleChange");

Toggle = GameObject.Find("Checkbox").GetComponent<UIToggle>();
Toggle.onChange.Add(toggleChangeDelegate);
}

private void ToggleChange()
{
Debug.Log(Toggle.value.ToString());
}