//2015/08/04//
/by xbw
环境 unity 4.6/
先看一下效果图,
来两段代码
using UnityEngine;
using System.Collections;
public class jifencontrol: MonoBehaviour
{
//This script handles the leveling up in the game and the score and distance records. In each level up the value ranges for platforms are changed ( platform length/height/width/rotate etc )
protected mishuzengjia m_player;
internal GameObject Player; //Used to hold the player object, if it's in the scene
public GUISkin GUIskin; //The skin gui we'll use
internal float TotalDistance = 0; //The total distance passed by the player
public Texture2D Gems;
public int right=1;
public GUISkin GUIskin2;
public Texture2D Stop;
public int stop = 1;
void Start()
{
Player = GameObject.FindWithTag("Player"); //Find the player in the scene and put it in a variable, for later use
GameObject obj = GameObject.FindGameObjectWithTag("Player");
if (obj != null)
{
m_player = obj.GetComponent();
// m_score = obj.GetComponent();
// m_time = obj.GetComponent();
}
}
public void EndLevel()
{
PlayerPrefs.SetInt("TotalDistance", (int)TotalDistance); //Put the value of TotalDistance in a playerPref record which will keep this value even after going to a different level or even quitting the game
StartCoroutine(WaitAndDelay(2.0f));
Application.LoadLevel("end"); //Load the end screenm, which has total score displayed and a menu
}
IEnumerator WaitAndDelay(float waitTime)
{
yield return new WaitForSeconds(waitTime);
}
void OnGUI()
{
if(right==0)
{
//The skin gui we'll use
//GUIStyle aa = new GUIStyle();
//aa.fontSize = 40;
//aa.normal.textColor = new Color(255, 255, 255);
GUI.skin = GUIskin;
GUI.Label(new Rect(Screen.width * 0.5f + 70, Screen.height * 0.2f, 0, 0), Mathf.Round(TotalDistance).ToString() + " M");
GUI.Label(new Rect(Screen.width * 0.5f + 160, Screen.height * 0.3f, 0, 0), "恭喜获得四枚玲珑轮胎");
GUI.Label(new Rect(Screen.width * 0.5f + 70, Screen.height * 0.4f, 0, 0), "游戏胜利");
GUI.skin = GUIskin2;
if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.5f, Screen.width * 0.2f, Screen.height * 0.1f), "再试一次"))
{
Application.LoadLevel(Application.loadedLevelName);
Time.timeScale = 1;
}
if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.7f, Screen.width * 0.2f, Screen.height * 0.1f), "退出游戏"))
{
Application.Quit();
}
if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.6f, Screen.width * 0.2f, Screen.height * 0.1f), "返回主菜单"))
{
Application.LoadLevel("first");
Time.timeScale = 1;
}
}
if (Time.timeScale == 0||stop==0)
{
GUI.skin = GUIskin2;
Time.timeScale = 0;
if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.4f, Screen.width * 0.2f, Screen.height * 0.1f), "继续游戏"))
{
Time.timeScale = 1;
stop = 1;
}
if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.5f, Screen.width * 0.2f, Screen.height * 0.1f), "再试一次"))
{
Application.LoadLevel(Application.loadedLevelName);
Time.timeScale = 1;
stop = 1;
}
if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.7f, Screen.width * 0.2f, Screen.height * 0.1f), "退出游戏"))
{
Application.Quit();
}
if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.6f, Screen.width * 0.2f, Screen.height * 0.1f), "返回主菜单"))
{
Application.LoadLevel("first");
Time.timeScale = 1;
stop = 1;
}
}
// GUI.skin = GUIskin2;
// if (GUI.Button(new Rect(0,0, 30, 30), ""))
// {
// Time.timeScale = 0;
// }
GUIStyle bb = new GUIStyle();
bb.fontSize = 40;
bb.normal.textColor = new Color(255, 255, 255);
GUI.skin = GUIskin; //The skin gui we'll use
// GUI.DrawTexture(new Rect(0,0, 32f, 32f), Stop);
GUI.Label(new Rect(Screen.width * 0.85f, Screen.height * 0.01f, 0, 0), Mathf.Round(TotalDistance).ToString() + " M",bb); //Place the distance count on the top right of the screen
GUI.DrawTexture(new Rect(Screen.width * 0.90f, Screen.height * 0.15f, Screen.width * 0.07f, Screen.width * 0.07f), Gems); //Place the gem image beside the gems count on the top right of the screen
GUI.Label(new Rect(Screen.width * 0.85f, Screen.height * 0.17f, 0f, 0f), m_player.m_score.ToString(),bb); //Place the gems count on the top right of the screen
}//ToString() 整型转字符型
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Time.timeScale = 0;
}
if(m_player.m_score==4)
{
StartCoroutine(WaitAndDelay(2.0f));
//Application.LoadLevel("end");
right = 0;
}
}
}
using UnityEngine;
using System.Collections;
public class mishuzengjia : MonoBehaviour {
// Use this for initialization
internal GameObject GameController;
public AudioClip CrashSound;
private AudioSource _audioSource;
public int m_score = 0;
public Texture2D Stop;
public GUISkin GUIskin2;
internal GameObject Player;
protected jifencontrol m_player;
void Awake()
{
//在添加此脚本的对象中添加AudioSource组件,此处为摄像机
_audioSource =this.gameObject.AddComponent();
//设置循环播放
_audioSource.loop = false;
//设置音量为最大,区间在0-1之间
_audioSource.volume = 1.0f;
//设置audioClip
// _audioSource.clip = backgroundMusic;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "shiwu1")
{
m_score += 1;
_audioSource.PlayOneShot(CrashSound);
Destroy(GameObject.FindGameObjectWithTag("shiwu1"));
//audio.PlayOneShot(CrashSound);
}
if (other.gameObject.tag == "shiwu2")
{
m_score += 1;
_audioSource.PlayOneShot(CrashSound);
Destroy(GameObject.FindGameObjectWithTag("shiwu2"));
//audio.PlayOneShot(CrashSound);
}
if (other.gameObject.tag == "shiwu3")
{
m_score += 1;
_audioSource.PlayOneShot(CrashSound);
Destroy(GameObject.FindGameObjectWithTag("shiwu3"));
//audio.PlayOneShot(CrashSound);
}
if (other.gameObject.tag == "shiwu4")
{
m_score += 1;
_audioSource.PlayOneShot(CrashSound);
Destroy(GameObject.FindGameObjectWithTag("shiwu4"));
//audio.PlayOneShot(CrashSound);
}
}
void Start () {
GameController = GameObject.FindWithTag("GameController");
//Player = GameObject.FindWithTag("Player"); //Find the player in the scene and put it in a variable, for later use
GameObject obj = GameObject.FindGameObjectWithTag("GameController");
if (obj != null)
{
m_player = obj.GetComponent();
}
}
// Update is called once per frame
void Update () {
GameController.GetComponent().TotalDistance = (int)GameObject.FindGameObjectWithTag("Player").transform.position.z;
if ((int)GameObject.FindGameObjectWithTag("Player").transform.position.x >= 28)
{
transform.Translate(Vector3.left * 25.0f);
transform.Translate(Vector3.up * 5.0f);
}
if((int)GameObject.FindGameObjectWithTag("Player").transform.position.x <= -28)
{
transform.Translate(Vector3.right * 25.0f);
transform.Translate(Vector3.up * 5.0f);
}
}
void OnGUI()
{
if (GameController.GetComponent().TotalDistance!=0)
{
GUI.skin = GUIskin2;
if (GUI.Button(new Rect(0, 0, Screen.width * 0.05f, Screen.width * 0.05f), ""))
{
m_player.stop = 0;
}
GUI.DrawTexture(new Rect(0, 0, Screen.width * 0.05f, Screen.width * 0.05f), Stop);
}
}
}
这个暂停呢,用了按钮加贴图,感觉还不错,资源不给了,自己找贴图吧。。