Preface
The use of unity extension method
1. Why do you want to extend the method
When we need to reuse the function method that untiy does not have, it is troublesome to write code repeatedly. At this time, we can use the extension method to write once and use it many times.
2. How to use
1. Writing method
First create a script to create a static class:
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. Use of methods
You can use this method directly in other scripts:
output results: