Unity实现Flappy Bird游戏开发实战

时间:2021-05-20

本文实例为大家分享了Unity实现Flappy Bird游戏的具体代码,供大家参考,具体内容如下

参考:腾讯课程(零基础制作像素鸟)
环境:Unity2017.2.0f3

主界面(Main)的制作

没有什么技巧性

注意点:

1.写好Button的点击效果,并在UI上添加效果
2.切换界面的实现不需要通过load,直接设置SetActive()true or false 来的更快更效率

// 比如:当点击打开解释说明的按钮时候 public void clickOpenExplainScene() { if (!explainScene.activeSelf) { explainScene.SetActive (true); } if (startScene.activeSelf) { startScene.SetActive (false); } }

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

游戏主场景

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窗口制作

导入到iOS设备上

File- BuildSetting - 加入所有场景,- iOS - build
会产生xcode的项目文件

在xcode中打开,在General里设置下证书(网上教程很多,不需要99刀也能真机测试)

主要遇到的问题

  • 为了设置分辨率需要调出Game窗口
  • 类中的变量,需要提前初始化好,不然就为空报错。要么从UI上吧对应的组件拖下来,要么写一句getComponent
  • BGM需要拖出来自立个类写,因为他不随场景变化而消失或者重复创建
  • 真机测试的时候,Bundle ldentifier不能乱写,假如出现security问题,需要在手机-通用-描述文件与设备管理中,点击信任你的苹果账号。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章