GameForFun

블로그 이미지
by PforPepsi

TAG CLOUD

  • Total hit
  • Today hit
  • Yesterday hit

http://answers.unity3d.com/questions/42095/unity3-can-i-serialize-non-pod-types-color-vector3.html

http://answers.unity3d.com/questions/8480/how-to-scrip-a-saveload-game-option.html

먼저 이 글은 위의 두 자료를 참고해서 만든 것입니다.

지난번 글에서는 serialize에 대해 살펴보았다.
간단히 정리하자면, serialize를 통해 자체적으로 정의한 클래스를 inspector창에 나타나게 하여 즉각즉각 수정할 수 있도록 설정하는 것이었다. 또한 serialize를 통해 데이터들을 저장, 로드할 수 있다고 언급하였는데, 이 글에서는 저장/로드 에 대해 적어볼려고 한다.

그 전에 프로그램들은 서로 어떻게 데이터를 주고 받는가에 대한 질문부터 해결해보자.
데이터는 0과 1의 2진수로 구성된 하나의 문자열이라 생각할 수 있고, 이러한 문자열을 프로그램간에 주고 받는다면, 그것이 프로그램간의 데이터 통신이 있다는 것을 의미할 것이다.

이와 마찬가지로 저장/로드도 동일하게 0과 1으로 구성된 문자열을 적절한 방식으로 쪼개(Parsing) 원하는 데이터를 저장/얻어오게 할 수 있다. 
자세한 작동 방식은 나도 잘 모르기때문에 이해 할 수 있는 부분만 설명하도록 하겠다.

우리가 필요한 것을 나열해 보도록 하자.
1. 저장할 데이터(Non-Binary.. 맞는 표현인가?  우리가 알아 볼 수 있는 데이터)
2. 저장할 파일
3. BInary로 구성된 데이터(컴퓨터가 알아 볼 수 있는 데이터/. 대충 알아들으시면 감사. 정확한 표현이 아니라는것도 알아두세요. )

우리가 필요한 것은 위의 3가지로 나뉘게 된다. 저장할 데이터, 사용자가 알아 볼 수 있는 데이터들을 컴퓨터가 알아 볼 수 있는 데이터로 변경시킨 뒤에, 파일에 저장을 해야 할 것이다
저장할 데이터 -> 변환 -> Binary 데이터( -> 파일에다 저장
위의 과정을 통하여 저장이 이뤄질 것인다. 반대로 로드는 다음과 같은 과정으로 이뤄질 것이다.
파일 -> Binary -> 변환 -> 우리가 알아 볼 수 있는 데이터
이러한 과정을 생각해보고 다음 코드를 봐보자.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using UnityEngine;
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Reflection;
 
public class MonoDerivedClass : MonoBehaviour{
    public ClassINeed INeed;
}
 
[System.Serializable]
public class ClassINeed{
    public string name;
    public float kkk;
     
    //기본 생성자.
    public ClassINeed(){
    }
    //직접적인 사용이 아니라. 프로그램 상에서 자동적으로 불러주는 생성자이기 때문에 꼭 선언해야한다.
    public ClassINeed(SerializationInfo info, StreamingContext ctxt){
        name = (string)info.GetValue("MyName",typeof(string));
        MyColor = (float)info.GetValue("MyFloat",typeof(float));
    }
    //저번에 설명한 SerializtionInfo에 데이터를 집어넣는 방법.
    public void GetObjectData(SerializationInfo info, StreamingContext ctxt){
        info.AddValue("MyName",name);
        info.AddValue("MyFloat",kkk);
    }
    public void SavePlayerData(){
        ClassINeed data = new ClassINeed();
         
        //새로 생성한 데이터에 값을 넣어준다.
        data.name = name;
        data.MyFloat = MyFloat;
         
        //저장할 파일을 연다.
        Stream stream = File.Open("Assets/Resources/Player/PlayerData.game",FileMode.Create);
        //요놈이 우리가 알아 볼 수 있는 데이터를  컴퓨터가 알아 볼 수 있도록 바꿔줄 것이다.
        BinaryFormatter bformatter  = new BinaryFormatter();
        //여기는 모르겠다.
        bformatter.Binder = new VersionDeserializationBinder();
        Debug.Log ("Saving");
        //데이터를 변환시키고 파일에 저장시켜주는 방법 (추측임)
        bformatter.Serialize(stream,data);
        stream.Close();
    }
     
    public void LoadPlayerData(){
        PlayerData data = new PlayerData();
        Stream stream = File.Open("Assets/Resources/Player/PlayerData.game",FileMode.Open);
        BinaryFormatter bformatter = new BinaryFormatter();
        bformatter.Binder = new VersionDeserializationBinder();
        Debug.Log ("Reading Data");
        //파일에서부터 읽어온 데이터를 다시 번역해서, 우리가 알아 볼 수 있도록 해준다.
        data = (PlayerData)bformatter.Deserialize(stream);
        stream.Close();
         
        name = data.name;
        MyFloat = data.MyFloat;
    }      
}
 
public sealed class VersionDeserializationBinder : SerializationBinder
{
    public override Type BindToType( string assemblyName, string typeName )
    {
        if ( !string.IsNullOrEmpty( assemblyName ) && !string.IsNullOrEmpty( typeName ) )
        {
            Type typeToDeserialize = null;
 
            assemblyName = Assembly.GetExecutingAssembly().FullName;
 
            // The following line of code returns the type.
            typeToDeserialize = Type.GetType( String.Format( "{0}, {1}", typeName, assemblyName ) );
 
            return typeToDeserialize;
        }
 
        return null;
    }
}
위의 코드를 잘 살펴보고 아~ 대충 이런 식으로 흘러가는구나를 알았으면 좋겠다....(나도 모름)

AND

ARTICLE CATEGORY

목록들 (118)
[Unity3D] (39)
그래픽관련 (13)
잉여잉여 (25)
프로그래밍 (27)
노래♬ (3)
Game_Design(기획) (3)

RECENT ARTICLE

RECENT COMMENT

RECENT TRACKBACK

CALENDAR

«   2024/05   »
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 29 30 31

ARCHIVE