• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

unity3DAction和Func的运用

武飞扬头像
weixin_44355867
帮助1

Action与Func是.NEt中的内置委托类型,可以直接用来定义委托而无需先声明,非常的方便

Action的简单运用

  1.  
    //使用Action,Func等.Net自带的内置委托Action和Func
  2.  
    //须引用名称为System的命名空间
  3.  
    using System;
  4.  
    using System.Collections;
  5.  
    using System.Collections.Generic;
  6.  
    using UnityEngine;
  7.  
     
  8.  
    /*//无参
  9.  
    public delegate void Action();
  10.  
    //1个参数
  11.  
    public delegate void Action<in T>(T obj);
  12.  
    //2个参数
  13.  
    public delegate void Action<in T1, in T2>(T1 Obj1, T2 obj2);*/
  14.  
     
  15.  
     
  16.  
    public class ActionTest : MonoBehaviour
  17.  
    {
  18.  
    //无参
  19.  
    Action SayGoodBayAction;
  20.  
    //一个参数
  21.  
    Action<string> SayHelloAction;
  22.  
    //两个参数
  23.  
    Action<string, string> strTestAction;
  24.  
    // Start is called before the first frame update
  25.  
    void Start()
  26.  
    {
  27.  
    SayHelloClass mySayHello = new SayHelloClass();
  28.  
    mySayHello.OnHello("Tom1");
  29.  
     
  30.  
    SayHelloAction = myTest;
  31.  
    SayHelloAction = myTest;
  32.  
    SayHelloAction = mySayHello.OnHello;
  33.  
     
  34.  
     
  35.  
    SayGoodBayAction = myTest2;
  36.  
     
  37.  
    strTestAction = myTest3;
  38.  
     
  39.  
    SayHelloAction.Invoke("Tom");
  40.  
    SayGoodBayAction.Invoke();
  41.  
     
  42.  
    strTestAction.Invoke("汤姆" , "杰瑞");
  43.  
     
  44.  
    }
  45.  
     
  46.  
    // Update is called once per frame
  47.  
    void Update()
  48.  
    {
  49.  
     
  50.  
    }
  51.  
     
  52.  
    private static void myTest( string name)
  53.  
    {
  54.  
    Debug.Log($"This is a Test,{name}");
  55.  
    }
  56.  
     
  57.  
    private static void myTest2()
  58.  
    {
  59.  
    Debug.Log("This is myTest2");
  60.  
    }
  61.  
     
  62.  
    private static void myTest3(string str1, string str2)
  63.  
    {
  64.  
    Debug.Log($"{str1} {str2}");
  65.  
    }
  66.  
    }
  67.  
     
  68.  
     
  69.  
    public class SayHelloClass : ISayHello
  70.  
    {
  71.  
    public void OnHello(string message)
  72.  
    {
  73.  
    Debug.Log($"Hello,my name is {message}");
  74.  
    }
  75.  
    }
  76.  
     
  77.  
    public interface ISayHello
  78.  
    {
  79.  
    void OnHello(string message);
  80.  
    }
学新通

Func的简单运用

  1.  
    using System;
  2.  
    using System.Collections;
  3.  
    using System.Collections.Generic;
  4.  
    using UnityEngine;
  5.  
     
  6.  
    public class Func : MonoBehaviour
  7.  
    {
  8.  
    Func<int,int,int> myFunc = null;
  9.  
    // Start is called before the first frame update
  10.  
    void Start()
  11.  
    {
  12.  
     
  13.  
    myFunc = Add;
  14.  
     
  15.  
    if(myFunc != null)
  16.  
    {
  17.  
    int result = myFunc.Invoke(2, 3);
  18.  
    Debug.Log(result);
  19.  
    }
  20.  
     
  21.  
    myFunc -= Add;
  22.  
     
  23.  
    //检查是否有误
  24.  
    try
  25.  
    {
  26.  
    myFunc.Invoke(5, 2);
  27.  
    }
  28.  
    catch
  29.  
    {
  30.  
    Debug.Log("委托可能为空");
  31.  
    }
  32.  
    finally
  33.  
    {
  34.  
    //重新指向Add函数
  35.  
    myFunc = Add;
  36.  
    Debug.Log($"result = {myFunc.Invoke(10,8)}");
  37.  
     
  38.  
    //移出Add
  39.  
    myFunc -= Add;
  40.  
     
  41.  
    //Lambda表达式
  42.  
    //指向匿名函数
  43.  
    myFunc = (x, y) =>
  44.  
    {
  45.  
    return x y;
  46.  
    };
  47.  
    Debug.Log($"result = {myFunc.Invoke(9, 6)}");
  48.  
    }
  49.  
     
  50.  
    }
  51.  
     
  52.  
    // Update is called once per frame
  53.  
    void Update()
  54.  
    {
  55.  
     
  56.  
    }
  57.  
     
  58.  
    //加法
  59.  
    int Add(int x, int y)
  60.  
    {
  61.  
    int z = x y;
  62.  
    return z;
  63.  
    }
  64.  
    //减法
  65.  
    int Mul(int x, int y)
  66.  
    {
  67.  
    int z = x - y;
  68.  
    return z;
  69.  
    }
  70.  
     
  71.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghfchc
系列文章
更多 icon
同类精品
更多 icon
继续加载