玲珑杯Unity开发心得——游戏中暂停及积分显示GUI

//////////////////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<mishuzengjia>();
           // m_score = obj.GetComponent<Player2>();
           // m_time = obj.GetComponent<textTest2>();

        }
    }
    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>();  

        //设置循环播放  

        _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<jifencontrol>();
        }
	}

	// Update is called once per frame
	void Update () {
        GameController.GetComponent<jifencontrol>().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<jifencontrol>().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);
        }

    }

}

这个暂停呢,用了按钮加贴图,感觉还不错,资源不给了,自己找贴图吧。。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-03 22:04:39

玲珑杯Unity开发心得——游戏中暂停及积分显示GUI的相关文章

玲珑杯Unity开发心得——进度条界面(异步加载游戏场景)

/////////////2015/08/04////////////// ////////////by xbw//////////////////// ///////////环境 unity 4.6////////// 先上效果图 还可以吧,先贴一下代码, using UnityEngine; using System.Collections; public class Loading4 : MonoBehaviour { public Texture2D load_write; //进度条底

玲珑杯Unity开发心得——开始菜单GUI制作

///////////////2015/08/04////////////// //////////////by xbw///////////////////// /////////////环境  unity 4.6////////// 先看一下效果图 不错吧,,, 来代码 using UnityEngine; using System.Collections; [RequireComponent(typeof(AudioSource))] public class MainMenuGUI :

玲珑杯Unity开发心得——欢迎界面淡出并且转场景

 ///////////2015/08/04/////////// ///////////by xbw///////////////// ///////////环境 unity4.6/////// 直接贴代码了,挂在给主摄像机就好了,其中有个class类直接放在文件夹就好: DisplayTextureFullScreen.. var graphic = TextureGUI(); //(28,23); var GUIColor:Color; function OnGUI() { GUI.c

Unity 开发心得(1)

C#与JS用于unity开发的区别: C#中定义函数必须声明函数类型(void等),但是JS中可以未知函数类型直接定义(function) 注意 C#脚本在Rect前有个new JS脚本没有. 例:在unity中创建GUI控件 在unity中使用脚本都需要绑定游戏对象.例如GUI需要绑定在camera对象中才能在Game界面中显示出来.具体的操作是,选中游戏对象(例如camera),点击右边Inspector中的"Add Component"并选择需要绑定的脚本. C#脚本: void

细数那些年我们一起玩过的Unity3D游戏(unity开发的游戏有哪些)

经典重现<新仙剑OL> <新仙剑OL>采用跨平台Unity3D引擎,耗资数千万,历时三年多,由台湾大宇正版授权,"仙剑之父"姚壮宪监制的全球首款Unity3D航母级双端(网页和客户端)中国风MMORPG网络游戏巨作.主打温情牌并且延续了仙剑系列的国风雅韵,人物塑造细腻唯美,场景构建精致逼真. <蒸汽之城>(City of Steam) 由国内游戏公司参与开发的Unity3D页游<蒸汽之城>(City of Steam)在北美地区呼声颇高,

玲珑杯Uinty开发心得——安卓平台发布

///////////2015/08/04/////////// ///////////by xbw///////////////// ///////////环境 unity4.6/////// 有些日子没写了,想的是把这个玲珑杯的工程昨晚再整理呢,今天终于结束了,先想到哪写哪吧:: 先说说最终的安卓版环境配置,这个关于配置网上有很多介绍了,我在这里说一下细节吧, 配置好了在switch platform时总会出现一些错误,可能是js脚本的问题,等等,总会有一些报错,但是查找资料后发现了一个好东

Unity开发Oculus游戏

1.下载Oculus对应自己操作系统的SKD&RunTime 下载地址 我这里是window系统,下载的是0.6.0.0版本的 2.安装RunTime,电脑右下角状态栏会出现一个小眼睛表示安装成功,具体参数设置方法我这里就不说了,网上有一大堆的教程 3.把下载好的SDK导入Unity,有两个文件夹,打开OVR-Prefabs,里面有两个预设体,一个是第一人称的,另一个是第三人称的,我们这里拿第三人称的举例 打开一个原来已有的工程,把SDK导入 我拿了之前的格斗游戏的Demo,把OVRCamera

如何在cocos2d-x中使用ECS(实体-组件-系统)架构方法开发一个游戏?

引言 在我的博客中,我曾经翻译了几篇关于ECS的文章.这些文章都是来自于Game Development网站.如果你对这个架构方式还不是很了解的话,欢迎阅读理解 组件-实体-系统和实现 组件-实体-系统. 我发现这个架构方式,是在浏览GameDev上的文章的时候了解到的.很久以前,就知道了有这么个架构方法,只是一直没有机会自己实践下.这一次,我就抽空,根据网上对ECS系统的讨论,采用了一种实现方法,来实现一个. 我很喜欢做游戏,所以同样的,还是用游戏实例来实践这个架构方法.我将会采用cocos2

游戏中水的渲染技术系列一

笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解>电子工业出版社等. CSDN视频网址:http://edu.csdn.net/lecturer/144 水的渲染一直是图形学需要解决的问题,网上也有很多关于这方面的技术实现,本博客的系列文章也是给读者做一个总结,本篇博客主要介绍用傅里叶变换算法实现的水反射,也是一种假反射效果,目的是优