using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class JsonKeyClick : MonoBehaviour {
//将开启的关存入levels中每次awake时进行展现开启关
public static List<Level> levels=new List<Level> ();
void Awake(){
print ("Awake前:"+levels.Count);
JsonParseCtr.ReLoad (levels);
print ("Awake后:"+levels.Count);
foreach(Level e in levels){
print(e.ID+" : "+e.Name+" : "+e.Star);
}
DiTuLoadByList ();
}
// Use this for initialization
void Start () {
foreach(Transform tf in transform){
UIEventListener.Get (tf.FindChild ("Key1").gameObject).onClick = loadLevel;
}
}
public static void DiTuLoadByList(){
//提取所有关的对象,检测是否在字典中,在则点亮,根据难度数点亮相应的星星
GameObject[] sons = GameObject.FindGameObjectsWithTag("Door");
foreach (GameObject son in sons) {
foreach(Level e in levels){
if(son.gameObject.name==e.Name){
print ("匹配的关名:"+son.gameObject.name);
if (!son.transform.FindChild ("Key1").gameObject.activeSelf) {//没激活的则激活,和显示星星数量
son.transform.FindChild ("Key1").gameObject.SetActive (true);
son.transform.FindChild ("Button1").gameObject.SetActive (false);
//print (e.Name+" 星星数量:"+e.Star);
for (int i = 2; i <=3; i++) {
if(e.Star>=i){
string star = "Star"+i;//获取要点亮的star
son.transform.FindChild ("Key1").FindChild (star).GetComponent<UISprite> ().spriteName = "JinXing_02";
}
}
} else {//已经激活的则改变星星的数量就可以
//print (e.Name+" 星星数量:"+e.Star);
for (int i = 2; i <=3; i++) {
if(e.Star>=i){
string star = "Star"+i;//获取要点亮的star
son.transform.FindChild ("Key1").FindChild (star).GetComponent<UISprite> ().spriteName = "JinXing_02";
}
}
}
break;
}
}
}
}
//选择图层
public void loadLevel(GameObject obj){
print (obj.transform.parent.gameObject.name);
setSliderByNanDu (obj.transform.parent.gameObject.name);//根据难度数改变slidderSpeed
LoadLevel.ApplicationLoad(obj.transform.parent.gameObject.name);
}
//获取上一关的level与要打开的关进行匹配
public static void SetNextDoor(int n){
if(JsonParseCtr.GetLockById(n)>0){//要开启的下一关,如果未开启则开启,已开启则不关
JsonParseCtr.UpdateLockById (n);
}
n = n - 1;
if(JsonParseCtr.GetStarById(n)<3){//当前关的难度加一,即星星的数量加一,后面可以根据难度数选择不同的level
JsonParseCtr.UpdateStarById (n,JsonParseCtr.GetStarById(n)+1);
}
}
public void setSliderByNanDu(string objName){//根据难度数更改Slider下滑的速度
foreach(Level e in levels){
if(e.Name==objName){
switch (e.Star) {
case 1:
SliderLive.slidderSpeed = 0.1f;
break;
case 2:
SliderLive.slidderSpeed += 0.05f;
break;
case 3:
SliderLive.slidderSpeed += 0.05f;
break;
}
}
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
public class JsonParseCtr : MonoBehaviour {
public static string fileName="MyJsonFile.json";
public static string Path = Application.streamingAssetsPath;
public static JsonData jd;
static JsonParseCtr(){
string strJson = JsonParse.LoadFile (Path,fileName);
Debug.Log (strJson);
jd = JsonMapper.ToObject (strJson);
}
public static void ReLoad(List<Level> _levels){
_levels.Clear ();
JsonData jsonArray=jd["levels"];
if(jsonArray.IsArray){
for (int i = 0; i < jsonArray.Count; i++) {
int n = int.Parse (jsonArray[i]["Lock"].ToJson());
if(n==0){
//JsonMapper.ToJson(jsonArray[i])使用对象Level的公有属性进行映射赋值,相当于java使用setXX,getXX映射
//所以Json中的键要与Level中的公有属性相同
Level temp = JsonMapper.ToObject<Level> (JsonMapper.ToJson(jsonArray[i]));
_levels.Add (temp);
}
}
}
}
public static void UpdateStarById(int id,int value){
JsonData jsonArray=jd["levels"];
if(jsonArray.IsArray){
for (int i = 0; i < jsonArray.Count; i++) {
int n = int.Parse (jsonArray[i]["ID"].ToJson());
print ("value:"+value+" Id:"+n);
if(n==id){
jsonArray [i] ["Star"] = value;
break;
}
}
}
JsonParse.CreateOrWriteConfigFile (Path,fileName,JsonMapper.ToJson(jd));
}
public static void UpdateLockById(int id)
{
print ("nihaoById");
JsonData jsonArray=jd["levels"];
if(jsonArray.IsArray){
for (int i = 0; i < jsonArray.Count; i++) {
int n = int.Parse (jsonArray[i]["ID"].ToJson());
if(n==id){
jsonArray [i] ["Lock"] = 0;
break;
}
}
}
JsonParse.CreateOrWriteConfigFile (Path,fileName,JsonMapper.ToJson(jd));
}
public static int GetStarById(int id){
int num = 0;
JsonData jsonArray=jd["levels"];
if(jsonArray.IsArray){
for (int i = 0; i < jsonArray.Count; i++) {
int n = int.Parse (jsonArray[i]["ID"].ToJson());
if(n==id){
num = int.Parse (jsonArray [i] ["Star"].ToJson ());
break;
}
}
}
return num;
}
public static int GetLockById(int id){
int num = 1;
JsonData jsonArray=jd["levels"];
if(jsonArray.IsArray){
for (int i = 0; i < jsonArray.Count; i++) {
int n = int.Parse (jsonArray[i]["ID"].ToJson());
print ("GetLockById:"+n);
if(n==id){
num = int.Parse (jsonArray [i] ["Lock"].ToJson ());
print ("num:"+num);
break;
}
}
}
return num;
}
}
using UnityEngine;
using System.Collections;using System.IO;
public class JsonParse : MonoBehaviour {
public static void CreateOrWriteConfigFile(string path,string fileName,string info){
File.WriteAllText (path+"//"+fileName,info);
}
public static void DeleteFile(string path,string fileName){
File.Delete (path+"//"+fileName);
}
public static string LoadFile(string path,string fileName){
string readPath = path + "//" + fileName;
if (File.Exists (readPath)) {
string text = File.ReadAllText (readPath);
return text;
} else {
return null;
}
}
}
using UnityEngine;
using System.Collections;
public class Level {
public int ID {
get;
set;
}
public string Name {
get;
set;
}
public int Star {
get;
set;
}
public int Lock {
get;
set;
}
public Level(int _id,string _name,int _star,int _lock){
ID = _id;
Name = _name;
Star = _star;
Lock = _lock;
}
public Level(){
}
}
//MyJsonFile.json
{"levels":[{"ID":1,"Name":"Container_KeyButton1","Star":2,"Lock":0},{"ID":2,"Name":"Container_KeyButton2","Star":3,"Lock":0},{"ID":3,"Name":"Container_KeyButton3","Star":1,"Lock":0},{"ID":4,"Name":"Container_KeyButton4","Star":1,"Lock":1},{"ID":5,"Name":"Container_KeyButton5","Star":1,"Lock":1},{"ID":6,"Name":"Container_KeyButton6","Star":1,"Lock":1}]}