WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Unity、c#中的拓展方法讲解

Posted on   WebEnh  阅读(244)  评论(0编辑  收藏  举报

很早以前看过这个东西,但是没有真正的用到过,希望之后会用到上面的方法,

大概的意思是这样的c#中尤其在用Unity的时候,已有的框架提供给我们一些固定的方法,但是有时我们想对这些方法进行修改,

这时我们就用到了拓展方法,也可以称为c#中的语法糖。

不过需要注意几点:

  • 类必须是static的,即静态类,但是不能继承MonoBehaviour类。
  • 静态类中的方法同样需要是static的。
  • 传入的参数需要有this修饰符修饰,如 public static void SetPositionX(this Transform trans){}
  • 如果写的拓展方法在一个命名空间中,在别的类用到的时候,需要引入命名空间,如 using Extends;

下面直接上代码,比较简单,明白意思即可。

复制代码
复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 namespace Extends {                                      //注意命名空间
 4     public static class extendTransform {                         //静态类
 5         public static void SetPositionX(this Transform trans, float x) {       //静态方法,注意this的位置
 6             trans.position = new Vector3(x, trans.position.y, trans.position.z);
 7         }
 8         public static bool HaveZero(this Transform trans) {               //可以有返回值
 9             return (trans.position==new Vector3(0,0,0));10         }
11     }
12 }
复制代码
复制代码

以上为拓展方法的定义代码,接下来是运用。

复制代码
复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 using Extends;                      //引入拓展方法命名空间
 4 public class Test : MonoBehaviour {
 5     public GameObject obj;
 6     void Start () {
 7         Debug.Log(obj.transform.position);
 8         obj.transform.SetPositionX(10);        //直接调用方法
 9     }
10 }
复制代码
复制代码

接下来粘贴一些从网上获取到的样例,帮助大家理解。具体作者不详。

复制代码
复制代码
 1 using UnityEngine;  
 2 using System.Collections;  
 3    
 4 public static class Extensions  
 5 {  
 6     public static void SetPositionX(this Transform t, float newX)  
 7     {  
 8         t.position = new Vector3(newX, t.position.y, t.position.z);  
 9     }  
10     
11     public static void SetPositionY(this Transform t, float newY)  
12     {  
13         t.position = new Vector3(t.position.x, newY, t.position.z);  
14     }  
15     
16     public static void SetPositionZ(this Transform t, float newZ)  
17     {  
18         t.position = new Vector3(t.position.x, t.position.y, newZ);  
19     }  
20     
21     public static float GetPositionX(this Transform t)  
22     {  
23         return t.position.x;  
24     }  
25     
26     public static float GetPositionY(this Transform t)  
27     {  
28         return t.position.y;  
29     }  
30     
31     public static float GetPositionZ(this Transform t)  
32     {  
33         return t.position.z;  
34     }  
35     
36     public static bool HasRigidbody(this GameObject gobj)  
37     {  
38         return (gobj.rigidbody != null);  
39     }  
40     
41     public static bool HasAnimation(this GameObject gobj)  
42     {  
43         return (gobj.animation != null);  
44     }  
45     
46     public static void SetSpeed(this Animation anim, float newSpeed)  
47     {  
48         anim[anim.clip.name].speed = newSpeed;   
49     }  
50 }
复制代码
复制代码

使用:

复制代码
复制代码
 1 using UnityEngine;  
 2 using System.Collections;  
 3     
 4 public class Player : MonoBehaviour   
 5 {  
 6     void Update ()   
 7     {  
 8         float currentX = transform.GetPositionX();  
 9         transform.SetPositionX(currentX + 5f);    
10         if(gameObject.HasRigidbody())  
11         {  
12         }  
13         if(gameObject.HasAnimation())  
14         {  
15             gameObject.animation.SetSpeed(2f);  
16         }  
17     }  
18 }
复制代码
复制代码

 

以后会不定期更新一些小的知识点,希望与大家共同提高。

 

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多