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

3 篇文章 0 订阅

00.结果图+视频演示

视频演示:

https://download.csdn.net/download/weixin_43784914/86088069

结果图

菜单页面

菜单主页

游戏开始界面:

在这里插入图片描述

暂停:

在这里插入图片描述

继续:

在这里插入图片描述

再来一局:

在这里插入图片描述

返回:

在这里插入图片描述

退出:

在这里插入图片描述

赢得游戏界面:

在这里插入图片描述

1.前期准备软件

Unity

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

2.制作整体思路

制作步骤

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.

3.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<Rigidbody> ();
	}
	#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。给三个场景添加序号。

4.源代码

整体源代码链接:
链接:https://pan.baidu.com/s/1GnbqngE-6sjZvlWrt6Cb8g?pwd=cxoi
提取码:cxoi

当时用的unity版本应该是unity 5
链接:https://pan.baidu.com/s/1I_tWla7h29Zq36TuWROsng?pwd=rv6e
提取码:rv6e

菜单界面代码

游戏界面代码

  • 1
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值