Unity實(shí)戰(zhàn)之方塊跑酷(一)

一、初始化項(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)建先使用的分辨率尺寸

創(chuàng)建多個(gè)分辨率
圖標(biāo)縮小

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

四、開始界面UI


初始界面UI
商城界面UI
游戲界面UI

五、地圖生成算法之菱形布局

1)Resources動(dòng)態(tài)加載資源

①新建資源目錄“Resources”存放預(yù)制體文件。!一定要叫這個(gè)名字!

②使用Resources.Load("資源名")進(jìn)行動(dòng)態(tài)資源加載。

prefabs文件中的地板磚預(yù)制體拖到Resources文件夾中,創(chuàng)建一個(gè)空物體,講c#腳本掛載其上

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è)置父物體

? ? ? ? ? ? }

? ? ? ? }

? ? }

修改攝像機(jī)參數(shù)值,正交模式,活用Transform組件右上角的copy component和paste

3)等腰直角三角形求底邊長(zhǎng)實(shí)現(xiàn)單排菱形平鋪

等腰直角三角形求底邊:2的平方根 乘以 直角邊長(zhǎng)

unity代碼實(shí)現(xiàn):Mathf.Sqrt(2)*n;

地板轉(zhuǎn)了45°,兩塊地板之間距離變?yōu)樾边?,調(diào)整SIZE

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)顏色

當(dāng)出現(xiàn)高光時(shí),把預(yù)制體中的金屬度調(diào)為0

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);

? ? ? ? }

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、Unity簡(jiǎn)介 1. Unity界面 Shift + Space : 放大界面 Scene界面按鈕渲染模式2D...
    MYves閱讀 8,662評(píng)論 0 22
  • 這是今天的成果吧,雖然還是沒有全部完成,但是大致的功能已經(jīng)實(shí)現(xiàn),飛機(jī)的移動(dòng),限制移動(dòng)范圍,子彈的發(fā)射,消滅敵人,敵...
    Unity學(xué)習(xí)的路上閱讀 2,944評(píng)論 1 2
  • 年初的時(shí)候是沒有什么計(jì)劃的。每天按部就班的上下班,也沒有時(shí)間和精力去想什么了。2018年過去一半了,現(xiàn)在時(shí)間充裕,...
    零落花泥閱讀 394評(píng)論 0 2
  • 時(shí)間會(huì)懂得 并不是所有的努力付出都會(huì)有回報(bào)。 有些事,就算拼盡全力 卻發(fā)現(xiàn)別人輕而易舉完成你不能完成的事。 但是,...
    我們的青春不后悔閱讀 142評(píng)論 0 0
  • 引言 眾所周知,數(shù)據(jù)挖掘算法并非十全十美,在某些情況下他們也會(huì)失效。 使用 K 均值算法(K-Means)時(shí)就可能...
    Datartisan數(shù)據(jù)工匠閱讀 571評(píng)論 0 3

友情鏈接更多精彩內(nèi)容