Unity3D开发——LeRunning商店系统的实现

/////////////////////2015/08/22//////////////////////

////////////////////by  xbw////////////////////////////

///////////////////环境  unity 4.6.1///////////////

今天又挤出一点时间来,继续写一下那个项目的内容,关于这个商店系统,在这个游戏中,实现了简单的轮胎兑换金币,也是用到了unity自带的数据库,真心好用呢,下面就详细说一下;

首先呢,轮胎哪来的呢,就是在游戏中我们的猪脚在躲避障碍的同时在收集轮胎,没一局的金币数量都可以累计,用的就是数据库呢,这样,我们在游戏轮胎数量显示的地方添加纪录游戏轮胎数增加的语句,

 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
        quanju.lun = m_player.m_score;

这一句quanju.lun = m_player.m_score;用的是全局变量,把轮胎的个数记录在全局变量中,这样不管在哪个脚本中,我们都可以使用这个变量了,不错吧,没有看到数据库的语句吧,我也没看到,再找找,再另一个脚本呢,我的End脚本,之前专家说游戏没有结束,这样一来增加了end游戏实现了结束模块,看一下代码吧

using UnityEngine;
using System.Collections;

public class End : 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 Texture2D img;
    public AudioClip CrashSound;
    private AudioSource _audioSource; 
    //public int right = 1;
    public GUISkin GUIskin2;
    //public Texture2D Stop;
    //public int stop = 1;
    //public int jieshu;
    void Awake()
    {
        _audioSource = this.gameObject.AddComponent<AudioSource>();
        _audioSource.loop = false;
        _audioSource.volume = 1.0f;
        _audioSource.pitch = 1.0f;
    }  
    void Start()
    {
        Player = GameObject.FindWithTag("Player"); //Find the player in the scene and put it in a variable, for later use
        _audioSource.PlayOneShot(CrashSound);
        GameObject obj = GameObject.FindGameObjectWithTag("Player");
        if (obj != null)
        {
            m_player = obj.GetComponent<mishuzengjia>();
        }
    }
    //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()
    {
            //The skin gui we'll use
            //GUIStyle aa = new GUIStyle();
        //aa.fontSize = 40;
        //aa.normal.textColor = new Color(255, 255, 255);

        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), img);

            GUI.skin = GUIskin;
            GUIStyle bb1 = new GUIStyle();
            bb1.fontSize = 40;
            bb1.normal.textColor = new Color(0, 0, 0);
            GUI.Label(new Rect(Screen.width * 0.4f, Screen.height * 0.25f, 0, 0),"最终距离 :  "+ quanju.mishu.ToString() + " M", bb1);
            GUI.DrawTexture(new Rect(Screen.width * 0.64f, Screen.height * 0.34f, 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.4f, Screen.height * 0.4f, 0f, 0f), "收集轮胎 :  "+quanju.lun.ToString(), bb1);
            //GUI.Label(new Rect(Screen.width * 0.5f + 160, Screen.height * 0.3f, 0, 0), "恭喜获得四枚玲珑轮胎");
            GUI.Label(new Rect(Screen.width * 0.5f, Screen.height * 0.5f, 0, 0), "游戏结束", bb1);
            PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
            quanju.LunTotal = PlayerPrefs.GetInt("LunTai");
            

            //Time.timeScale = 0;
            GUI.skin = GUIskin2;
            if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.6f, Screen.width * 0.2f, Screen.height * 0.1f), "再试一次"))
            {
                

                quanju.LunTotal += quanju.lun;
                PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
                Time.timeScale = 1;
                Application.LoadLevel("choose");
            }
            if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.8f, Screen.width * 0.2f, Screen.height * 0.1f), "退出游戏"))
            {
                

                quanju.LunTotal += quanju.lun;
                PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
                Application.Quit();
            }
            if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.7f, Screen.width * 0.2f, Screen.height * 0.1f), "返回主菜单"))
            {
              

                quanju.LunTotal += quanju.lun;
                PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
                Time.timeScale = 1;
                Application.LoadLevel("first");
            }
        }
    void Update()
    {
    }
}

PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
            quanju.LunTotal = PlayerPrefs.GetInt("LunTai");

重点这两句话,实现了对这个轮胎个数数据的读取,然后再存储

  if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.6f, Screen.width * 0.2f, Screen.height * 0.1f), "再试一次"))
            {
                

                quanju.LunTotal += quanju.lun;
                PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
                Time.timeScale = 1;
                Application.LoadLevel("choose");
            }
            if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.8f, Screen.width * 0.2f, Screen.height * 0.1f), "退出游戏"))
            {
                

                quanju.LunTotal += quanju.lun;
                PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
                Application.Quit();
            }
            if (GUI.Button(new Rect(Screen.width * 0.45f, Screen.height * 0.7f, Screen.width * 0.2f, Screen.height * 0.1f), "返回主菜单"))
            {
              

                quanju.LunTotal += quanju.lun;
                PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
                Time.timeScale = 1;
                Application.LoadLevel("first");
            }

就是点击了按钮才会存储呢,

PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
            quanju.LunTotal = PlayerPrefs.GetInt("LunTai");这是unity自带的数据库功能,真的好方便,

这样这个轮胎的个数就能很好的记录下来了,就算是重新启动游戏数据也不会丢失的,所以我们就可以有了新游戏模式跟游戏回顾模式,是不是很高大上呢,至于怎么实现,我想大家都很清楚,新游戏模式就是点击后清除所有数据,然后进入游戏场景,

 if (GUI.Button(new Rect(playButton), "新游戏"))
            {
                audio.PlayOneShot(beep);
                //menuPage = "main";
                PlayerPrefs.DeleteAll();
                Application.LoadLevel("choose");
            }

PlayerPrefs.DeleteAll();这一句就是unity自带的数据库的清除数据的语句,当然也很方便啦;

游戏回顾模式就是直接进入游戏场景就好啦;

说了半天还没到游戏商店呢,先上个图吧;

Unity3D开发——LeRunning商店系统的实现_第1张图片

这个就是商店啦,不过是用了点GUI的输入输出罢了,我们的比例是1:500;嘿嘿,都是用了数据库,直接上大片代码,是不是很爽呢;

using UnityEngine;
using System.Collections;

public class shangdian : MonoBehaviour {

    public GUISkin GUIskin;
    public Texture2D img;
    public Texture2D jinbi;
    public Texture2D luntai;
    public string FromText = "";
    private int number;
	// Use this for initialization

	void Start () {
	            
	}
	
	// Update is called once per frame
	void Update () {
	
	}
    void OnGUI()
    {
        GUI.skin = GUIskin;
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), img);
        if(quanju.shangdian==1)
        {

            GUIStyle bb1 = new GUIStyle();
            bb1.fontSize = 40;
            bb1.normal.textColor = new Color(0, 0, 0);
            GUI.Label(new Rect(Screen.width * 0.185f, Screen.height * 0.44f, 0, 0), "输入个数: ", bb1);
            
            FromText = GUI.TextField(new Rect(Screen.width * 0.31f, Screen.height * 0.44f, Screen.width * 0.1f, Screen.height * 0.08f), FromText,bb1);
            int.TryParse(FromText, out number);
            
            GUI.Label(new Rect(Screen.width * 0.69f, Screen.height * 0.44f, 0, 0), "获取: "+ (number*500).ToString(), bb1);
            GUI.DrawTexture(new Rect(Screen.width * 0.05f, Screen.height * 0.29f, Screen.width * 0.5f, Screen.height * 0.6f), jinbi);
            GUI.DrawTexture(new Rect(Screen.width * 0.55f, Screen.height * 0.29f, Screen.width * 0.5f, Screen.height * 0.6f), luntai);
            PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
            quanju.LunTotal = PlayerPrefs.GetInt("LunTai");
            PlayerPrefs.SetInt("JinBi", quanju.JinTotal);
            quanju.JinTotal = PlayerPrefs.GetInt("JinBi");
            GUI.Label(new Rect(Screen.width * 0.1f, Screen.height * 0.2f, 0, 0), "拥有轮胎: " + quanju.LunTotal.ToString(), bb1);
            GUI.Label(new Rect(Screen.width * 0.6f, Screen.height * 0.2f, 0, 0), "拥有金币: " + quanju.JinTotal.ToString(), bb1);
            if (GUI.Button(new Rect(Screen.width * 0.42f, Screen.height * 0.7f, Screen.width * 0.2f, Screen.height * 0.1f), "确认兑换"))
            {
                if (quanju.LunTotal<number)
                {

                }
                else
                {
                    quanju.LunTotal = quanju.LunTotal - number;
                    PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
                    quanju.JinTotal = quanju.JinTotal + number * 500;
                    PlayerPrefs.SetInt("JinBi", quanju.JinTotal);

                }
                
                
            }
            if (GUI.Button(new Rect(Screen.width * 0.42f, Screen.height * 0.8f, Screen.width * 0.2f, Screen.height * 0.1f), "返回"))
            {
                Application.LoadLevel("choose");
                quanju.shangdian = 0;
            }
        }
        if(quanju.shangdian==2)
        {
            
            if (GUI.Button(new Rect(Screen.width * 0.42f, Screen.height * 0.3f, Screen.width * 0.2f, Screen.height * 0.3f), "只能通过游戏"+"\n"+"获取轮胎"))
            {

            }
            if (GUI.Button(new Rect(Screen.width * 0.42f, Screen.height * 0.7f, Screen.width * 0.2f, Screen.height * 0.1f), "返回"))
            {
                Application.LoadLevel("choose");
                quanju.shangdian = 0;
            }
            
        }

        
        //GUIStyle bb1 = new GUIStyle();
        //bb1.fontSize = 30;
        //bb1.normal.textColor = new Color(0, 0, 0);
        //quanju.LunTotal = PlayerPrefs.GetInt("LunTai");
        //GUI.Label(new Rect(Screen.width * 0.5f, Screen.height * 0.5f, 0, 0), "轮胎个数" + "\n" + quanju.LunTotal.ToString() + " 个",bb1);
    }
}


FromText = GUI.TextField(new Rect(Screen.width * 0.31f, Screen.height * 0.44f, Screen.width * 0.1f, Screen.height * 0.08f), FromText,bb1);

在这之前呢,我们public了一个FormText,不是我不想直接定义整型,是GUI的输入估计只能是字符等,这样呢,只能是String了,这样呢,我就需要字符转整型了,

int.TryParse(FromText, out number);

unity中的字符转整型是这样用的,估计是其中一种用法,应该还用很多种方法,然而整型转字符型却很简单,直接是整型.ToString();是不是很爽,这样呢,我们就可以直接用number了,

 quanju.LunTotal = quanju.LunTotal - number;
                    PlayerPrefs.SetInt("LunTai", quanju.LunTotal);
                    quanju.JinTotal = quanju.JinTotal + number * 500;
                    PlayerPrefs.SetInt("JinBi", quanju.JinTotal);

点下兑换按钮,我们要相应的做出显示的变化以及存储数据,这样就好了,帅吧;,今天就这样了,

你可能感兴趣的:(Unity3D开发——LeRunning商店系统的实现)