【Unity】M_studio-随机RoughLike项目优化:动态代码生成对象

在跟着麦扣学习随机房间生成的过程中,其中墙壁的生成是通过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 文件夹里开始写。
用 / 表示子文件夹。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值