Unity3d制作2D游戏飞翔的小鸟(FlappyBird)
目錄
一、學(xué)習(xí)方向
二、制作步驟
1.新建項(xiàng)目
3.設(shè)置為精靈模式
4.精靈圖像分割
5.場(chǎng)景布局設(shè)置
6.錄制小鳥(niǎo)飛翔動(dòng)畫(huà)
7.構(gòu)建飛翔的小鳥(niǎo)讓其響應(yīng)
?8.構(gòu)建UI對(duì)象并讓其響應(yīng)
9.構(gòu)建游戲背景并讓其移動(dòng)
10.障礙生成制作
三、結(jié)語(yǔ)
一、學(xué)習(xí)方向
1.學(xué)習(xí)設(shè)置2D圖形
2.搭建2D游戲場(chǎng)景
3.錄制小鳥(niǎo)飛翔動(dòng)畫(huà)
4.構(gòu)建飛翔的小鳥(niǎo)讓其響應(yīng)
5.構(gòu)建UI對(duì)象并讓其響應(yīng)
6.構(gòu)建游戲背景并讓其移動(dòng)
7.障礙生成制作
二、制作步驟
1.新建項(xiàng)目
新建unity2D視圖,命名為FlappyBird。(注意:項(xiàng)目名稱(chēng)和存儲(chǔ)路徑中不要包含中文)
2、導(dǎo)入資源與場(chǎng)景設(shè)置
在unity中把我們需要的資源文件Fonts和Sprites拖拽到項(xiàng)目視圖文件夾Assets中如下圖(選中解壓文件夾直接拖動(dòng)即可)
素材鏈接:https://pan.baidu.com/s/1au5i39kyDubrhRejYtZT9w?pwd=x46m?
提取碼:x46m
3.設(shè)置為精靈模式
(1).在工具欄中找到Windows點(diǎn)擊,然后在下拉菜單中找到Package Manager 點(diǎn)擊彈出如下窗口。
(2).在加號(hào)后的菜單中選擇Unity? Registry
(3).在Packages選項(xiàng)中找到2D Sprite然后導(dǎo)入(install)即可
4.精靈圖像分割
在Assets文件夾中的sprits文件夾中找到我們的BirdHero選中,然后在Inspector(檢視視圖)中,將Texture Type設(shè)置為Sprite(2D and UI),將Sprite Mode設(shè)置為Multiple(也就是將單個(gè)更改為多個(gè)),最后在彈出的窗口中選擇Apply(應(yīng)用)。(如下圖所示)?
在點(diǎn)擊Apply 之后會(huì)彈出新的窗口,在新的窗口最上方菜單欄中選擇Slice下拉菜單中點(diǎn)擊Slice,這樣我們就將BirdHero圖片分成了三個(gè)小鳥(niǎo)貼圖。
關(guān)閉這個(gè)窗口后,在我們的Sprite文件夾的小鳥(niǎo)素材中就可以看到有三個(gè)小鳥(niǎo)素材(如果你的依然顯示為BirdHero,不要著急我們只需要點(diǎn)擊后面的小三角即可顯示。)
5.場(chǎng)景布局設(shè)置
(1)將BridHero_01拖到Hierarchy(層級(jí)視圖)中,重命名為Bird
(2)依次將Grass Thin Sprite和Sky Tile Sprite拖拽到Hierarchy(層級(jí)視圖)中重命名為Ground和Background
(3)調(diào)整圖層順序
選擇我們需要調(diào)整的圖層,在(Inspector)檢視視圖中找到Order in Layer設(shè)置數(shù)字,將Bird設(shè)置為3,Ground設(shè)置為1,BackGround設(shè)置為0(效果圖如下)
6.錄制小鳥(niǎo)飛翔動(dòng)畫(huà)
(1)給Bird添加組件Polygen Collider 2D和Rigidbody 2D Component
給Ground添加Box Collider 2D,調(diào)整屬性,設(shè)置參數(shù)(如下圖),運(yùn)行一下會(huì)發(fā)現(xiàn)小鳥(niǎo)會(huì)掉落在草地上。
(2)這時(shí)的小鳥(niǎo)還不會(huì)飛翔,所以我們需要制作小鳥(niǎo)飛翔的動(dòng)畫(huà),步驟如下:
a.選擇Bird,在菜單欄中找到Window下拉菜單中的Animation。
b.在彈出的窗口中點(diǎn)擊Creat,接著會(huì)彈出新的窗口將文件命名為IDLE用來(lái)存放資源,然后點(diǎn)擊保存即可。(如下圖)
c.點(diǎn)擊錄制按鈕,然后點(diǎn)擊Add Property,選擇Sprite Renderer在下拉菜單中找到Sprite,點(diǎn)擊后面的加號(hào)。
d.這時(shí)候我們需要選中最后一個(gè)關(guān)鍵幀將其刪除(選中,delete即可)。?
e.選擇第一個(gè)關(guān)鍵幀,在Inspector(檢視視圖)中將Sprite設(shè)置為BirdHero_0?
f.選擇第一個(gè)關(guān)鍵幀復(fù)制,點(diǎn)擊左上角的IDLE下拉菜單中Creat New Clip,命名為Flappy,保存。
在Flappy面板中首先點(diǎn)擊錄制按鈕,然后將我們復(fù)制的IDLE的第一個(gè)關(guān)鍵幀粘貼在我們Flappy中,在Inspector(檢視視圖)中找到Sprite為BirdHero_1。
g.按照步驟f,新建一個(gè)Clip命名為DIE,點(diǎn)擊錄制按鈕,復(fù)制第一個(gè)關(guān)鍵幀,然后更改Sprite為BirdHero_2。到這一步我們就完成了小鳥(niǎo)飛翔的錄制。點(diǎn)擊關(guān)閉Animation,在彈出的窗口保存為Animations。(如果沒(méi)有彈出系統(tǒng)會(huì)默認(rèn)保存在Assets文件中)
(3)雙擊Bird Controller進(jìn)入Animator 面板中
?選擇IDLE右擊選擇Make Transition
將IDLRE和另外兩個(gè)(DIE和Flappy)鏈接起來(lái),然后選擇Flappy右鍵選擇Make Transition制作一條從Flappy到IDLE 的線(xiàn)
在我們的Animator 面板菜單欄中找到Parameters在下面創(chuàng)建兩個(gè)Trigger分別命名為Flappy和Die?
選擇從IDLE到DIE的線(xiàn),在Inspector(檢視視圖)中,取消勾選Has Exit Time,在下面Conditions中點(diǎn)擊加號(hào)選擇Die?
選擇從IDLE到Flappy的線(xiàn),在Inspector(檢視視圖)中,取消勾選Has Exit Time,在下面Conditions中點(diǎn)擊加號(hào)選擇Flappy(如下圖所示)。?
從Flappy到IDLE這條線(xiàn),我們希望在Flappy播放完后直接切換回IDLE,因此不需要取消Has Exit Time的勾選,也不需要設(shè)置Conditions,所以這條線(xiàn)就不用管了。
7.構(gòu)建飛翔的小鳥(niǎo)讓其響應(yīng)
回到Scene視口,首先我們?cè)贏ssets中新建一個(gè)文件夾:點(diǎn)擊Assets在空白區(qū)域右擊選擇Create在下拉列表中找到Folder就會(huì)創(chuàng)建出一個(gè)新的文件夾,這個(gè)文件夾主要存放我們的腳本
雙擊打開(kāi)我們的Scripts文件夾,然后空白區(qū)域右鍵選擇Create下拉列表中的C# Script,然后重命名為Bird,然后將我們的腳本左擊不放拖拽到Bird上面
雙擊打開(kāi)腳本,進(jìn)行編譯,這一步是鼠標(biāo)點(diǎn)擊屏幕小鳥(niǎo)向上飛翔,Ctrl+s保存腳本然后回到我們的Unity中,這時(shí)點(diǎn)擊運(yùn)行我們的游戲,就實(shí)現(xiàn)了小鳥(niǎo)向上飛翔
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Bird : MonoBehaviour {public float upperForce = 250f; // the force add to bird when it flappy.private Rigidbody2D rb2d; // the rigidbody 2d componentprivate Animator animator; // the animator componentprivate bool isDead = false; // mark if bird is dead.void Start(){rb2d = GetComponent<Rigidbody2D>();animator = GetComponent<Animator>();}void Update(){if (Input.GetMouseButtonDown(0) && !isDead){ // check input and bird's statusrb2d.velocity = Vector2.zero;rb2d.AddForce(new Vector2(0, upperForce)); // add a upper forceanimator.SetTrigger("Flappy"); // change current animation clip to flappy}}void OnCollisionEnter2D(){rb2d.velocity = Vector2.zero;isDead = true;animator.SetTrigger("Die"); // change current animation clip to Die}}?8.構(gòu)建UI對(duì)象并讓其響應(yīng)
(1)在Hierarchy(層級(jí)視圖)中右鍵找到UI在下拉列表中找到legacy(舊版)下拉列表選擇Text點(diǎn)擊創(chuàng)建,重命名為Score Text用來(lái)記錄分?jǐn)?shù)
(2)選中Score Text,更改Text屬性為Score:0,將字體改為L(zhǎng)uckiesGuy,設(shè)置字體大小Font Size為22,將Alignment屬性選擇中間兩個(gè)將字體居中,把字體顏色更改為白色。
(3)選中Score Text,Ctrl+d拷貝兩份,分別重命名為GameOverText和RestartText,參照上一步調(diào)整字體大小及位置(記得更改Text屬性哦),將RestartText拖拽到GameOverText上,讓RestartText成為GameOverText的子對(duì)象。效果圖如下
(4)在Hierarchy視圖中右鍵創(chuàng)建空物體CreateEmpty重命名為GameManager
(5)在Scripts文件夾中創(chuàng)建腳本,右鍵>create>C# script,重命名為GameManager,然后將其拖拽到我們創(chuàng)建的空物體GameManager上。
(6)雙擊打開(kāi)GameManager腳本,進(jìn)行編輯,主要是更新得分和小鳥(niǎo)死后彈出Game Over UI更新畫(huà)面Ctrl+s保存,回到unity。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System;public class GameManager : MonoBehaviour {public static GameManager instance; // single instancepublic GameObject gameOverText; // game over UIpublic Text scoreText; // score textpublic Button playBtn;public GameObject logo;private bool gameOver = false; // mark current game statusprivate int score = 0; // store score.void Awake(){if (instance == null){ // set single instanceinstance = this;}else if (instance != null){Destroy(gameObject);}}void Update(){if (gameOver == true && Input.GetMouseButtonDown(0)){ // if gameover and click the picture, restart the game.SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);}}public void AddScore() // when the bird cross a obstacle, add score.{if (gameOver)return;score += 1;scoreText.text = "Score: " + score.ToString();}public void GameOver() // game over function.{gameOverText.SetActive(true);gameOver = true;}}(7)雙擊打開(kāi)Bird腳本,添加GameOver方法GameManager.instance.GameOver(),添加在?void OnCollisionEnter2D中,Ctrl+S保存,回到unity。
void OnCollisionEnter2D(){rb2d.velocity = Vector2.zero;isDead = true;animator.SetTrigger("Die"); // change current animation clip to DieGameManager.instance.GameOver();}(8)在Hirarchy視圖中添加一個(gè)Image(圖片)重命名為logo,和一個(gè)Botton(按鈕)重命名為playBtn
選中l(wèi)ogo在Inspector視圖中更改Source Image屬性為標(biāo)題
選中?playBtn,刪除下面自帶的Text(Legacy),在Inspector視圖中更改Source Image屬性為btnPlay
調(diào)整playBtn 和logo位置及大小效果圖如下
(9)選中Hierarchy視圖中的GameManager,將Score Text、Game Over、logo 、playBtn 拖拽到Inspector相應(yīng)的位置。
選中GameOver 取消勾選Active。
(10)創(chuàng)建一個(gè)腳本命名為UIManager,將這個(gè)腳本拖拽到logo 和playBtn 上
雙擊打開(kāi)我們的GameManager腳本,在代碼最后面添加Play的定義指令(添加在GameOver后),這樣我們就可以在UIManager腳本中調(diào)用Ctrl+s保存
public void GameOver() // game over function.{gameOverText.SetActive(true);gameOver = true;//UIManager.Instance.ShowUI();}internal void Play(){throw new NotImplementedException();}接下開(kāi)打開(kāi)我們的UIManager腳本進(jìn)行編輯,Ctrl+s保存,將此腳本拖分別拽到playBtn和logo上
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; public class UIManager : MonoBehaviour {public Button playBtn;public GameObject logo;public static UIManager Instance;// Use this for initializationvoid Awake(){Instance = this;}void Start(){playBtn.onClick.AddListener(onPlay);}// Update is called once per framevoid Update(){}private void onPlay(){playBtn.gameObject.SetActive(false);logo.SetActive(false);GameManager.instance.Play();}public void ShowUI(){playBtn.gameObject.SetActive(true);} }選中l(wèi)ogo在Inspector中修改UIManager 屬性,如下圖所示?
9.構(gòu)建游戲背景并讓其移動(dòng)
(1)給Ground添加一個(gè)Rigidbody??2D的組件,將Body Type屬性更改為Kinematic(因?yàn)槭峭ㄟ^(guò)腳本來(lái)移動(dòng),而不是用物理系統(tǒng)來(lái)完成移動(dòng))
在Scripts文件夾中創(chuàng)建新的腳本重命名為ScorllingObject,Ctrl+S將其賦予到Ground上
using System.Collections; using System.Collections.Generic; using UnityEngine;public class ScrollingObject : MonoBehaviour {public float scrollSpeed = -2f; // object scroll speed.private Rigidbody2D rb2d;void Start(){rb2d = GetComponent<Rigidbody2D>();rb2d.velocity = new Vector2(scrollSpeed, 0);}void Update(){if (GameManager.instance.gameOver == true){rb2d.velocity = Vector2.zero;}}}(2)新建一個(gè)c#腳本重命名為RepeatingBackground,打開(kāi)編輯腳本,Ctrl+s保存腳本將腳本拖拽到Ground上。
using System.Collections; using System.Collections.Generic; using UnityEngine;public class RepeatingBackground : MonoBehaviour {private BoxCollider2D groundCollider;private float groundHorizontalLength; // store background's width.void Start(){groundCollider = GetComponent<BoxCollider2D>();groundHorizontalLength = groundCollider.size.x; // get background's width;}void Update(){if (transform.position.x < -groundHorizontalLength){ // when it move out of screen, reset it's position.RepositionBackground();}}void RepositionBackground(){Vector2 groundOffset = new Vector2(groundHorizontalLength * 2f, 0);transform.position = (Vector2)transform.position + groundOffset; // reset position}}(3)將Background拖拽到Ground上,讓其成為Ground的一個(gè)子對(duì)象,新建一個(gè)空物體重命名為Bg,將Ground拖拽到Bg上讓他成為Bg的子對(duì)象,調(diào)整Ground的位置,復(fù)制一份Groung,效果圖如下?。
10.障礙生成制作
(1)將ColumnSprite拖拽到Hierarchy中,為其添加一個(gè)Box Collider 2D Component組件調(diào)整大小修改屬性參數(shù),如下圖所示?
?(2)復(fù)制一份ColumnSprite,旋轉(zhuǎn),調(diào)整位置如下
(3)創(chuàng)建一個(gè)空物體命名為Column,將兩個(gè)ColumnSprite拖拽到空物體上,給空物體添加RigidBody 2D組件,設(shè)置Type為Kinematic,添加一個(gè)Box Collider 2D組件,勾選Is Trigger,調(diào)整屬性參數(shù),如下圖所示
?(4)將ScrollingObject腳本拖拽到Column上,然后新建腳本命名為ColumnCtrl+s保存,然后將腳本拖拽到Column上。
using System.Collections; using System.Collections.Generic; using UnityEngine;public class Column : MonoBehaviour {void OnTriggerEnter2D(Collider2D other){if (other.GetComponent<Bird>() != null){GameManager.instance.AddScore();}} }(5)在Project中新建文件夾命名為Prefabds,將空物體Column拖拽到文件夾中讓其成為預(yù)制件,這時(shí)你會(huì)發(fā)現(xiàn)Column變成了藍(lán)色。?
(6)新建一個(gè)腳本命名為ColumnPool,用于管理Column,將此腳本拖拽到GameManager上然后將預(yù)制文件夾中的Column拖拽到Column Prefab中如下圖所示。
using System.Collections; using System.Collections.Generic; using UnityEngine;public class ColumnsPool : MonoBehaviour {public GameObject columnPrefab; // column prefabpublic int poolSize = 5; // max size of poolpublic float spawnRate = 4.5f; // spawn ratepublic float columnYMin = -1.45f; // column's min ypublic float columnYMax = 2.8f; // column's max yprivate GameObject[] columns; // column poolprivate Vector2 startSpawnPos = new Vector2(13f, 0f); // origin start spawn position.private float spawnXPosition = 10f; // column's x not change.private float lastSpawnTime = 0; // record the last spawn time.private int currentIndex = 0; // current column's index in poolvoid Start(){columns = new GameObject[poolSize]; // init columns poolfor (int i = 0; i < poolSize; i++){columns[i] = (GameObject)Instantiate(columnPrefab, startSpawnPos, Quaternion.identity);columns[i].SetActive(false);}}void Update(){if (Time.time - spawnRate > lastSpawnTime){ // after spawnRate time, reset a column's positionlastSpawnTime = Time.time;float columnY = Random.Range(columnYMin, columnYMax); // get a random y.columns[currentIndex].transform.position = new Vector2(spawnXPosition, columnY); // reset positioncolumns[currentIndex].SetActive(true); // active columncurrentIndex++; // turn to next column.if (currentIndex >= poolSize){ // check overflowcurrentIndex = 0;}}}}(7)刪除Hierarachy中的?Column,運(yùn)行游戲測(cè)試一下即可。
(8)當(dāng)你運(yùn)行的時(shí)候發(fā)現(xiàn),你的游戲界面是這樣的,那么我們只需要修改游戲場(chǎng)景的大小即可
打開(kāi)我們的游戲視圖,點(diǎn)擊Free Aspect下拉列表找到+號(hào),創(chuàng)建一個(gè)320×480場(chǎng)景大小即可?
最后你就會(huì)得到這樣的視圖
三、結(jié)語(yǔ)
????????如果你想讓你的游戲更加有趣可以為他添加游戲音效等等,那么本次游戲制作就到此結(jié)束啦,不要忘了點(diǎn)贊加關(guān)注哦~
注意:如果發(fā)現(xiàn)本文章有侵權(quán)內(nèi)容,可以隨時(shí)聯(lián)系作者本人,本人將即使撤回進(jìn)行修改。
總結(jié)
以上是生活随笔為你收集整理的Unity3d制作2D游戏飞翔的小鸟(FlappyBird)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [英语阅读]希腊古剧场对高跟鞋说“不”
- 下一篇: 实现打印爱心