标签:获取 实例 点击 onclick cto mono 选择 level alt
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); } }
标签:获取 实例 点击 onclick cto mono 选择 level alt
原文地址:http://www.cnblogs.com/VR-1024/p/6021050.html