unity扩展方法的使用

    科技2024-12-05  36

    前言

    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.方法的使用

    可以直接在其他脚本中使用这个方法: 输出结果:

    Processed: 0.010, SQL: 8