您的位置:首页 > 移动开发 > Unity3D

【Unity】Unity中的枚举和标志

2016-02-16 17:23 162 查看
原文链接 :http://unity3d.9tech.cn/news/2014/0410/40176.html

枚举和标志

今天的主题是枚举,它是C#语言中的一个很有帮助的工具,可以增强代码的清晰度以及准确性。

枚举一系列值

C#中最经常使用枚举的地方就是用来定义一系列可能值的时候。例如在制作一个角色类游戏中,角色有多个不同的状态效果。可以用一些基本的方式来定义玩家可能的状态效果:

using UnityEngine;

public class Player : MonoBehaviour
{
public string status;

void Update()
{
if (status == "Poison")
{
//Apply poison effect
}
else if (status == "Slow")
{
//Apply slow effect
}
else if (status == "Mute")
{
//Apply mute effect
}
}
}

此处我们用一个简单的if-else检测来查看应用的是哪个状态效果,哪个运行良好。但是如果你想应用一个状态效果,但是突然写成这样:

//Oops, I spelled "Poison" wrong!
status = "Pioson";

突然,就不会有什么状态效果了。我们会利用if-else栈检测来查看我们的变量是否被设置成一个非法值,但是如果使用枚举,则是一种更好的方式:

定义一个枚举

这次我们使用一个枚举来编写我们的状态效果:

using UnityEngine;

public class Player : MonoBehaviour
{
//This is the "set" of all possible status effects
public enum StatusEffect
{
None,
Poison,
Slow,
Mute
}

//Now we can make a variable using that set as its type!
public StatusEffect status;

void Update()
{
if (status == StatusEffect.Poison)
{
//Apply poison effect
}
else if (status == StatusEffect.Slow)
{
//Apply slow effect
}
else if (status == StatusEffect.Mute)
{
//Apply mute effect
}
}
}

这里我们定义了一个叫StatusEffect的枚举,通过给状态赋值,我们强制编译器只等于StatusEffect中的一个值,如果还像之前赋值:

//This type doesn't even exist, ERROR TIME!
status = StatusEffect.Pioson;

游戏就不可能运行,编辑器会抛出一个错误,告诉你正在使用一个非法的枚举值,并指出你需要修复的确切的代码行。

在上述代码中,我没有制定状态的默认值,通常来说,一个枚举都是指定集合中的第一个值作为默认值的,因此在此例中,我已经说过,StatusEffect变量的任意默认值都是StatusEffect.None。

一个干净整洁的下拉菜单用来选择定义的类型!

枚举位标志

StatusEffect枚举,如下:

[System.Flags]
public enum StatusEffect
{
None    = 1,
Poison    = 2,
Slow    = 4,
Mute    = 8
}

通过如此,我们告知编译器将此枚举看做是位标记,表示每个不同的枚举值代表一个不同的位。这意味着我们必须通过给它们用2的幂来赋值,以此告知每个枚举值。另一种就是通过获取两个值的幂,然后通过移位来实现:

[System.Flags]
public enum StatusEffect
{
None   = 1 << 0, // 1
Poison = 1 << 1, // 2
Slow   = 1 << 2, // 4
Mute   = 1 << 3  // 8
}


现在可以通过平常的位操作来控制这些枚举域了。如果你对位操作不熟悉,我会简单讲解下如何使用。

枚举标志和位操作符

现在我们的枚举值是通过System.Flags标记的,我们可以像之前一样,如下来做:

//Character is poisoned!
status = StatusEffect.Poison;


但如果玩家中毒或者行动缓慢怎么办?我们可以使用或|操作符来将多个状态值合并一起:

//Now we are poisoned *and* slowed!
status = StatusEffect.Poison | StatusEffect.Slow;

//We could also do this:
//First we are poisoned, like so
status = StatusEffect.Poison;

//Then we become *also* slowed later
status |= StatusEffect.Slow;


如果我们想移除一个状态效果,我们可以使用&操作符和取反~。

//First, we are poisoned and muted
status = StatusEffect.Poison | StatusEffect.Mute;

//But now we want to remove just the poison, and leave him mute!
status &= ~StatusEffect.Poison;


最后如果我们检测发现如果一个玩家在if语句中,我们可以使用&操作符来查看一个特定的位值:

//Let's check if we are poisoned:
if ((status & StatusEffect.Poison) == StatusEffect.Poison)
{
//Yep, definitely poisoned!
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: