前言
unity扩展方法的使用
一、为什么要扩展方法
当我们需要重复使用untiy没有的函数方法时,反复的编写代码比较麻烦,这时可以使用扩展方法来一次编写,多次使用
二、使用方法
1.编写方法
首先创建一个脚本,创建一个静态的类:
using System
.Collections
;
using System
.Collections
.Generic
;
using UnityEngine
;
public static class GetInt
{
public static Vector2 Round(this Vector3 v
)
{
int x
= Mathf
.RoundToInt(v
.x
);
int y
= Mathf
.RoundToInt(v
.y
);
return new Vector2(x
, y
);
}
}
2.方法的使用
可以直接在其他脚本中使用这个方法: 输出结果:
转载请注明原文地址:https://blackberry.8miu.com/read-35106.html