關卡界面選擇


using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ShowTitle : MonoBehaviour {

    void Start()
    {
        //獲取關卡編號
        int index = Singleton.GetInstance ().currentLevelIndex;
        //顯示關卡編號到UI
        GetComponent<Text> ().text = "Level " + index.ToString ();
    }
}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class NumberLimit : MonoBehaviour {

    public void OnNumberTextValueChange(string msg)
    {
        //如果用戶輸入的是一個數字
        if (msg != "" && msg != "-") {
            //獲取用戶輸入的數字
            int num = System.Convert.ToInt32 (msg);
            //限制用戶輸入的字符為0-3
            num = Mathf.Clamp (num, 0, 3);
            //把規范的數字顯示到輸入框
            GetComponent<InputField> ().text = num.ToString ();
        } else {
            //如果用戶輸入了‘-’,手動設置為空字符串“”
            GetComponent<InputField> ().text = "";
        }
    }

}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class OKButton : MonoBehaviour {

    //輸入框
    public InputField inputF;

    private Singleton ins;

    void Start()
    {
        ins = Singleton.GetInstance ();
    }

    public void OnOKButtonClick()
    {
        //存儲當前關卡所獲得的星星數量

        //第幾關
        int levelIndex = ins.currentLevelIndex;
        //幾顆星(默認0個)
        int stars = 0;
        //如果用戶有輸入內容,將用戶輸入星星數量保存
        if (inputF.text != "") {
            stars = System.Convert.ToInt32(inputF.text);
        }
        //判斷字典內是否有當前關卡的數據
        if (ins.data.ContainsKey (levelIndex)) {
            //更新當前關的數據(星星數量)
            ins.data [levelIndex] = Mathf.Max (stars, ins.data [levelIndex]);
        } else {
            //添加該關卡的數據
            ins.data.Add (levelIndex, stars);
        }
        //最大關卡數
        ins.maxLevelIndex++;
        //切換回選擇關卡場景
        SceneManager.LoadScene("LevelSelect");
    }
}

第三個腳本引用到的單例在下面,單例中儲存了關卡數

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Singleton {

    //單例
    private static Singleton instance;

    /// <summary>
    /// 獲取單例
    /// </summary>
    /// <returns>The instance.</returns>
    public static Singleton GetInstance()
    {
        if (instance == null) {
            instance = new Singleton ();
        }
        return instance;
    }
    //構造私有化
    private Singleton()
    {
        //實例化字典
        data = new Dictionary<int, int> ();
    }
    /// <summary>
    /// 當前選擇的關卡編號
    /// </summary>
    public int currentLevelIndex = 0;
    /// <summary>
    /// 關卡所對應的星星數量
    /// </summary>
    public Dictionary<int,int> data;
    //當前玩家玩到的最高關卡
    public int maxLevelIndex = 1;
}

接下來進入另外一個場景就是具體關卡數量

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class SelectedController : MonoBehaviour {

    /// <summary>
    /// 關卡按鈕點擊事件
    /// </summary>
    /// <param name="currentButton">被點擊的按鈕.</param>
    public void OnLevelButtonClick(Transform currentButton)
    {
        //獲取當前按鈕是否被鎖
        bool active = currentButton.GetChild (2).gameObject.activeSelf;
        //如果當前按鈕沒被鎖定
        if (!active) {
            //設置該按鈕為選擇框的父物體
            transform.SetParent (currentButton);
            //設置相對於父物體的坐標為000,即將紅框移動到所點擊的按鈕身上
            transform.localPosition = Vector3.zero;
        }
    }
}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class YesButton : MonoBehaviour {

    private Transform selected;

    void Start()
    {
        //查找到紅框
        selected = GameObject.FindWithTag ("Selected").transform;
    }

    /// <summary>
    /// 確定按鈕點擊事件
    /// </summary>
    public void OnYesButtonClick()
    {
        //記錄關卡編號
        int index = System.Convert.ToInt32(selected.parent.
            GetChild(0).GetComponent<Text>().text);
        //傳入單例存儲
        Singleton.GetInstance ().currentLevelIndex = index;
        //切換場景
        SceneManager.LoadScene("GameStar");
    }

}

最后是星星解鎖后的顯示

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ShowStars : MonoBehaviour {

    private Singleton ins;

    void Start()
    {
        Init ();
    }

    /// <summary>
    /// 關卡信息初始化
    /// </summary>
    void Init ()
    {
        //獲取數據
        Dictionary<int,int> data = Singleton.GetInstance ().data;
        //遍歷數據
        foreach (var item in data) {
            //獲取當前的關卡
            Transform currentLevel = transform.GetChild (item.Key - 1);
            //顯示星星
            currentLevel.GetChild (1).gameObject.SetActive (true);
            //隱藏鎖
            currentLevel.GetChild (2).gameObject.SetActive (false);
            //臨時存儲星星的父對象
            Transform currentStars = currentLevel.GetChild (1);
            //0、1、2、3四種情況
            switch (item.Value) {
            case 0:
                //隱藏三顆星星
                for (int i = 0; i < currentStars.childCount; i++) {
                    //隱藏
                    currentStars.GetChild (i).gameObject.SetActive (false);
                }
                break;
            case 1:
                for (int i = 1; i < currentStars.childCount; i++) {
                    //隱藏
                    currentStars.GetChild (i).gameObject.SetActive (false);
                }
                break;
            case 2:
                //隱藏第三顆星星
                currentStars.GetChild (1).gameObject.SetActive (false);
                break;
            case 3:
                break;
            default:
                break;
            }
        }
        //解鎖下一關
        transform.GetChild (data.Count).GetChild (2).gameObject.SetActive (false);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM