IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity——使用Unity实现小球吃金币小游戏并且导出成.exe文件 -> 正文阅读

[游戏开发]Unity——使用Unity实现小球吃金币小游戏并且导出成.exe文件

结果图+视频演示

视频演示:

视频演示地址,点击跳转

结果图

菜单页面:
菜单主页游戏开始界面:
在这里插入图片描述暂停:
在这里插入图片描述
继续:
在这里插入图片描述

再来一局:
在这里插入图片描述

返回:
在这里插入图片描述

退出:
在这里插入图片描述
赢得游戏界面:
在这里插入图片描述

前期准备软件

Unity

版本:(挑选适合自己以及自己电脑的版本)本次版本为:
在这里插入图片描述

制作整体思路

制作步骤

1.在assets下面创建三个场景,分别叫ball,ball1,menu。实现menu菜单操作,ball实现大部分操作,ball1和ball之间实现转场操作。

图1 三个场景
2.在menu场景下通过ui创建一个面板,为其命名为background,并将其背景颜色改为黑色。

图2 新建image并为其改名为background

图3 将面板颜色改为黑色
3.在menu场景下通过ui创建两个button,分别命名为playbutton和quitbutton,分别掌管开始游戏和退出游戏两种操作。并将其text改为play和quit,字体大小改为80,水平居中,颜色为红色。

图4 创建两个button,分别命名为playbutton和quitbutton

图5 将其text改为play和quit,字体大小改为80,水平居中,颜色为红色
4.在ball场景中,创建一个面板,将其大小改为2,1,2。

图6 创建一个面板,将其大小改为2,1,2
5.新建一个文件夹materias,并新建一个materia,将其颜色改为绿色,并赋给面板。

图7 新建一个materia,将其颜色改为绿色
6.在ball场景中,创建4个cube,并更改它们的大小,使得他们和面板组成如下形状。确保小球可以不掉落在面板之外的地方。

图8 创建4个cube,并更改它们的大小

图9 他们和面板组成的形状
7.再创建一个小球,新建一个materia,颜色为蓝色。将其赋给小球。添加重力。

图10 新建一个materia,颜色为蓝色
8.创建一个正方体,命名为pickup。X,Y,Z轴都旋转45度,复制12个,同时围成一个圆放置于面板上。并将它们组合在一起。为其赋于黄色。

图11 新建正方体的X,Y,Z轴都旋转45度
9.通过ui创建面板,并在面板上放置得分,胜利条件文本和暂停、继续、返回和再来一局按钮。

图12 布置面板灯最终效果图
10.编写如下C#脚本。使得脚本again实现ball和ball1之间的场景切换,脚本back实现退出游戏到主菜单,脚本begin实现通过主菜单切换到场景ball,游戏开始。脚本continue实现游戏继续。脚本exit实现退出游戏到主菜单。脚本followtarget实现相机跟随小球。脚本pickup实现小球和金币碰撞时消失。脚本play实现通过键盘上的方向键控制小球上下左右移动。脚本stop实现使小球停止动作,达到暂停游戏。
各个脚本的代码如下:

图13 10. 编写的C#脚本
Again:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Again : MonoBehaviour {

void Start(){
	
}
void Update(){
	
}
#if UNITY_EDITOR
public void jump(){
	SceneManager.LoadScene (1);
}
#endif
#if UNITY_EDITOR
public void Jump(){
	SceneManager.LoadScene (2);
}
#endif

}
Back:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Back : MonoBehaviour {

// Use this for initialization
void Start () {
	
}

// Update is called once per frame
void Update () {
	
}
#if UNITY_EDITOR
public void back(){
	SceneManager.LoadScene (0);
}
#endif

}
Begin:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Begin : MonoBehaviour {
#if UNITY_EDITOR
public void PlayGame(){
SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex+1);
}
#endif
}
Continue:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Continue : MonoBehaviour {
#if UNITY_EDITOR
public void conClick()
{
Time.timeScale = 1;
}
#endif
}
Exit:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Exit : MonoBehaviour {
#if UNITY_EDITOR
public void Quit(){
UnityEditor.EditorApplication.isPlaying = false;
Application.Quit ();
}
#endif
}
Stop:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Stop : MonoBehaviour {
#if UNITY_EDITOR
public void stopClick()
{
Time.timeScale = 0;
}
#endif
}
FollowTarget:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class FollowTarget : MonoBehaviour {
#if UNITY_EDITOR
public Transform playerTransform;
#endif
#if UNITY_EDITOR
private Vector3 offset;
#endif
// Use this for initialization
#if UNITY_EDITOR
void Start () {
offset = transform.position - playerTransform.position;
}
#endif
#if UNITY_EDITOR
// Update is called once per frame
void Update () {
transform.position = playerTransform.position + offset;
}
#endif
}
PickUp:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class PickUp : MonoBehaviour {

// Use this for initialization
void Start () {
	
}
#if UNITY_EDITOR
// Update is called once per frame
void Update () {
	transform.Rotate (new Vector3(0,5,0));//xuanzhuanshudu5
}
#endif

}
Play:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Player : MonoBehaviour {
#if UNITY_EDITOR
private Rigidbody rd;
#endif
#if UNITY_EDITOR
public int force=25;
#endif
#if UNITY_EDITOR
public Text text;
#endif
#if UNITY_EDITOR
public GameObject WinText;
#endif
#if UNITY_EDITOR
private int score=0;
#endif
#if UNITY_EDITOR
// Use this for initialization
void Start () {
rd = GetComponent ();
}
#endif
#if UNITY_EDITOR
// Update is called once per frame
void Update () {
float h = Input.GetAxis (“Horizontal”);
float v = Input.GetAxis (“Vertical”);
rd.AddForce (new Vector3(h,0,v)*force);
}
#endif
#if UNITY_EDITOR
//pengzhuangjiance
void OnCollisionEnter(Collision collision){
//string name = collision.collider.name;
//print (name);
if(collision.collider.tag==“PickUp”){
Destroy (collision.collider.gameObject);
}
}
#endif
#if UNITY_EDITOR
void OnTriggerEnter(Collider collider){
if(collider.tag==“PickUp”){
score++;
text.text = score.ToString ();
if(score==13){
WinText.SetActive (true);
}
Destroy (collider.gameObject);
}
}
#endif
}
11.调整主相机的位置,挂载各个脚本到它们相应的位置。

图14 11. 调整主相机的位置
12.选择file->buildsettings。给三个场景添加序号。

源代码

整体源代码链接:

菜单界面代码

游戏界面代码

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章           查看所有文章
加:2021-08-09 10:32:59  更:2021-08-09 10:33:45 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/8 21:23:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码