在跟着麦扣学习随机房间生成的过程中,其中墙壁的生成是通过switch来写的,但是这样写代码不方便维护和阅读
switch (room.doorOpenNumber)
{
case 1:
if (room.IsDoorTop)
{
Instantiate(wallType.Wall_U, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorDown)
{
Instantiate(wallType.Wall_D, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorLeft)
{
Instantiate(wallType.Wall_L, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorRight)
{
Instantiate(wallType.Wall_R, room.gameObject.transform.position, Quaternion.identity);
}
break;
case 2:
if (room.IsDoorTop && room.IsDoorDown)
{
Instantiate(wallType.Wall_UD, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorTop && room.IsDoorLeft)
{
Instantiate(wallType.Wall_UL, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorTop && room.IsDoorRight)
{
Instantiate(wallType.Wall_UR, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorLeft && room.IsDoorRight)
{
Instantiate(wallType.Wall_LR, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorLeft && room.IsDoorDown)
{
Instantiate(wallType.Wall_DL, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorLeft && room.IsDoorRight)
{
Instantiate(wallType.Wall_LR, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorDown && room.IsDoorRight)
{
Instantiate(wallType.Wall_DR, room.gameObject.transform.position, Quaternion.identity);
}
break;
case 3:
if (room.IsDoorTop && room.IsDoorDown && room.IsDoorLeft)
{
Instantiate(wallType.Wall_UDL, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorTop && room.IsDoorDown && room.IsDoorRight)
{
Instantiate(wallType.Wall_UDR, room.gameObject.transform.position, Quaternion.identity);
}
if (room.IsDoorTop&&room.IsDoorLeft&&room.IsDoorRight)
{
Instantiate(wallType.Wall_ULR,room.gameObject.transform.position,Quaternion.identity);
}
if (room.IsDoorDown&&room.IsDoorLeft&&room.IsDoorRight)
{
Instantiate(wallType.Wall_DLR,room.gameObject.transform.position,Quaternion.identity);
}
break;
case 4:
if (room.IsDoorDown&&room.IsDoorLeft&&room.IsDoorRight&&room.IsDoorTop)
{
Instantiate(wallType.Wall_UDLR,room.gameObject.transform.position,Quaternion.identity);
}
break;
}
改用动态加载对象
string wallpath = "RoughLikeRoom/prefab/Wall/" ;
Object Wall = Resources.Load(wallpath + GetWallType(room.IsDoorTop, room.IsDoorDown, room.IsDoorLeft, room.IsDoorRight));
Instantiate(Wall, room.gameObject.transform.position, Quaternion.identity);
/// <summary>
/// 通过判断房间四个bool来判断墙体类型
/// </summary>
/// <param name="IsDoorTop">房间top是否有门</param>
/// <param name="IsDoorDown">房间down是否有门</param>
/// <param name="IsDoorLeft">房间left是否有门</param>
/// <param name="IsDoorRight">房间right是否有门</param>
/// <returns>WallPrefabName</returns>
public string GetWallType(bool IsDoorTop, bool IsDoorDown, bool IsDoorLeft, bool IsDoorRight)
{
string WallPrefabName = "Wall_";
if (IsDoorTop)
WallPrefabName += "U";
if (IsDoorDown)
WallPrefabName += "D";
if (IsDoorLeft)
WallPrefabName += "L";
if (IsDoorRight)
WallPrefabName += "R";
// Debug.Log(WallPrefabName);
return WallPrefabName;
}
这样可以让代码通过返回的四个门的bool来判断生成哪一个prefab。
但是注意的是使用Resources.Load加载的对象必须在你想要加载的文件必须放在Resources目录下才行!并且开始Resources下级文件夹写
读取文件时的根目录是 Assets/Resources,所有资源文件都放在该文件夹下,命令中的路径从 Resources 文件夹里开始写。
用 / 表示子文件夹。