AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > Unity3D

Unity添加自定义拓展方法

51自学网 2014-05-24 http://www.51zixue.net

通常你会发现你不能修改正在使用的那些类,无论它是基础的数据类型还是已有框架的一部分,它提供的方法让你困苦不堪。不过。。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指令以包含这个命名空间。

 


建议使用电驴(eMule)下载分享的资源。

说明
:本教程来源互联网或网友分享或出版商宣传分享,仅为学习研究或媒体推广,51zixue.net不保证资料的完整性。
 
上一篇:Unity3D Mecanim在某个状态下动画重新播放  下一篇:Unity 2D动画如何检测tap?