Unity读写配置文件:安卓

目录

一、参考:

1、思路:StreamingAssets里有一个xml文件,persistentDataPath第一次时候需要读取StreamingAssets里的xml文件,程序都是对persistentDataPath的xml文件进行读写操作

1、代码:

1、解析:

1、xml文件

1、运行效果

①、电脑:如果是编辑器运行的话,第一次电脑上面会有这个xml产生,以后你代码中进行处理的是这个xml文件,

①、手机:

1、注意 

①、第一次运行时候系统就自动检测在系统文件夹下面是否有这个文件,所以以后修改的都是这个目录下的文件了​

①、使用 int.TryParse(student.Attributes["x"].Value,out x);得到属性里面x的值

①、使用 student.Attributes["y"].Value = str_tmp_value;修改属性里面x的值

①、save出来是BOM格式的,但是LoadXML是UTF-8格式的,所以会报错



 


一、参考:

https://blog.csdn.net/ou_nvhai/article/details/78838481

https://blog.csdn.net/a123qaz021/article/details/83715159

1、思路:StreamingAssets里有一个xml文件,persistentDataPath第一次时候需要读取StreamingAssets里的xml文件,程序都是对persistentDataPath的xml文件进行读写操作

 

研究了很久,终于找到了安卓读取配置文件的方法,下面是一步步步骤

1、代码:

/// <summary>
/// 安卓读写xml:准备使用persistentDataPath读取到最初的xml文件,然后之后都是修改persistentDataPath文件
/// 结果: 成功,
/// 功能:
/// </summary>               

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml;
using UnityEngine.UI;

public class MyTestXml05 : MonoBehaviour
{
    private string xmlPath;
    public static string id;
    public static string score;

    public Text text_xml;

    private bool bIsCanShowConfig;//显示或者隐藏config

    [HideInInspector]
    public Vector3[] targetVec3;//目标3维坐标数组
    private int targetNum;//目标个数

    private string streamingAssetsPath; // streamingAssetsPath路径
    private string persistentDataPath; //persistentDataPath路径


    /// <summary>
    /// 功能:不同平台下不同的路径
    /// </summary>
    void Awake()
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            //localPath = Application.streamingAssetsPath + "/score.xml"; //在Android中实例化WWW不能在路径前面加"file://"
            streamingAssetsPath = "jar:file://" + Application.streamingAssetsPath + "/assets" + "/config.xml";
            Debug.Log(streamingAssetsPath);
        }
        else
        {
            streamingAssetsPath = "file://" + UnityEngine.Application.streamingAssetsPath + "/config.xml";//在Windows中实例化WWW必须要在路径前面加"file://"

            Debug.Log(streamingAssetsPath);
        }

        StartCoroutine(CopyFiles(streamingAssetsPath));

    }

    // Use this for initialization
    void Start()
    {
        bIsCanShowConfig = false;
                                                                                
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.W))
        {
            ModifyConfig(0, 0,5675675);
            ModifyConfig(1, 1,546456);
            ModifyConfig(2, 2,234234);
        }

    }


    /// <summary>
    /// 功能:如果persistentDataPath没有找到xml文件,就会在persistentDataPath目录下面新建一个xml文件
    /// </summary>
    IEnumerator CopyFiles(string path)
    {
        WWW www = new WWW(path);
        yield return www;
        if (www.isDone)
        {
           persistentDataPath= Application.persistentDataPath + "/" + "config.xml";
           Debug.Log(persistentDataPath);
           if (!File.Exists(persistentDataPath))
            {
                File.WriteAllBytes(persistentDataPath, www.bytes);//如果persistentDataPath没有找到xml文件,就会在persistentDataPath目录下面新建一个xml文件
            }
        }
        LoadXml();    
    }

    /// <summary>
    /// 安卓读xml:
    /// 网页:https://blog.csdn.net/a123qaz021/article/details/83715159
    /// </summary>
    public void LoadXml()
    {
        persistentDataPath = Application.persistentDataPath + "/" + "config.xml";

        if (File.Exists(persistentDataPath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            //根据路径将XML读取出来
            xmlDoc.Load(persistentDataPath);

            XmlElement team = (XmlElement)xmlDoc.SelectSingleNode("config");
            string teamname = team.GetAttribute("name");

            Debug.Log("这是一个名叫" + teamname + "的配置文件:");
            XmlNodeList studentlist = team.ChildNodes;
            Debug.Log("一共有" + studentlist.Count + "对象");       

            targetNum = studentlist.Count;
            targetVec3 = new Vector3[targetNum];
           
            for (int i = 0; i < studentlist.Count; i++)
            {           
                string str_target_name = "target" + i.ToString();                 
                foreach (XmlNode student in studentlist)
                {
                    if (student.Name == str_target_name)
                    {                                                                                             
                        float.TryParse(student.Attributes["x"].Value, out targetVec3[i].x );
                        float.TryParse(student.Attributes["y"].Value, out targetVec3[i].y );
                        float.TryParse(student.Attributes["z"].Value, out targetVec3[i].z );

                        Debug.Log("targetVec3["+i+"]" + targetVec3[i]);
                    }
                }       
            }

        }
    }

    /// <summary>
    ///功能:点击:显示或者隐藏xml信息
    /// </summary>
    public void OnClick_ShowOrHideXml()
    {
        if (bIsCanShowConfig==false)
          {
              bIsCanShowConfig = true;
              persistentDataPath= Application.persistentDataPath + "/" + "config.xml";

              string strs = File.ReadAllText(persistentDataPath);
              text_xml.text = strs; 
          }
        else if (bIsCanShowConfig == true)
        {
            bIsCanShowConfig = false;
            text_xml.text = "";            
        }             
    }

      /// <summary>
    ///功能:修改配置文件
    ///参数: 【参数1:目标,从0开始】【参数2:轴,0=x轴 1=y轴 2=z轴】 【参数3:轴的值】
    /// </summary>
    public void ModifyConfig(int para_target,int para_axis,int para_value)
    {
        persistentDataPath = Application.persistentDataPath + "/" + "config.xml";    
        if (File.Exists(persistentDataPath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            //根据路径将XML读取出来
            xmlDoc.Load(persistentDataPath);         
            XmlElement team = (XmlElement)xmlDoc.SelectSingleNode("config");
            XmlNodeList studentlist = team.ChildNodes;
            string str_target_name = "target" + para_target.ToString();
            string str_tmp_value = (para_value).ToString();

            foreach (XmlNode student in studentlist)
            {
                if (student.Name == str_target_name)
                {
                    Debug.Log("str_target_name:" + str_target_name);
                    switch (para_axis)
                      {
                        case 0 :
                              student.Attributes["x"].Value = str_tmp_value;
                              Debug.Log("x被改了:" );
                              break;
                        case 1:
                              student.Attributes["y"].Value = str_tmp_value;
                              Debug.Log("y被改了:");
                              break;
                        case 2:
                              student.Attributes["z"].Value = str_tmp_value;
                              Debug.Log("z被改了:");
                              break;
                      }
                }      
            } 

            xmlDoc.Save(persistentDataPath);   
        }
    }

}

1、解析:

当我找到了persistentDataPath里的xml文件后,通过解析它,就可以设置它里面的属性,xe.SetAttribute("id", "123123");

1、xml文件

<config name="榫卯的奥秘">
  <target0 x="5675675" y="12" z="12" />
  <target1 x="9" y="546456" z="85" />
  <target2 x="9" y="789" z="234234" />
</config>

1、运行效果

①、电脑:如果是编辑器运行的话,第一次电脑上面会有这个xml产生,以后你代码中进行处理的是这个xml文件,

①、手机:

1、注意 

①、第一次运行时候系统就自动检测在系统文件夹下面是否有这个文件,所以以后修改的都是这个目录下的文件了,所以需要注意StreamingAssets和persistentDataPath下面不同的配置文件

①、使用 int.TryParse(student.Attributes["x"].Value,out x);得到属性里面x的值

 

①、使用 student.Attributes["y"].Value = str_tmp_value;修改属性里面x的值

①、save出来是BOM格式的,但是LoadXML是UTF-8格式的,所以会报错

 

参考:http://ju.outofmemory.cn/entry/88909

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值