国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity實現Flappy Bird游戲開發實戰

2019-10-29 19:41:03
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Unity實現Flappy Bird游戲的具體代碼,供大家參考,具體內容如下

參考:騰訊課程(零基礎制作像素鳥) 
環境:Unity2017.2.0f3

主界面(Main)的制作

沒有什么技巧性

注意點:

1.寫好Button的點擊效果,并在UI上添加效果 
2.切換界面的實現不需要通過load,直接設置SetActive()true or false 來的更快更效率

Unity,游戲開發

// 比如:當點擊打開解釋說明的按鈕時候  public void clickOpenExplainScene() {    if (!explainScene.activeSelf) {      explainScene.SetActive (true);    }     if (startScene.activeSelf) {      startScene.SetActive (false);    }    }

Unity,游戲開發

2.因為不管是哪個場景,背景音樂都只有一個。所以背景音樂通過單例模式實現

// 實現背景音樂播放的單例類using System.Collections;using System.Collections.Generic;using UnityEngine;public class BGSingleton : MonoBehaviour {  private static BGSingleton instance = null;  public AudioSource audioSource = null;  public static BGSingleton getSingleton() {    if (instance == null) {      instance = new BGSingleton ();    }    return instance;  }  void Awake () {    if (instance != null && instance != this) {      Destroy (this.gameObject);    } else {      instance = this;      Debug.Log ("create");    }    DontDestroyOnLoad (this.gameObject);  }//通過主界面上的開關button控制是否靜音  public void IsPlay(bool isPlay) {    if (isPlay == true) {      audioSource.mute = false;      Debug.Log ("play background music");    } else {      audioSource.mute = true;    }  }}

Unity,游戲開發

游戲主場景

1 場景的來回切換 

通過2個場景,來回播放實現 
前一個場景完全移除屏幕后,立刻重新設置坐標,并且讓柱子的位置隨機出現

// mapMove.csusing System.Collections;using System.Collections.Generic;using UnityEngine;public class mapMove : MonoBehaviour {  public float speed = 300f;  public RectTransform tube1;  public RectTransform tube2;  private RectTransform transform;  // Use this for initialization  void Awake () {    transform = GetComponent<RectTransform>();   }  // Update is called once per frame  void Update () {    // Translate:Moves the transform in the direction and distance of translation.    // If you add or subtract to a value every frame chances are you should multiply with Time.deltaTime.    // When you multiply with Time.deltaTime you essentially express:     // I want to move this object 10 meters per second instead of 10 meters per frame.    transform.Translate (Vector3.left * Time.deltaTime * speed);    // 如果前一個地圖移到-764以外的地方,重放到764    if (transform.anchoredPosition.x <= -764) {      transform.anchoredPosition = new Vector2 (764, 0);      // 設置水管的位置為隨機出現,x不變,y隨機出現      tube1.anchoredPosition = new Vector2 (tube1.anchoredPosition.x, Random.Range (-110, 200));      tube2.anchoredPosition = new Vector2 (tube2.anchoredPosition.x, Random.Range (-110, 200));    }  }}

2 主要是鳥和柱子接觸后產生的碰撞檢測,首先需要設置鳥和柱子都為is Trigger觸發器,因為這里不需要物理的碰撞效果

提前給所有柱子和上下面設置好tag,通過tag檢測碰撞,停止移動地圖并銷毀鳥

// 碰撞檢測  void OnTriggerEnter2D (Collider2D other)  {    // 如果鳥碰到柱子    if (other.gameObject.CompareTag("tube")) {      if (!gameOver.activeSelf) {        gameOver.SetActive (true);        audioManager.singer.setAudio (audioClipType.hit);      }      // 通過地圖的名字獲取到地圖移動腳本      mapMove map1 = GameObject.Find ("map1").GetComponent<mapMove> ();      map1.enabled = false;      mapMove map2 = GameObject.Find ("map2").GetComponent<mapMove> ();      map2.enabled = false;      Rigidbody2D playerRigidBody2D = GameObject.Find ("player").GetComponent<Rigidbody2D> ();      Destroy (playerRigidBody2D);    }  }

3 音效設置和鳥的朝向問題

因為鳥震動翅膀的聲音需要和其他音效不是一個線程,所以只能單獨領出來寫

// playerController.csusing System.Collections;using System.Collections.Generic;using UnityEngine;public class playerController : MonoBehaviour {  private Rigidbody2D player;  public AudioSource playerAudio;  public float speed;  // Use this for initialization  void Start () {    player = GetComponent<Rigidbody2D>();    // 重置分數    gameSingleton.getSingleton ().score = 0;  }  // Update is called once per frame  void Update () {    // 如果鳥被銷毀游戲結束了,直接返回    if (player == null) { return; }    // 當點擊鼠標,給鳥一個向上的速度    if (Input.GetMouseButtonDown (0)) {      player.velocity = new Vector2(0, speed);//     audioManager.singer.setAudio (audioClipType.wing);      // 因為翅膀的聲音和過柱子的聲音,不能是同個線程的      if (!gameSingleton.getSingleton ().isMute) {        playerAudio.Play();      }    }    // 通過判斷鳥的速度正負設計鳥的頭的轉向,    if (player.velocity.y > 0) {      transform.eulerAngles = new Vector3 (0, 0, 45);    } else {      transform.eulerAngles = new Vector3 (0, 0, -45);    }  }}

4 分數的計算

這里需要再次用到觸碰檢測,給柱子之間空隙加個透明的檢測器,每次一過柱子就加一分

用單例類存儲分數等數據:

// gameSingleton.csusing System.Collections;using System.Collections.Generic;using UnityEngine;public class gameSingleton {  // 使用單例模式記錄分數  // 顯然單例模式的要點有三個;一是某個類只能有一個實例;  // 二是它必須自行創建這個實例;三是它必須自行向整個系統提供這個實例。  // 從具體實現角度來說,就是以下三點:一是單例模式的類只提供私有的構造函數,  // 二是類定義中含有一個該類的靜態私有對象,三是該類提供了一個靜態的公有的函數用于創建或獲取它本身的靜態私有對象。  public float score;  public float bestScore;  public bool isMute; // 用來控制音效// public bool isFirstToPlay;  // 含有一個靜態私有對象,這也是唯一一個對象  private static gameSingleton singer;  // 私有的構造函數  private gameSingleton () {    score = 0;    bestScore = 0;    isMute = false;//   isFirstToPlay = true;  }  // 提供一個靜態的公有函數 用于創建或獲取本身的靜態私有對象  public static gameSingleton getSingleton() {    if (singer == null) {      singer = new gameSingleton ();    }    return singer;  }  public void setBestScore() {    if (score > bestScore) {      bestScore = score;    }  }}

5 最后的gameover界面的動畫效果,可以通過Unity的Animation窗口制作

Unity,游戲開發

導入到iOS設備上

File- BuildSetting - 加入所有場景,- iOS - build 
會產生xcode的項目文件

在xcode中打開,在General里設置下證書(網上教程很多,不需要99刀也能真機測試)

Unity,游戲開發

主要遇到的問題

  • 為了設置分辨率需要調出Game窗口
  • 類中的變量,需要提前初始化好,不然就為空報錯。要么從UI上吧對應的組件拖下來,要么寫一句getComponent
  • BGM需要拖出來自立個類寫,因為他不隨場景變化而消失或者重復創建
  • 真機測試的時候,Bundle ldentifier不能亂寫,假如出現security問題,需要在手機-通用-描述文件與設備管理中,點擊信任你的蘋果賬號。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沭阳县| 栖霞市| 夹江县| 马山县| 临清市| 新竹市| 金沙县| 佳木斯市| 涟源市| 安泽县| 高平市| 本溪市| 洪江市| 吉木萨尔县| 昭苏县| 和静县| 天水市| 镇康县| 忻城县| 正镶白旗| 武乡县| 阳东县| 紫阳县| 上杭县| 临潭县| 莱西市| 兴义市| 桓台县| 星子县| 陵川县| 开远市| 定远县| 吴江市| 庄河市| 昭觉县| 祁门县| 固原市| 汉源县| 唐海县| 廊坊市| 信阳市|