using System.Collections;
using System.Collections.Generic;
public class KeyClick : MonoBehaviour {
//将开启的关存入levels中每次awake时进行展现开启关
public static List<Level> levels=new List<Level> ();
void Awake(){
print ("Awake前:"+levels.Count);
XmlCtr.ReLoad (levels);
print ("Awake后:"+levels.Count);
foreach(Level e in levels){
print(e.ID+" : "+e.Name+" : "+e.Star);
}
DiTuLoadByList ();//通过xml的list形式加载
}
// 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(XmlCtr.GetLockById(n)>0){
XmlCtr.UpdateLockById (n);
}
n = n - 1;
if(XmlCtr.GetStarById(n)<3){
XmlCtr.UpdateStarById (n,XmlCtr.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;
public class XmlCtr {
private static ParseXml px=ParseXml.CreateInstance();
public static void ReLoad(List<Level> _levels){
_levels.Clear ();
var ls = px.LoadList ();
foreach(Level e in ls){
_levels.Add (e);
}
}
public static void UpdateStarById(int id,int value){
px.UpdateStarById (id,value);
}
public static void UpdateLockById(int id)
{
px.UpdateLockById (id);
}
public static int GetStarById(int id){
return px.GetStarById (id);
}
public static int GetLockById(int id){
return px.GetLockById (id);
}
}
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
public class ParseXml {
private static ParseXml px;
private string xmlFileName="/MyXml.xml";
private string xmlFilePath;
private List<Level> levels=new List<Level> ();
private ParseXml(){
xmlFilePath = Application.streamingAssetsPath + xmlFileName;
}
public static ParseXml CreateInstance(){
if(px==null){
px=new ParseXml ();
}
return px;
}
public List<Level> LoadList(){
levels.Clear ();
XmlDocument doc = new XmlDocument ();
doc.Load (xmlFilePath);
XmlNode root = doc.SelectSingleNode ("Levels");
foreach(XmlElement child in root){
Level temp = new Level ();
temp.ID = int.Parse(child.GetAttribute ("id"));
foreach(XmlElement e in child){
if (e.Name == "Name") {
temp.Name = e.InnerText;
} else if (e.Name == "Star") {
temp.Star = int.Parse (e.InnerText);
} else {
temp.Lock = int.Parse (e.InnerText);
}
}
if(temp.Lock==0){
levels.Add (temp);
}
}
Debug.Log ("parseXml Level count: "+levels.Count);
return levels;
}
public void UpdateStarById(int id,int value){
XmlDocument doc = new XmlDocument ();
doc.Load (xmlFilePath);
XmlNode root = doc.SelectSingleNode ("Levels");
XmlNode node = root.FirstChild;
for (int i = 1; i < id; i++) {
node = node.NextSibling;
}
foreach(XmlElement e in node){
if(e.Name=="Star"){
e.InnerText = value.ToString ();
}
}
doc.Save (xmlFilePath);
}
public void UpdateLockById(int id){
int value = 0;
XmlDocument doc = new XmlDocument ();
doc.Load (xmlFilePath);
XmlNode root = doc.SelectSingleNode ("Levels");
XmlNode node = root.FirstChild;
for (int i = 1; i < id; i++) {
node = node.NextSibling;
}
foreach(XmlElement e in node){
if(e.Name=="Lock"){
e.InnerText = value.ToString ();
}
}
doc.Save (xmlFilePath);
}
public int GetStarById(int id){
int value = 0;
XmlDocument doc = new XmlDocument ();
doc.Load (xmlFilePath);
XmlNode root = doc.SelectSingleNode ("Levels");
XmlNode node = root.FirstChild;
for (int i = 1; i < id; i++) {
node = node.NextSibling;
}
foreach(XmlElement e in node){
if(e.Name=="Star"){
value=int.Parse(e.InnerText);
}
}
return value;
}
public int GetLockById(int id){
int value = 0;
XmlDocument doc = new XmlDocument ();
doc.Load (xmlFilePath);
XmlNode root = doc.SelectSingleNode ("Levels");
XmlNode node = root.FirstChild;
for (int i = 1; i < id; i++) {
node = node.NextSibling;
}
foreach(XmlElement e in node){
if(e.Name=="Lock"){
value=int.Parse(e.InnerText);
}
}
return value;
}
}
//MyXml.xml
<?xml version="1.0" encoding="UTF-8"?>
<Levels>
<Level id="1">
<Name>Container_KeyButton1</Name>
<Star>3</Star>
<Lock>0</Lock>
</Level>
<Level id="2">
<Name>Container_KeyButton2</Name>
<Star>3</Star>
<Lock>0</Lock>
</Level>
<Level id="3">
<Name>Container_KeyButton3</Name>
<Star>2</Star>
<Lock>0</Lock>
</Level>
<Level id="4">
<Name>Container_KeyButton4</Name>
<Star>1</Star>
<Lock>0</Lock>
</Level>
<Level id="5">
<Name>Container_KeyButton5</Name>
<Star>1</Star>
<Lock>1</Lock>
</Level>
<Level id="6">
<Name>Container_KeyButton6</Name>
<Star>1</Star>
<Lock>1</Lock>
</Level>
</Levels>