一、初始化項(xiàng)目開發(fā)環(huán)境
1)項(xiàng)目創(chuàng)建

2)導(dǎo)入NGUI插件
3)相關(guān)資源導(dǎo)入
4)Atlas制作(由UI中的圖片導(dǎo)入)

二、初始屏幕自適應(yīng)
原因:Android手機(jī)屏幕尺寸不一樣,屏幕分辨率千奇百怪
該實(shí)戰(zhàn)用16:9的分辨率自適應(yīng) 1920*1080? ?1280*720? 960*540? 720*405
三、NGUI初步實(shí)現(xiàn)UI自適應(yīng)
Game面板創(chuàng)建先使用的分辨率尺寸


UIRoot組件相關(guān)設(shè)置:1)縮放類型,手機(jī)自適應(yīng) 內(nèi)容寬高度 填寫數(shù)值,勾選fit

四、開始界面UI



五、地圖生成算法之菱形布局
1)Resources動(dòng)態(tài)加載資源
①新建資源目錄“Resources”存放預(yù)制體文件。!一定要叫這個(gè)名字!
②使用Resources.Load("資源名")進(jìn)行動(dòng)態(tài)資源加載。

private GameObject m_prefab_tile;
void Start () {
? ? ? ? m_prefab_tile = Resources.Load("tile_white") as GameObject;
}
2)代碼生成地圖菱形布局
兩層for循環(huán)嵌套生成長(zhǎng)方形的地面效果。
Quaternion.Euler(Vector3)可以將三維向量轉(zhuǎn)換為四元數(shù)。
private void CreateMapItem()
? ? {
? ? ? ? for (int i = 0; i < 10; i++)
? ? ? ? {
? ? ? ? ? ? for (int j = 0; j < 5; j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Vector3 pos = new Vector3(j*0.254f, 0, i*0.254f);
? ? ? ? ? ? ? ? Vector3 rot = new Vector3(-90, 0, 0);
? ? ? ? ? ? ? ? GameObject tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;//實(shí)例化生成,默認(rèn)位置,無旋轉(zhuǎn)
? ? ? ? ? ? ? ? tile.GetComponent<Transform>().SetParent(m_Transform);//設(shè)置父物體
? ? ? ? ? ? }
? ? ? ? }
? ? }

3)等腰直角三角形求底邊長(zhǎng)實(shí)現(xiàn)單排菱形平鋪
等腰直角三角形求底邊:2的平方根 乘以 直角邊長(zhǎng)
unity代碼實(shí)現(xiàn):Mathf.Sqrt(2)*n;

private void CreateMapItem()
? ? {
? ? ? ? for (int i = 0; i < 10; i++)
? ? ? ? {
? ? ? ? ? ? for (int j = 0; j < 5; j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Vector3 pos = new Vector3(j * Mathf.Sqrt(2) * 0.254f, 0, i * Mathf.Sqrt(2) * 0.254f);
? ? ? ? ? ? ? ? Vector3 rot = new Vector3(-90, 45, 0);//地板變成斜的了
? ? ? ? ? ? ? ? GameObject tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;//實(shí)例化生成,默認(rèn)位置,無旋轉(zhuǎn)
? ? ? ? ? ? ? ? tile.GetComponent<Transform>().SetParent(m_Transform);//設(shè)置父物體
? ? ? ? ? ? }
? ? ? ? }
? ? }
4)菱形地圖雙排布局
第二排菱形位置需要偏移,偏移量為半個(gè)底邊長(zhǎng)度

//第二排
? ? ? ? for (int i = 0; i < 10; i++)
? ? ? ? {
? ? ? ? ? ? for (int j = 0; j < 4; j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Vector3 pos = new Vector3(j * bottomLength + bottomLength / 2, 0, i * bottomLength + bottomLength / 2);
? ? ? ? ? ? ? ? Vector3 rot = new Vector3(-90, 45, 0);
? ? ? ? ? ? ? ? GameObject tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;//實(shí)例化生成,默認(rèn)位置,無旋轉(zhuǎn)
? ? ? ? ? ? ? ? tile.GetComponent<Transform>().SetParent(m_Transform);//設(shè)置父物體
? ? ? ? ? ? }
? ? ? ? }
調(diào)顏色

private Color color1 = new Color(124/255f, 155/255f, 230/255f);
tile.GetComponent<MeshRenderer>().material.color = color1;
?tile.GetComponent<Transform>().FindChild("normal_a2").GetComponent<MeshRenderer>().material.color = color1;//找到子物體,再找他的組件meshrender改顏色,顏色rbg要除以255加個(gè)f

將預(yù)制體wall,修改金屬度,拖入resources文件夾中,在腳本中找到該預(yù)制體

因?yàn)閴Ρ跊]有子物體,所以FindChild這句要放在else里面?!?/p>


六、地圖生成算法之?dāng)?shù)據(jù)管理
1)美化瓷磚地圖
①攝像機(jī)位置與角度調(diào)整
②燈光位置與角度一集顏色調(diào)整
③墻壁模型重新定義顏色

2)地圖數(shù)據(jù)存儲(chǔ)
3)地圖數(shù)據(jù)測(cè)試
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 地圖管理器
/// </summary>
public class MapManager : MonoBehaviour {
? ? private GameObject m_prefab_tile;
? ? private GameObject m_prefab_wall;
? ? //地圖數(shù)據(jù)存儲(chǔ)
? ? private List<GameObject[]> mapList = new List<GameObject[]>();
? ? private Transform m_Transform;
? ? private float bottomLength = Mathf.Sqrt(2) * 0.254f;
? ? private Color color1 = new Color(124/255f, 155/255f, 230/255f);
? ? private Color color2 = new Color(125/255f,169/255f,233/255f);
? ? private Color color3 = new Color(87 / 255f, 93 / 255f, 169 / 255f);
void Start () {
? ? ? ? m_prefab_tile = Resources.Load("tile_white") as GameObject;
? ? ? ? m_prefab_wall = Resources.Load("wall2") as GameObject;
? ? ? ? m_Transform = gameObject.GetComponent<Transform>();
? ? ? ? CreateMapItem();
}
? ? /// <summary>
? ? /// 創(chuàng)建地圖元素(段)
? ? /// </summary>
? ? private void CreateMapItem()
? ? {
? ? ? ? //單排
? ? ? ? for (int i = 0; i < 10; i++)
? ? ? ? {
? ? ? ? ? ? GameObject[] item = new GameObject[6];
? ? ? ? ? ? for (int j = 0; j < 6; j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Vector3 pos = new Vector3(j * bottomLength, 0, i * bottomLength);
? ? ? ? ? ? ? ? Vector3 rot = new Vector3(-90, 45, 0);
? ? ? ? ? ? ? ? GameObject tile = null;
? ? ? ? ? ? ? ? if (j == 0 || j == 5)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? tile = GameObject.Instantiate(m_prefab_wall, pos, Quaternion.Euler(rot)) as GameObject;//實(shí)例化生成,默認(rèn)位置,無旋轉(zhuǎn)
? ? ? ? ? ? ? ? ? ? tile.GetComponent<MeshRenderer>().material.color = color3;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;//實(shí)例化生成,默認(rèn)位置,無旋轉(zhuǎn)
? ? ? ? ? ? ? ? ? ? tile.GetComponent<Transform>().FindChild("normal_a2").GetComponent<MeshRenderer>().material.color = color1;
? ? ? ? ? ? ? ? ? ? tile.GetComponent<MeshRenderer>().material.color = color1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? tile.GetComponent<Transform>().SetParent(m_Transform);//設(shè)置父物體
? ? ? ? ? ? ? ? item[j] = tile;
? ? ? ? ? ? }
? ? ? ? ? ? mapList.Add(item);
? ? ? ? ? ? //第二排
? ? ? ? ? ? GameObject[] item2 = new GameObject[5];
? ? ? ? ? ? for (int j = 0; j < 5; j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Vector3 pos = new Vector3(j * bottomLength + bottomLength / 2, 0, i * bottomLength + bottomLength / 2);
? ? ? ? ? ? ? ? Vector3 rot = new Vector3(-90, 45, 0);
? ? ? ? ? ? ? ? GameObject tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;//實(shí)例化生成,默認(rèn)位置,無旋轉(zhuǎn)
? ? ? ? ? ? ? ? tile.GetComponent<MeshRenderer>().material.color = color2;
? ? ? ? ? ? ? ? tile.GetComponent<Transform>().FindChild("normal_a2").GetComponent<MeshRenderer>().material.color = color2;
? ? ? ? ? ? ? ? tile.GetComponent<Transform>().SetParent(m_Transform);//設(shè)置父物體
? ? ? ? ? ? ? ? item2[j] = tile;
? ? ? ? ? ? }
? ? ? ? ? ? mapList.Add(item2);
? ? ? ? }
? ? }
void Update () {
? ? ? ? if (Input.GetKeyDown(KeyCode.Space))
? ? ? ? {
? ? ? ? ? ? string str = "";
? ? ? ? ? ? for (int i = 0; i < mapList.Count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? for (int j = 0; j < mapList[i].Length; j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? str += mapList[i][j].name;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? str += "\n";
? ? ? ? ? ? }
? ? ? ? ? ? Debug.Log(str);
? ? ? ? }
}
}