基础篇.委托,event

-1 ngui的子类父类

///子类的panel
public class ChildPanel : MonoBehaviour
{
    public UIButton btn3;
    void Start()
    {
        btn3 = transform.Find("btn3").GetComponent<UIButton>();
        btn3.onClick.Add(new EventDelegate(OnClickBtn3));
    }
    public Action HandleAct;
    private void OnClickBtn3()
    {
        print("Btn3--一部分方法体放在这里");
        if (HandleAct!=null)
        {
            HandleAct();
        }
    }
}
//父类的panel
public class ParentPanel : MonoBehaviour
{
  private TestPanel scriptTestPanel;
  void Start(){
      scriptTestPanel = GameObject.Find("Panel").GetComponent<TestPanel>();
      scriptTestPanel.HandleAct = TestMethod;
  }
   private void TestMethod()
   {
      Debug.Log("方法内容 一部交给父类实现");
   }
}

0 普通的方法委托

public cl`这里写代码片`ass UIView{
    public delegate void GetDelegateName(string name);
    public static  void DGName(string str, GetDelegateName dg)
    {   
        //调用者传入了str-- 每个调用都必须做这个方法  
        //...方法体中....做事情中... 
        str = "hello "+str;   //必做内容
        //这里做完了此类能做的事之后,让调用者继续做自已想做的事
        dg( str);//返回去调用做自己事情
    }
  }
  //调用者类中,写的
    public void TestSprit()
    {
        UIView.DGName("曾思信", ShowDGName);
        UIView.DGName("曾思信", delegate(string name)
        {
            Debug.Log("匿名方法 " + name);
        });
        UIView.DGName("曾思信", del =>
        {
            Debug.Log("做自己事情-Lambda " + del);
        });
    }
    private void ShowDGName(string name)
    {
        Debug.Log("做自己事情-方法" + name);
    }

1

    /// <summary>    定义 委托 (UGUI)   </summary>
    public delegate void UIViewAction(params object[] parameter);
    //定义方法
        public void ButtonAddListening(Button btn, UIViewAction callBack, bool isScale, params object[] pars)
    {
        btn.onClick.AddListener(
            () => { callBack(pars); });//把pars绑上
    }
    //调用,方法绑定了参数
            this.View.ButtonAddListening(gradeScrollView.SelectButton, RecourdButtonEvent,false, gradeScrollView);
            //    绑定了回调参数
            private void RecourdButtonEvent(object[] param)
            {
            }

2.

//UGUI
 public void ButtonAddListening(Button btn, UnityAction action, bool isScale = false)
    {
        btn.onClick.AddListener(action);
    }
   // 调用写法1.
    this.View.ButtonAddListening(this.View.buttonClose, Click_Close);
       private void Click_Close()
    {
        UIManager.Instance.HideUI(EUIViewID.dialog_view);
    }
    //调用写法2,3
     this.View.ButtonAddListening(this.View.buttonClose, () => {
            UIManager.Instance.HideUI(EUIViewID.dialog_view);
        });
        this.View.ButtonAddListening(this.View.buttonClose, delegate {
            UIManager.Instance.HideUI(EUIViewID.dialog_view);
        });

3

 void ActionMethod(Action action)
    {
        Debug.Log("执行后,再执行action");
        action();  //action相当于 调用者可以 继续写 未完成的 语句,action是方法
    }
    public void click_button()
    {
        ActionMethod(() => { Debug.Log("  test "); });
    }

4

public void click_button()
    {
        ActionHasMethod("detail", s =>
        {
            Debug.Log("s="+s);
        });
    }
    /// <summary>
    /// Action<T>  
    /// </summary>
    void ActionHasMethod(string yourname, Action<string> action)
    {
        string firstStr = "Welcome to CSDN ";
        action(firstStr + yourname);//得到这拼接的字符串,在调用者中继续写的的代码
    }

5

//定义委托
  public delegate void TimerCallBack();
    public class Tick
    {
    //...类参数
        public TimerCallBack cbfunc;
    }
    //定义方法
        public int AddDeltaTimer(float interval, int count, float start, TimerCallBack func){
        //...todo方法语句
        Tick tick=new Tick();
        tick.cbfunc=func;
        //...方法语句
        return 1;
        }
        //调用 
        Timer.Instance.AddDeltaTimer(1, 1, 3, RemoveEffect);
        //RemoveEffect
        void RemoveEffect(){
        }

6.

//定义事件
    public event EventHandler OnRecvData = null;
 private void EndReceive(IAsyncResult result)
    {
        try
        {
            Client client = result.AsyncState as Client;
            int len = client.socket.EndReceive(result);
            if (len > 0)
            {
                byte[] data = new byte[len];
                Buffer.BlockCopy(client.data, 0, data, 0, len);
                if (OnRecvData != null)
                    OnRecvData(new object[] { client.socket, data }, null);//把事件传入
                client.Receive(data);//待定             
                BeginReceive(client);
            }
            else
            {
                Disconnect(client.socket);
            }
        }
        catch (Exception ex)
        {
            Disconnect((Socket)result.AsyncState);
        }
    }
    //别一个类的调用
    AysnServer m_SocketMain;
    void Start()
    {
            m_SocketMain = new AysnServer();
            m_SocketMain.OnRecvData += m_SocketMain_OnRecvData;
 }
   void m_SocketMain_OnRecvData(object sender, EventArgs e)
    {
        object[] obj = sender as object[];
        Socket sck = (Socket)obj[0];
        string data = System.Text.Encoding.UTF8.GetString((byte[])obj[1]);
  //todo
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值