第一个案例呀哈哈哈哈哈哈

游戏设计逻辑顺序:

**第一步 设置UI;此步略
**第二步 地图自动生成、美化、储存。
1.地图效果预览
地图效果 这里写图片描述

2.地图组成分析

地图模型组成:两边高方块(墙),中间低方块(地板);
地图颜色组成:三种颜色,取色工具可取;

3.地图生成逻辑
获得预制体—->设置预制体生成(类型、方位、角度)
涉及代码:
加载 预制体 —–Resources.load<>(“”string”);
定义各种量————————类型 名;
写一个类专门存放方法————-类 FANGFA;
写一个类专门控制地图生成——–类MapManager;

FANGFA类

using UnityEngine;
using System.Collections;

public class FANGFA : MonoBehaviour {
public GameObject jiazai(string P)
{
return Resources.Load(P);
}//加载物体方法
public void setparent(GameObject OBJ, string parentname) //设置父物体;, string p
{
OBJ.gameObject.GetComponent().SetParent(GameObject.Find(parentname).transform);
}
public void setcolor(GameObject OBJ, string name, float x, float y, float z)//设置颜色;
{
Color C = new Color(x, y, z);
OBJ.GetComponent().material.color = C;//”normal_a2”
OBJ.GetComponent().FindChild(name).GetComponent().material.color = C;
}
public void setRotate(GameObject OBJ, float X, float Y, float Z, float Angle)
{
OBJ.gameObject.GetComponent().Rotate(new Vector3(X, Y, Z), Angle);
}//设置旋转
public GameObject creatObject(GameObject OBJ2, Vector3 V)//实例化对象 这里注意需要返回一个一个物体
{
return GameObject.Instantiate(OBJ2, V, Quaternion.identity) as GameObject;
}

MapManager类

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MapManager : MonoBehaviour
{

private GameObject Prewall;
private GameObject Pretile;
public  List<GameObject[]> ITEM = new List<GameObject[]>();
private GameObject tile = null;
private Transform m_Transform;
private FANGFA fangfa = new FANGFA();
void Start()
{
    Prewall = fangfa.jiazai("wall2"); //wall2载入
    Pretile = fangfa.jiazai("tile_white");//tile_white载入
    m_Transform = gameObject.GetComponent<Transform>();
    creatMap();

}
/// <summary>
/// 创建地图
/// </summary> 
void Update()
{ 
    bianli(ITEM);

}

/* if (Input.GetKeyDown(KeyCode.Space))
{
string str = “”;
for (int i = 0; i < ITEM.Count; i++)
{
for (int j = 0; j < ITEM[i].Length; j++)
{
str += ITEM[i][j].name;
ITEM[i][j].name = i + “–” + j;
}
str += “\n”;
}

        Debug.Log(str);//ITEM.Count
    }*/
private void creatMap()
{
    for (int i = 0; i < 10; i++)
    {
        float A = Mathf.Sqrt(2) * 0.254f;               //计算方块斜边长
        GameObject[] item1 = new GameObject[6];
        for (int j = 0; j < 6; j++)
        {

            if (j == 0 || j == 5)                             //边界墙
            {
                Vector3 POS1 = new Vector3(j * A, 0, i * A);
                tile = fangfa.creatObject(Prewall, POS1);//生成地板
                fangfa.setRotate(tile, 1, 0, 0, -90f);//设置旋转
                fangfa.setRotate(tile, 0, 0, 1, 45f);//设置旋转
                fangfa.setcolor(tile, null, 87 / 255f, 93 / 255f, 169 / 255f);//设置颜色;
            }
            else                                        //奇数行
            {
                Vector3 POS2 = new Vector3(j * A, 0, i * A);
                tile = fangfa.creatObject(Pretile, POS2);
                fangfa.setRotate(tile, 1, 0, 0, -90f);//设置旋转
                fangfa.setRotate(tile, 0, 0, 1, 45f);//设置旋转
                fangfa.setcolor(tile, "normal_a2", 124 / 255f, 155 / 255f, 230 / 255f);//设置颜色;

            }
            fangfa.setparent(tile,"Map");//设置父物体
            item1[j] = tile;
        }
        ITEM.Add(item1);
             GameObject[] item2 = new GameObject[5];
        for (int j = 0; j < 5; j++)                     //偶数行
        {
            Vector3 POS3 = new Vector3((j + 0.5f) * A, 0, (i + 0.5f) * A);
            tile = fangfa.creatObject(Pretile, POS3);//生成地板
            fangfa.setRotate(tile, 1, 0, 0, -90f);//设置旋转
            fangfa.setRotate(tile, 0, 0, 1, 45f);//设置旋转
            fangfa.setparent(tile, "Map");//设置父物体
            fangfa.setcolor(tile, "normal_a2", 125 / 255f, 169 / 255f, 233 / 255f);//设置颜色;
            item2[j] = tile;
        }
        ITEM.Add(item2);
    }

}
/*/// <summary>
/// 构造辅助函数
/// </summary>
/// <param name="OBJ"></param>
/// <param name="p"></param>
private void setparent(GameObject OBJ) //设置父物体;, string p
{
    //OBJ.gameObject.GetComponent<Transform>().SetParent(GameObject.Find(p).transform);
    OBJ.GetComponent<Transform>().SetParent(m_Transform);
}
private void setcolor(GameObject OBJ, string name, float x, float y, float z)//设置颜色;
{
    Color C = new Color(x, y, z);
    OBJ.GetComponent<MeshRenderer>().material.color = C;//"normal_a2"
    OBJ.GetComponent<Transform>().FindChild(name).GetComponent<MeshRenderer>().material.color = C;
}
private void setRotate(GameObject OBJ, float X, float Y, float Z, float Angle)
{
    OBJ.gameObject.GetComponent<Transform>().Rotate(new Vector3(X, Y, Z), Angle);
}//设置旋转
private GameObject creatObject(GameObject OBJ2, Vector3 V)//实例化对象    这里注意需要返回一个一个物体
{
    return GameObject.Instantiate(OBJ2, V, Quaternion.identity) as GameObject;
}*/
private void bianli(List<GameObject[]> list)
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        string str = "";
        string ctr = "";
        for (int i = 0; i < list.Count; i++)
        {
            for (int j = 0; j < list[i].Length; j++)
            {
                str += list[i][j].name;
                list[i][j].name = i + "_" + j;
                ctr += list[i][j].name;
                Debug.Log(i + "-------" + list[i][j].name + "--------" + j);
            }
            ctr += "\n";
        }
        Debug.Log(ctr);
    }
}  //遍历list集合

}

猜你喜欢

转载自blog.csdn.net/qq_37215648/article/details/78938662