/*
* 游戏开发(Unity&C#)总结33 - Func委托
*
* Func委托是有返回值的委托,Func有多个参数时,最后一个参数代表返回值,其他的代表形参
*
* **/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Demo : MonoBehaviour
{
Func<int> func;
Func<String, int> func2;
void Start()
{
func += new Func<int>(MyMethod);
func();
//func2 += new Func<String, int>(MyMethod2);
//简易写法
func2 += MyMethod2;
func2("123");
}
public int MyMethod() {
return 10;
}
public int MyMethod2(string str)
{
int myNumber = 0;
int.TryParse(str, out myNumber);
return myNumber;
}
}
demo下载
https://gitlab.com/demofile1/uploadfile/-/tree/main