服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity实现Flappy Bird游戏开发实战

Unity实现Flappy Bird游戏开发实战

2022-03-07 13:15amy260231120 C#

这篇文章主要为大家详细介绍了Unity实现Flappy Bird游戏开发实战,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

主界面(main)的制作

没有什么技巧性

注意点:

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

Unity实现Flappy Bird游戏开发实战

?
1
2
3
4
5
6
7
8
9
// 比如:当点击打开解释说明的按钮时候
  public void clickopenexplainscene() {
    if (!explainscene.activeself) {
      explainscene.setactive (true);
    }
    if (startscene.activeself) {
      startscene.setactive (false);
    
  }

Unity实现Flappy Bird游戏开发实战

2.因为不管是哪个场景,背景音乐都只有一个。所以背景音乐通过单例模式实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 实现背景音乐播放的单例类
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实现Flappy Bird游戏开发实战

游戏主场景

1 场景的来回切换

通过2个场景,来回播放实现
前一个场景完全移除屏幕后,立刻重新设置坐标,并且让柱子的位置随机出现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// mapmove.cs
using 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检测碰撞,停止移动地图并销毁鸟

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 碰撞检测
  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 音效设置和鸟的朝向问题

因为鸟震动翅膀的声音需要和其他音效不是一个线程,所以只能单独领出来写

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// playercontroller.cs
using 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 分数的计算

这里需要再次用到触碰检测,给柱子之间空隙加个透明的检测器,每次一过柱子就加一分

用单例类存储分数等数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// gamesingleton.cs
using 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实现Flappy Bird游戏开发实战

导入到ios设备上

file- buildsetting - 加入所有场景,- ios - build
会产生xcode的项目文件

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

Unity实现Flappy Bird游戏开发实战

主要遇到的问题

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

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

原文链接:https://blog.csdn.net/amy260231120/article/details/78532481

延伸 · 阅读

精彩推荐
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28