Beispiel #1
0
    /// <summary>
    /// 加载Xml文件
    /// </summary>
    /// <returns>The levels.</returns>
    public List <Mission> LoadMissions()
    {
        //创建Xml对象
        XmlDocument xmlDoc = new XmlDocument();
        //如果本地存在配置文件则读取配置文件
        //否则在本地创建配置文件的副本
        //为了跨平台及可读可写,需要使用Application.persistentDataPath
        string filePath = Application.persistentDataPath + "/missions.xml";

        //检查是否存在配置文件
        if (!IOUntility.isFileExists(filePath))                           //不存在
        {
            xmlDoc.LoadXml(((TextAsset)Resources.Load("missions")).text); //加载Resources文件夹中的配置范本,拷贝其中内容
            IOUntility.CreateFile(filePath, xmlDoc.InnerXml);             //使用IOUntility类方法创建配置文件
        }
        else                                                              //存在
        {
            xmlDoc.Load(filePath);                                        //加载配置文件
        }

        XmlElement  root         = xmlDoc.DocumentElement;                //读取配置文件中的元素
        XmlNodeList missionNodes = root.SelectNodes("/missions/mission"); //查找xml配置文件中的mission节点
        //初始化关卡列表
        List <Mission> missions = new List <Mission>();

        foreach (XmlElement xe in missionNodes)         //遍历配置节点,把配置存在missons数组中
        {
            Mission l = new Mission();
            l.ID    = int.Parse(xe.GetAttribute("id"));    //读取关卡id
            l.Score = int.Parse(xe.GetAttribute("score")); //读取关卡得分
            //使用unlock属性来标识当前关卡是否解锁
            if (xe.GetAttribute("unlock") == "1")
            {
                l.UnLock = true;
            }
            else
            {
                l.UnLock = false;
            }

            missions.Add(l);    //添加到数组
        }

        return(missions);        //返回关卡信息数组
    }
Beispiel #2
0
    // 加载Xml文件
    public static List <Level> LoadLevels()
    {
        //创建Xml对象
        XmlDocument xmlDoc = new XmlDocument();

        //如果本地存在配置文件则读取配置文件
        //否则在本地创建配置文件的副本
        //为了跨平台及可读可写,需要使用Application.persistentDataPath
        //string filePath = Application.dataPath + "/levels.xml";
        if (!IOUntility.isFileExists(xmlPath))
        {
            xmlDoc.LoadXml(((TextAsset)Resources.Load("levels")).text);
            IOUntility.CreateFile(xmlPath, xmlDoc.InnerXml);
        }
        else
        {
            xmlDoc.Load(xmlPath);
        }
        XmlElement  root       = xmlDoc.DocumentElement;
        XmlNodeList levelsNode = root.SelectNodes("/levels/level");
        //初始化关卡列表
        List <Level> levels = new List <Level>();

        foreach (XmlElement xe in levelsNode)
        {
            Level l = new Level();
            l.ID   = xe.GetAttribute("id");
            l.Name = xe.GetAttribute("name");
            User user        = UserUtil.getUser(PlayerPrefs.GetString("currentUser"));
            int  lastLevelId = int.Parse(l.ID) - 1;
            if (user.getScoreForStage(lastLevelId) >= int.Parse(xe.GetAttribute("unlockScore")))
            {
                l.UnLock = true;
            }
            else
            {
                l.UnLock = false;
            }

            levels.Add(l);
        }

        return(levels);
    }