Unity添加自定义拓展方法ExternMethod

本文介绍如何在Unity3D中利用C#扩展方法提高代码可读性和效率,通过创建静态类和静态修饰符,实现对现有类如Transform的操作,如设置位置的特定轴值,同时提供了实例代码和拓展方法示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文原创版权归 Unity3D学习网 所有,转载请详细注明原创作者及出处,以示尊重!

作者:Unity3D学习网

原文:http://www.unity.5helpyou.com/2550.html

如果这篇文章对你有帮助,敬请关注作者 Unity3D学习网 系列教程。


通常你会发现你不能修改正在使用的那些类,无论它是基础的数据类型还是已有框架的一部分,它提供的方法让你困苦不堪。不过。。C# 提供了一种巧妙的方式来让你扩充已有的类,也就是我们今天要讲的扩展方法。

扩展方法由于很方便而被经常使用到,我们更愿意叫他语法糖豆(syntactic sugar),一个实用样例是Unity的Transform类,比如:你只想设置Transform.position中Vector3的x轴,而其它两个轴保持不变。

?
1
2
3
4
5
6
7
8
9
10
11
using UnityEngine;
using System.Collections;
 
public class Player : MonoBehaviour
{
void Update ()
{
//Set new x position to 5
transform.position = new Vector3(5f, transform.position.y, transform.position.z);
}
}

在这个样例中,如果你只设置Transform.position的x轴变量,编译会报错,所以你不得不把其余的y轴和z轴加上组成一个完整的Vector3。于是一个拓展方法比如SetPositionX() 就可以添加到Transform类中,从而让代码的可读性更好。

为了创建拓展方法,你需要创建一个静态类。此外,一个拓展方法的声明前缀也必须加上静态修饰符static,同时该方法的第一个参数为所操作的类型,且必须以this关键字修饰。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
using UnityEngine;
using System.Collections;
 
//Must be in a static class
public static class Extensions
{
//Function must be static
//First parameter has "this" in front of type
public static void SetPositionX( this Transform t, float newX)
{
t.position = new Vector3(newX, t.position.y, t.position.z);
}
}

现在你可以在其它脚本里用这个新的拓展方法替换掉旧的代码。

?
1
2
3
4
5
6
7
8
9
10
11
using UnityEngine;
using System.Collections;
 
public class Player : MonoBehaviour
{
void Update ()
{
//Set new x position to 5
transform.SetPositionX(5f);
}
}

提示:拓展方法只能在实例中被调用,而不能在类本身内部使用。

这里有一些样例代码来帮你更好理解拓展方法,以作参考。

拓展:

?
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using UnityEngine;
using System.Collections;
 
public static class Extensions
{
public static void SetPositionX( this Transform t, float newX)
{
t.position = new Vector3(newX, t.position.y, t.position.z);
}
 
public static void SetPositionY( this Transform t, float newY)
{
t.position = new Vector3(t.position.x, newY, t.position.z);
}
 
public static void SetPositionZ( this Transform t, float newZ)
{
t.position = new Vector3(t.position.x, t.position.y, newZ);
}
 
public static float GetPositionX( this Transform t)
{
return t.position.x;
}
 
public static float GetPositionY( this Transform t)
{
return t.position.y;
}
 
public static float GetPositionZ( this Transform t)
{
return t.position.z;
}
 
public static bool HasRigidbody( this GameObject gobj)
{
return (gobj.rigidbody != null );
}
 
public static bool HasAnimation( this GameObject gobj)
{
return (gobj.animation != null );
}
 
public static void SetSpeed( this Animation anim, float newSpeed)
{
anim[anim.clip.name].speed = newSpeed;
}
}

使用示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;
using System.Collections;
 
public class Player : MonoBehaviour
{
void Update ()
{
//move x position 5 units
float currentX = transform.GetPositionX();
transform.SetPositionX(currentX + 5f);
 
if (gameObject.HasRigidbody())
{
//Do something with physics!
}
 
if (gameObject.HasAnimation())
{
//Double the animation speed!
gameObject.animation.SetSpeed(2f);
}
}
}
提示:静态类不能拓展MonoBehaviour类。
提示:如果你是在某个命名空间内部定义的拓展方法,那么在调用时,你必须添加using指令以包含这个命名空间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值