2014년 5월 8일 목요일

xml 파싱

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class XmlParsing : MonoBehaviour {

public Dictionary<int, RoomInfo> dictRI = new Dictionary<int, RoomInfo>();

// Use this for initialization
void Start () {
WriteXML();
ReadXML();
}

void WriteXML()
{
RoomInfo ri = new RoomInfo();
ri.id = 1;
ri.byteB = 1;
ri.stringS = "aaa";
ri.boolB = false;

dictRI.Add(ri.id, ri);

ri = new RoomInfo();

ri.id = 2;
ri.byteB = 2;
ri.stringS = "bbb";
ri.boolB = true;

dictRI.Add(ri.id, ri);

DataControl.i.WriteRoomInfo(dictRI, "RoomInfo.xml");
}

void ReadXML()
{
dictRI.Clear();

dictRI = DataControl.i.ReadRoomInfo("RoomInfo.xml");
foreach(RoomInfo ri in dictRI.Values)
{
Debug.Log ("id : " + ri.id + ", byte : " + ri.byteB + ", string : " + ri.stringS + ", bool : " + ri.boolB);
}
}


}

데이터 컨트롤...

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Xml.XPath;

public class DataControl : MonoBehaviour {

public static DataControl i;

void Awake()
{
i = this;
}


/* 방 정보 리스트를 전달하는 함수이다. */
public Dictionary<int, RoomInfo> ReadRoomInfo(string URI)
{
Dictionary<int, RoomInfo> dictRI = new Dictionary<int, RoomInfo>();

//TextAsset ta = Resources.Load (URI) as TextAsset;
string strXML = readStringFromFile(URI);
Debug.Log (strXML);


XmlDocument xmlDoc = new XmlDocument(); // xmlDoc is the new xml document.
// xmlDoc.LoadXml(ta.text); // load the file.
xmlDoc.LoadXml(strXML); // load the file.
XmlNodeList rooms = xmlDoc.GetElementsByTagName("RoomInfo"); // get StagePattern[]

foreach(XmlNode room in rooms)
{
XmlNodeList roomContent = room.ChildNodes;

RoomInfo ri = new RoomInfo();

foreach(XmlNode roomItem in roomContent) // pattern nodes.
{
if(roomItem.Name == "id") ri.id = XmlConvert.ToInt32(roomItem.InnerText);
if(roomItem.Name == "byteB") ri.byteB = XmlConvert.ToByte(roomItem.InnerText);
if(roomItem.Name == "stringS") ri.stringS = roomItem.InnerText;
if(roomItem.Name == "boolB") ri.boolB = XmlConvert.ToBoolean(roomItem.InnerText);
}

if( !dictRI.ContainsKey(ri.id) )
{
dictRI.Add(ri.id, ri);
}
}

return dictRI;
}

/* The following metods came from the referenced URL */
public string UTF8ByteArrayToString(byte[] characters)
{    
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}

public string SerializeObject<T>(T obj)
{
try
{
string xmlString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, obj);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlString = UTF8ByteArrayToString(memoryStream.ToArray());    
return xmlString;
}
catch
{
return string.Empty;
}
}

public void WriteRoomInfo(Dictionary<int, RoomInfo> dictRI, string path)
{
try
{
List<RoomInfo> listRI = new List<RoomInfo>();
listRI.AddRange(dictRI.Values);
string myXML = SerializeObject<List<RoomInfo>>(listRI);

File.WriteAllText(path, myXML);
}
catch
{
Debug.LogError ("XML Convert Error! : WriteRoomInfo");
}
}

public string readStringFromFile( string filename)//, int lineIndex )
{
#if !WEB_BUILD
string path = pathForDocumentsFile( filename );

if (File.Exists(path))
{
FileStream file = new FileStream (path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader( file );

string str = null;
str = sr.ReadLine ();

sr.Close();
file.Close();

return str;
}

else
{
return null;
}
#else
return null;
#endif
}

public string pathForDocumentsFile( string filename )
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
string path = Application.dataPath.Substring( 0, Application.dataPath.Length - 5 );
path = path.Substring( 0, path.LastIndexOf( '/' ) );
return Path.Combine( Path.Combine( path, "Documents" ), filename );
}

else if(Application.platform == RuntimePlatform.Android)
{
string path = Application.persistentDataPath;
path = path.Substring(0, path.LastIndexOf( '/' ) );
return Path.Combine (path, filename);
}

else
{
string path = Application.dataPath;
path = path.Substring(0, path.LastIndexOf( '/' ) );
return Path.Combine (path, filename);
}
}


}

2014년 4월 11일 금요일


Vector3 vOriginPos = //원래 위치. 
Vector3 vMovePos = //이동할 위치. 

//체인지 버튼을 누르면 호출되는 함수. 
public void MoveStartObject() 

    TweenPosition tp = TweenPosition.Begin(gameobject, 1f, vMovePos); 
    tp.from = //원래위치. 
    tp.to = //이동위치. 
    tp.duration = //이동할때걸리는 시간. 
    tp.eventReceiver = gameobject;  //이동이 끝나면 호출되는 콜백 - 
    tp.callWhenFinished= "MoveEnd"; 


//무빙이 끝나면 호출되는 콜백함수. 
void MoveEnd() 

  TweenScale ts = TweenScale.Begin(gameobject, 커지는 시간, 크기); 
  ts.from = //원래 크기. 
  ts.to = //커질 크기. 
  ts.delay = //이동후에 바로 커질것인가 딜레이타임을 주고 커질것인가. 
  ts.duration = //커지는 시간. 
}


boardObject.SetActive(true); 
TweenAlpha tweenAlpha = boardObject.AddComponent<TweenAlpha>(); 
tweenAlpha.from = 0.0f; 
tweenAlpha.to = 1.0f; 
tweenAlpha.onFinished = delegate(UITweener tween) 

        UIScale uiScale = tween.gameObject.AddComponent<UIScale>(); // 현재의 컴포넌트와 연결된 게임오브젝트에 다른 트윈 추가. 
        GameObject.Destroy(tween); // 실행이 끝난 현재의 트윈 컴포넌트를 제거. 
        uiScale.onFinished = // 여기에 새로운 트윈이 끝난시점의 내용을 위와 똑같이 추가. 
        uiScale.Play(); // 새로운 트윈을 실행. 
};

2014년 4월 5일 토요일

개발 시작...

1. 카메라 세팅
다른 방법이 있다고는 하지만.. 사실 알지 못하고..
나는 카메라를 세개 사용합니다.

먼저 게임 카메라...
게임안에 조작과 각종 화면을 담당합니다.

두번째 ui카메라로 .. ngui 카메라입니다.
여기서 각종 ui를 담당합니다.

세번째는 fx 카메라로..
화면에 특수 효과를 담당합니다.

실제로 2d 게임등에는.. ui위로.. 특수효과가 나오는 경우가 있습니다.
이때를 대비하여 만든 것입니다.
이부분은 차후 수정이 필요한것으로..

게임 화면에서 동전을 먹고
그 동적인 ui에 가서 번쩍이려면...
게임 구조가 달라져야 하기 때문입니다.


2. 해상도 세팅..

해상도는 ngui 에 의지합니다.
어차피.. 카메라는..기기별로 달라집니다.
그냥.. 액션게임이면 신경 안써도 되고..(조금 화면이 넓어진다고 뭔 상관이겠습니까)
퍼즐 등이면 미리 보완을 합니다.

ui 카메라가 문제인데...
해상도는 오토로 사용합니다.

그리고 각 부분은 앵커를 박아서 사용합니다.
이렇게 되면.. 세로 해상도를 기준으로..
ui가 늘어지지 않고
조금씩 위치를 넓혀 가면서... 보여지게 됩니다.

요기까지만 와도.. 게임 개발의 ABC중 a는 확실하게 온것입니다.
이제부터 본화면과 함께... 개발에 들어갑니다.
개발은 소스를 위주로 하겠습니다..

프로그램....순서

일단 먼저 틀을 잡아야 할 것이다.
나는 유니티를 기반으로 ngui를 위주로 사용한다.

먼저 스크립트는 크게 분야별로 나눈다.

1. 데이터 클래스 

게임에서 제공되는 각종 정보를 받거나 저장하는 클래스와
게임내부에서 사용되는 각종 정보 클래스...

여기서..
데이터를 받아롤때...
웹에서 받을 것인지... 를 정해야 하며
기본 작업에서는 그렇게까지 할 필요는 없다.

그럼에도.. xml 을 상용하고자 하면..이것도 큰 문제는 없다.

2. 연결하는 클래스..
xml 에 데이터를 받아서 클래스에 넘겨주는 클래스..

즉, xml 에서 정보를 받아서 가공하여
본 게임에서 사용할수 있게 해주는 클래스이다.

3. 게임 관련 클래스..
가공된 데이터등으로 본 게임에서 직접 게임을 제어하는 클래스....이다.


게임은 기본적으로 위에 세가지 클래스를 기반으로 작업된다.
게임의 규모가 작다면
클래스는 하나로 유지해도 상관없다.
끽해야 몇장 나온다.

그러나 광범위하게 작업되려면...
각각의 작업에 관련한 클래스를 정의해서 가지고 있어햐 한다
물론.. 이렇게 한다고 특별하게 뭔가가 정의되는 것은 아니지만..

어차피 큰 파이프 라인은 가지고 있어야 한다.
어느 클래스를 어디에서 쓰고
다시 어디로 연결하고 하는지.. 알아야 한다.

모르면 상황 웃겨진다.
서로 협의하는데만 시간 십만년 걸린다.
문제는 설명해주는 쪽이..
자시는 긴 작업과 회의 끝에 내용이 정리되 것임에도
설명을 받는 쪽에...게

이거 간단한거 아냐... 이렇게 생각하는데 있다...

긴시간이 흐르고서야.. 왜 우리가 늦어졌지.. 이렇게 생각하게 된다.




2014년 1월 2일 목요일

퍼즐 게임 만들기..

1. 배치

화면안에.. 퍼즐을 배치한다.
퍼즐을 배치 하기 위해..
기분이 되는 배열을 만든다.

미리 만들어도 되고..
만들면서 배치해도 된다.

나는

randomlist

를 만들어... 가로 새로열을 정한다.
이 배열에... 퍼즐의 종류별로 난수를 발생하여 집어 넣는다...

이 배열은.. 향후 게임의 모든 기준이 된다.

이 배열을 만들면서.. 동시에 이름을 만들어 집어 넣는 배열을 만든다..
이 배열은..

randomnamelist

이 배열에는 1_1_1 이런식으로... 이름을 넣는다...
첫번째는 세로열, 두번째 숫자는 가로열 마지막은 퍼즐의 종류다...
이름을 넣는 이유는 이를 이용해.. 나중에 사용하기 위해서다..

클릭했을때.. 클릭된 자신이 누군지 알아야 하는데..
그를 위해 이렇게 이름을 만들어... 넣는 것이다..

이후..
특정 퍼즐을 클릭하면..

2_3_1 식으로.. 이름을 알수 있다.
이는 이 퍼즐이.. 새로 두번째 가로 세번째의 파란색 퍼즐이란 뜻이다..

이를 문자열로 받아.. 가공하게 된다.



2. 클릭
클릭하면.. 해당 퍼즐의 정보를 받게 된다.
받은 퍼즐의 이름은..  가공하여..
현재 퍼즐은 어느 위치에 있는 퍼즐이며.. 종류가 무엇인지 알아낸다.


3. 체크
이후 위, 아래, 좌, 우 를 체크하여