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

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

  • 00.结果图+视频演示
    • 视频演示:
    • 结果图
      • 菜单页面
      • 游戏开始界面:
      • 暂停:
      • 继续:
      • 再来一局:
      • 返回:
      • 退出:
      • 赢得游戏界面:
  • 1.前期准备软件
    • Unity
  • 2.制作整体思路
    • 制作步骤
  • 3.C#脚本
    • Again:
    • Back:
    • Begin:
    • Continue:
    • Exit:
    • Stop:
    • FollowTarget:
    • PickUp:
    • Play:
  • 4.源代码
    • 菜单界面代码
    • 游戏界面代码

00.结果图+视频演示

视频演示:

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

结果图

菜单页面

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

游戏开始界面:

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

暂停:

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

继续:

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

再来一局:

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

返回:

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

退出:

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

赢得游戏界面:

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

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

菜单界面代码

游戏界面代码

你可能感兴趣的:(Unity,C#,unity,游戏开发,游戏,小球吃金币,C#)