12.17 Unity Editor 활용해 실시간으로 Json 파일 만들기

C#/실습 2019. 12. 17. 17:52

Unity 파일명: Study_015 Title

 

코드:

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
 
public enum eStars { one, two, three, none }
 
public class HQ : MonoBehaviour
{    
    public StageInfo[] stageInfos = new StageInfo[18];
    public eStageStatus[] stageStatuses;
    public eStars[] stars;
    public Stage[] stages;
    private GameObject star1;
    private GameObject star2;
    private GameObject star3;
    int starsN = 0;
 
    // Start is called before the first frame update
    private void Awake()
    {
        star1 = Resources.Load<GameObject>("Stage/Star1");
        star2 = Resources.Load<GameObject>("Stage/Star2");
        star3 = Resources.Load<GameObject>("Stage/Star3");
        
 
    }
    void Start()
    {
        for(int i=0; i<stages.Length; i++)
        {
            stages[i].stageStatus = stageStatuses[i];
            stages[i].gameObject.transform.GetChild(0).gameObject.GetComponent<Text>().text = i.ToString();
        }
 
        for (int i = 0; i<stages.Length; i++// stage status 체크
        {
            if(stages[i].stageStatus == eStageStatus.Cleared) //깬 경우
            {
                stages[i].gameObject.GetComponent<Image>().sprite = Instantiate(Resources.Load<Sprite>("Stage/Cleared"));
                stages[i].Stars = (int)stars[starsN];
                starsN++;
                switch(stages[i].Stars)
                {
                    case 0:
                        {
                            var model = Instantiate(star1);
                            model.transform.SetParent(stages[i].gameObject.transform.GetChild(1).transform, false);
                            model.transform.localPosition = Vector3.zero;
                            break;
                        }
                        
                    case 1:
                        {
                            var model = Instantiate(star2);
                            model.transform.SetParent(stages[i].gameObject.transform.GetChild(1).transform, false);
                            model.transform.localPosition = Vector3.zero;
                            break;
                        }
                    case 2:
                        {
                            var model = Instantiate(star3);
                            model.transform.SetParent(stages[i].gameObject.transform.GetChild(1).transform, false);
                            model.transform.localPosition = Vector3.zero;
                            break;
                        }                        
                }
            }
            else if(stages[i].stageStatus == eStageStatus.Opened)
            {
                stages[i].gameObject.GetComponent<Image>().sprite = Instantiate(Resources.Load<Sprite>("Stage/Opened"));
            }
            else if(stages[i].stageStatus == eStageStatus.Locked)
            {
                stages[i].gameObject.GetComponent<Image>().sprite = Instantiate(Resources.Load<Sprite>("Stage/Locked"));
                stages[i].gameObject.transform.GetChild(0).gameObject.SetActive(false);
            }
        }
    }
    public void GenerateJson()
    {
        for (int i = 0; i < stageInfos.Length; i++)
        {
            //stageInfos[i].stageID = i;
            //stageInfos[i].stageStatus = (int)stageStatuses[i];
            //stageInfos[i].stars = (int)stars[i];
 
            StageInfo stageInfo = new StageInfo(i, (int)stageStatuses[i], (int)stars[i]);
            stageInfos[i] = stageInfo;
            
        }
        string json = JsonConvert.SerializeObject(stageInfos);
        File.WriteAllText("StageInfo", json);
 
    }
 
    
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
 
[CustomEditor(typeof(HQ))]
 
public class GenerateJson : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
 
        HQ generator = (HQ)target;
        if (GUILayout.Button("Generate Json"))
        {
            generator.GenerateJson();
        }
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class StageInfo
{
    public int stageID = 0;
    public int stageStatus;
    public int stars = 0;
    public StageInfo(int stageID, int stageStatus, int stars)
    {
        this.stageID = stageID;
        this.stageStatus = stageStatus;
        this.stars = stars;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public enum eStageStatus { Opened, Cleared, Locked}
public class Stage : MonoBehaviour
{
    public eStageStatus stageStatus;
    private int stars = 0;
    public int Stars { get { return stars; } set { stars = value; } }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

'C# > 실습' 카테고리의 다른 글

12.18 인벤토리만들기  (0) 2019.12.18
12.16 UI 버튼 만들기  (0) 2019.12.16
12.13 캐릭터 선택하기(랜덤포함)  (0) 2019.12.13
12.09 충돌감지와 sprite활용하기(delegate)  (0) 2019.12.09
12.06 드래곤플라이트 구현  (0) 2019.12.06
: