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

扩展方法声明

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";
        }
    }
}

扩展方法调用

using UnityEngine;
using MyHelp;

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

 

posted @ 2022-07-12 16:57  枫亦  阅读(11)  评论(0)    收藏  举报