2016. 2. 22. 05:32

파일 입출력 기본 방법


StreamWriter 클래스의 WriteLine 함수를 사용하여 파일에 데이터를 쓸 수 있습니다.

StreamReader 클래스의 ReadLine 함수를 사용하여 파일로부터 데이터를 읽어올 수 있습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 using System.IO; // 파일 입출력 함수를 사용하기 위해 스크립트에 포함시킨다.
 
// 파일에 쓰기
void WriteFile( String filepathIncludingFileName )
{
    StreamWriter sw = new StreamWriter( filepathIncludingFileName );
    sw.WriteLine("Line to write"); // 줄단위로 파일에 입력
    sw.WriteLine("Another Line");
    sw.Flush(); // 파일 쓰기 반드시 해준다.
    sw.Close(); // 파일 쓰기 반드시 해준다.
}
 
 
// 파일로 부터 읽기
void ReadFile( String filepathIncludingFileName)
{
    StreamReader sr = new File.OpenText(filepathIncludingFileName);
 
    input = "";
    // 파일을 줄단위로 읽는다.
    while (true)
        {
        input = sr.ReadLine();
        if (input == null) { break; }
        Debug.Log("line="+input);
    }
    sr.Close(); // 파일 읽기후 반드시 해준다.
}
cs


Posted by 시리시안
2016. 2. 22. 05:30

this post shows asimple tutorial to do some basic operations with xml file on Unity3D using C#.

How to load?

Put an XML file on Resources folder and load itwith Resources class from Unity3D
xml sample:

<achievement>

 <uniqueid>0</uniqueid>

 <referencename>MyAchievement</referencename>

 <goal>Solve until 10seconds</goal>

 <points>10</points>

</achievement>

 

using UnityEngine;

using System.Collections;

using System.Xml;

 

 

public class XMLEditor : MonoBehaviour

{

 

   void Awake()

    {

       //Load

       TextAsset textXML = (TextAsset)Resources.Load("myxml.xml",typeof(TextAsset));

       XmlDocument xml = new XmlDocument();

       xml.LoadXml(textXML.text);

    }

}

Yeah! Using a TextAsset you can make it very simply!

How to read?

Pretty simple! Use .NET framework, XmlDocumentwill turn you live more easy.

using UnityEngine;

using System.Collections;

using System.Xml;

 

 

public class XMLEditor : MonoBehaviour

{

   void Awake()

    {

       //Load

       TextAsset textXML = (TextAsset)Resources.Load("myxml.xml",typeof(TextAsset));

       XmlDocument xml = new XmlDocument();

       xml.LoadXml(textXML.text);

 

       //Read

       XmlNode root = xml.FirstChild;

       foreach(XmlNode node in root.ChildNodes)

       {

           if (node.FirstChild.NodeType == XmlNodeType.Text)

                Debug.Log(node.InnerText);

       }

    }

}


Ok, but if there are multiplehierarchies you will must implement your own ReadMethod to do that bynavigating for all nodes.

How to save?

using UnityEngine;

using System.Collections;

using System.Xml;

 

 

public class XMLEditor : MonoBehaviour

{

   void Awake()

    {

       //Load

       TextAsset textXML = (TextAsset)Resources.Load("myxml.xml",typeof(TextAsset));

       XmlDocument xml = new XmlDocument();

       xml.LoadXml(textXML.text);

 

       //Read

       XmlNode root = xml.FirstChild;

        foreach(XmlNode node in root.ChildNodes)

       {

           if (node.FirstChild.NodeType == XmlNodeType.Text)

                node.InnerText ="none";

       }

 

       //Simple Save

       xml.Save(AssetDatabase.GetAssetPath(textXML));

    }

}

The easy way! :)



출처 : http://fernandogamedev-en.blogspot.kr/2012/09/how-to-load-read-and-save-xml-on-unity3d.html

Posted by 시리시안
2016. 2. 22. 05:29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
void MultiTouch()
{
    //현재 터치 개수를 받아온다 
    int touchCount = Input.touchCount; 
    //터치 Begin에서 터치 개수가 2개면 두 점의 거리를 계산한다. 
    if( touchCount == 2 ) 
       m_OldTouchDistance = Vector2.Distance( Input.touches[0].position, Input.touches[1].position ); 
    
    //계속 거리를 계산해서 거리가 짧아지면 줌인, 거리가 멀어지면 줌아웃 한다. 이전 거리 갱신한다. 
    m_NowTouchDistance= Vector2.Distance( Input.touches[0].position, Input.touches[1].position ); 
    if( m_NowTouchDistance > m_OldTouchDistance ) 
        ZoomOut(); 
    else if( m_NowTouchDistance < m_OldTouchDistance ) 
        ZoomIn(); 
    m_OldTouchDistance = m_NowTouchDistance;     
}
// 이곳에서 줌인 줌아웃을 구현합니다.
void ZoomOut() 
void ZoomIn() 
 
cs


Posted by 시리시안