u3d讀取xml txt

u3d讀取xml文件和u3d 讀取txt外部文件this

using UnityEngine;
using System.Collections;

using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;


public class u3dxml : MonoBehaviour
{
    private string m_filename = "2.txt";
    private string m_path =null ;


    // Use this for initialization
    void Start ()
    {
        XmlDocument xmlDoc = new XmlDocument();
        string xmlPath = Application.dataPath + "\\1.xml";
        
        //FileStream fs = new FileStream(xmlPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        xmlDoc.Load(xmlPath);
        //xmlDoc.Load(fs);//能夠Load文件的路徑也能夠Load文件的流

        XmlNodeList componentList = xmlDoc.SelectSingleNode("ttx").ChildNodes;//獲取car節點的全部子節點

        foreach (XmlNode componentXN in componentList)//遍歷全部子節點,獲得component結點
        {
            XmlElement componentXE = (XmlElement)componentXN;//將子節點類型轉換爲XmlElement類型


            XmlNodeList nls = componentXE.ChildNodes;//繼續獲取component子節點的全部子節點

            Debug.Log("data:" + nls.Item(0).InnerText);
            

           // dictionaryOfActionName.Add(int.Parse(nls.Item(0).InnerText), nls.Item(1).InnerText);//取值

        }


        readTXT();
    }
    
    // Update is called once per frame
    void Update ()
    {
    
    }
///////////////////////////////////////////////////////////////////////////// 1.txt
   using UnityEngine;
using System.Collections;

using System.Runtime.InteropServices;
using System.Text;
using System.IO;spa

void readTXT()
    {
        StreamReader m_reader = null;
        m_reader = File.OpenText(Application.dataPath + "\\" + "config.txt");
        string s_line;

        int Count = 0;
        while ((s_line = m_reader.ReadLine()) != null)
        {
            // Debug.Log(s_line);
            int numt;
            string[] words = s_line.Split(':');
            numt = int.Parse(words[1]);
            if (Count == 0)
            {

                m_WheelCenter = numt;
                Debug.Log("Wheelcenter:" + m_WheelCenter);
            }
            if (Count == 1)
            {

                m_WheelLeft = numt;
                Debug.Log("wheelleft:" + m_WheelLeft);
            }
            if (Count == 2)
            {

                m_WheelRight = numt;
                Debug.Log("wheelright:" + m_WheelRight);
            }

            if (Count == 3)
            {
                m_YouMenMin = numt;
                Debug.Log("YouMenMin:" + m_YouMenMin);

            }

            if (Count == 4)
            {
                m_YouMenMax = numt;
                Debug.Log("YouMenMax:" + m_YouMenMax);
            }

          

            Count++;
        }
        m_YouMenFanWei = m_YouMenMax - m_YouMenMin;
        m_ShaCheFanWei = m_ShaCheMax - m_YouMenMin;


        m_reader.Close();
        m_reader.Dispose();
    }

 

更新

3d

txt文件讀和寫code

void ReadConfig(string _name)
    {
        StreamReader m_reader = null;
        m_reader = File.OpenText(Application.dataPath + "\\" + _name);
        string s_line;

        int Count = 0;
        while ((s_line = m_reader.ReadLine()) != null)
        {
            if (Count == 0)
            {

                int numt;
                string[] words = s_line.Split(' ');

                int v = int.Parse(words[0]);

                Debug.Log("Debug Mode:" + v);
            }


            if (Count == 1)
            {
                int numt;
                string[] words = s_line.Split(' ');

                int v = int.Parse(words[0]);

                Debug.Log("Load Config CameraID:" + v);
            }


            ++Count;
        }


        m_reader.Close();
        m_reader.Dispose();
    }

    
    void WriteTxtMaxScore(string _name)
    {
        StreamWriter writeStream = new StreamWriter(Application.dataPath +"\\"+ _name);

        string histroyMaxscore = "HistoryMaxScore:"+ m_HistoryMaxScore;
        string todayScore = "TotadyMaxScore:" + m_TodayMaxScore;


        writeStream.WriteLine(histroyMaxscore);
        writeStream.WriteLine(todayScore);
     //關閉流
        writeStream.Close();
      //銷燬流
        writeStream.Dispose();

    }
相關文章
相關標籤/搜索