纳金网

标题: UGUI学习日记 - Button [打印本页]

作者: 烟雨    时间: 2015-8-30 00:51
标题: UGUI学习日记 - Button
Selectable<----Button   (Button继承自Selectable)

OnClick 按钮点击事件.
添加与删除事件的操作:
1. 在Inspector面板中直接拖拽物体给Button属性下的OnClick事件中选择事件函数.
2. 通过代码操作.
  脚本UIEventTest.cs
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;

  5. public class UIEventTest : MonoBehaviour
  6. {
  7.         public Button button;
  8.         public Text text;
  9.         void Start ()
  10.         {
  11.                 button.onClick.AddListener (OnClicke);
  12.                 button.onClick.AddListener (
  13.                         delegate {text.color = Color.green; }  //匿名委托
  14.                 );
  15.         }
  16.         public void OnClick ()
  17.         {
  18.                 text.text = "Clicked:" + name;
  19.                 Debug.Log (text.text);
  20.         }
  21. }
复制代码
将脚本UIEventTest.cs拖拽到按钮上.拖拽目标Text放到脚本变量里.

可以在脚本中删除所有事件.button.onClick.RemoveAllListeners();
也可以删除指定事件 button.onClick.RemoveListener(OnClick);

事件被移除掉之后匿名委托事件还存在.
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;

  5. public class UIEventTest : MonoBehaviour
  6. {
  7.         private Button button;
  8.         public Text text;
  9.         void Start ()
  10.         {
  11.                 button = gameObject.GetComponent<Button> ();
  12.                 button.onClick.AddListener (OnClick);
  13.                 button.onClick.AddListener (delegate {
  14.                         text.color = Color.green;
  15.                         button.onClick.RemoveListener (OnClick);
  16.                         Debug.Log ("delegate event.");

  17.                 });
  18.         }
  19.         public void OnClick ()
  20.         {
  21.                 text.text = "Clicked:" + name;
  22.                 Debug.Log (text.text);
  23.         }
  24. }
复制代码
删除点击事件后, 匿名委托的事件在.
unity不支持给事件传递多个参数,但是我们可以通过传递一个GameObject类型的参数.
间接的可以访问此GameObject的所有属性.
可以把某个类放到GameObject上.然后把这个GameObject传进来.
比如:整一个 test.cs脚本如下
  1. using UnityEngine;
  2. using System.Collections;

  3. public class test : MonoBehaviour
  4. {
  5.         public string name;
  6.         public int age;
  7.         public float hight;
  8.         public bool isDead;
  9.         void Start ()
  10.         {
  11.                 name = "kitty";
  12.                 age = 1;
  13.                 hight = 2;
  14.                 isDead = false;
  15.         }
  16. }
复制代码
把这个脚本放到一个GameObject上面.
然后把这个GameObjcet物体拖拽到 放有UIEventTest2.cs脚本的按钮身上.
UIEventTest2.cs
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;

  5. public class UIEventTest2 : MonoBehaviour
  6. {
  7.         public void OnClick (GameObject obj)
  8.         {
  9.                 test t = obj.GetComponent<test> ();
  10.                 Debug.Log ("[pet attribute]");
  11.                 Debug.Log ("name:" + t.name);
  12.                 Debug.Log ("age:" + t.age);
  13.                 Debug.Log ("hight:" + t.hight);
  14.                 Debug.Log ("isAlive:" + !t.isDead);
  15.         }
  16. }
复制代码
在按钮的属性中OnClick中选择我们自己定义的按钮点击处理事件.把这个拥有test脚本的GameObject物体给这个事件函数做参数.
然后就可以用了.


作者: q283215159    时间: 2015-8-30 09:26
谢谢分享~ 通过代码操作事件通知有什么好处呢?




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5