0%

简单在Unity中使用c#扩展方法

扩展方法声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace MyHelp
{
public static class MyClass
{
/// <summary>
/// 返回Hello World
/// </summary>
/// <param name="x"></param>
/// <returns>Hello World</returns>
public static string Print(this Transform x)
{
return "Hello World";
}
}
}

扩展方法调用

1
2
3
4
5
6
7
8
9
10
using UnityEngine;
using MyHelp;

public class Test : MonoBehaviour
{
public void _Test()
{
transform.Print();
}
}