Unity实现Flappy Bird游戏开发实战

2020-01-05 10:06:39刘景俊

本文实例为大家分享了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;
    }
  }
}