C#/수업내용

2020.05.20. 수업내용 - GUI Mission Pop up (Slide view)

dev_sr 2020. 5. 20. 23:22

 

 

프리뷰 내용과 비슷하게 표현 해보았습니다.

확대해서 보면 게이지가 사라져서 작게 캡쳐했습니다..

 

 

게임 오브젝트 및 파일 구조

 

 

데이터 매니저와 매핑 클래스는 생략했습니다.

 

1. App - 씬전환 기능

 

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.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class App : MonoBehaviour
{
    private void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
    }
    void Start()
    {
        //비동기화하면서 넘어가기
        //다음 씬을 로딩하면서 음악이나 애니메이션을 실행할 수 있다.
        AsyncOperation asynOper = SceneManager.LoadSceneAsync("Title");
        asynOper.completed += (obj) =>
        {
            Debug.LogFormat("{0}", obj);
            var title = GameObject.FindObjectOfType<Title>();
            Debug.LogFormat("title: {0}", title);
            title.Init();
        };
    }
 
    
}
 

 

 

2. Title - Title을 관리

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Title : MonoBehaviour
{
    public UITitle uiTitle;
 
    public void Init()
    {
        Debug.Log("Title: Init");
        this.uiTitle.Init();
    }
}
 

 

 

3. UITitle - Title화면의 UI들을 관리한다

 

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
using com.saerom.ui.title.binder;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
public class UITitle : MonoBehaviour
{
    public Button btnStart;
    public UIBinder_BtnMission uIBinderBtnMission;
    private GameObject missionPopUpPrefabs;
    private GameObject missionPopUpGo;
    private UIBinder_PopupMission popupMission;
    
    void Start()
    {
        
    }
 
    public void Init()
    {
        Debug.Log("UITitle:Init");
 
        //로드는 버튼 밖에서 한번만 하기
        this.missionPopUpPrefabs = Resources.Load<GameObject>("Prefabs/UI/UIPopup_Mission");
 
        //시작 버튼을 누르면
        this.btnStart.onClick.AddListener(() =>
        {
            Debug.Log("게임 시작");
        });
 
        //미션버튼을 누르면
        //팝업이 나타나면서(프리팹에서 가져온 오브젝트가 실체화 되면서)
        //팝업을 관리하는 클래스를 가져오고
        //팝업 상태 초기화
        this.uIBinderBtnMission.btn.onClick.AddListener(() =>
        {
            Debug.Log("미션 팝업");
 
            this.missionPopUpGo = Instantiate(missionPopUpPrefabs) as GameObject;
            this.popupMission = missionPopUpGo.GetComponent<UIBinder_PopupMission>();
            this.popupMission.Init();
        });
 
        
 
    }
 
 
}
 

 

 

4. UIBinder_PopupMission - 미션 팝업창을 관리

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
 
public class UIBinder_PopupMission : MonoBehaviour
{
    public Button closeBtn;
    public UIListItem_Mission[] arrMissionLists;    //미션 리스트들 배열을 가짐
   
 
    private void Start()
    {
        Debug.Log("UIBinder_PopupMission:Start");
        
        //닫기 누르면 자신의 오브젝트를 파괴하면서 닫힘
        //dim같은 오브젝트 뒤에 가려져 있는지 확인 잘하기..
        //가려지면 아예 안눌림
        this.closeBtn.onClick.AddListener(() =>
        {
            Debug.Log("닫기");
            Object.Destroy(this.gameObject);
        });
     
    }
 
    //Init=>Start
    public void Init()
    {
        Debug.Log("UIBinder_PopupMission:Init");
 
        DataManager.GetInstance().Load();
 
        List<MissionData> missionList = DataManager.GetInstance().GetMissionDatas();
 
        for (int i=0; i<this.arrMissionLists.Length; i++)
        {
            this.arrMissionLists[i].Init(missionList[i]);
        }
       
    }
 
}
 

 

 

5. UIListItem_Mission - 미션팝업창의 미션 리스트의 한 요소를 관리한다(각 요소마다 이 컴포넌트를 가짐)

 

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
 
public class UIListItem_Mission : MonoBehaviour
{
 
    public GameObject[] arrIcons;   //미션 아이콘 -완료
    public GameObject[] arrStars;   //별들
    public Text textName; //미션 이름   -완료
    public Slider sliderProgress;   //미션 진행상황 게이지 - 나타내는 것만 완료(slider.value 값을 0~1 사이로 할당해서 조절가능)
    public UIBinder_RewardIconMission[] arrUIBinderRewardIcons; //아이콘 바인더들  -완료
    public Button btnGetReward;     //보상획득 버튼
    public GameObject[] arrBtns;    //상태에 따라서 보여줄 리워드 버튼 오브젝트
    private int starCount;
 
    //초기화
    public void Init(MissionData data)
    {
        Debug.Log("UIListItem_Mission : Init");
 
        Debug.LogFormat("data.resName: {0}",data.res_name);
 
 
        string missionName = string.Format(data.name, data.goal);
 
        //텍스트 바꾸기
        this.textName.text = missionName;
 
 
        //아이콘 끔
        for (int i = 0; i < this.arrIcons.Length; i++)
        {
            this.arrIcons[i].SetActive(false);
        }
 
        //해당되는 아이콘만 켜기
        for (int i=0; i<this.arrIcons.Length; i++)
        {
            if(this.arrIcons[i].GetComponent<Image>().sprite.name == data.res_name)
            {
                this.arrIcons[i].SetActive(true);
                
            }
        }
 
        //보상아이콘 표시하기
        //미션데이터에서 보상 아이디를 찾아서 값을 가져오고
        //리워드 아이콘 객체를 찾고->컴포넌트로 찾기로 클래스 정보부터 가져오고
        //UIBinder_RewardIconMission show/ hide를 호출해준다
 
        RewardData rewardData = DataManager.GetInstance().GetRewardDataById(data.reward_id);
        //미리 꺼놓기
        for (int i = 0; i < this.arrUIBinderRewardIcons.Length; i++)
        {
            this.arrUIBinderRewardIcons[i].gameObject.SetActive(false);
        }
 
        for (int i=0; i<this.arrUIBinderRewardIcons.Length; i++)
        {
            var uiRewardIconMission = this.arrUIBinderRewardIcons[i].gameObject.GetComponent<UIBinder_RewardIconMission>();
            if(rewardData.res_name == arrUIBinderRewardIcons[i].GetComponent<Image>().sprite.name)
            {
                uiRewardIconMission.Show(data.reward_amount);
                break;
            }
           
        }
 
        //버튼 다 끄기
        for (int i = 0; i < this.arrBtns.Length; i++)
        {
            this.arrBtns[i].SetActive(false);
        }
        //별 다 끄기
        for (int i = 0; i < this.arrStars.Length; i++)
        {
            this.arrStars[i].SetActive(false);
        }
 
 
        //미션항목별로 프리뷰 이미지처럼 게이지 표현하기
        //슬라이더 value 값을 조절해서 게이지를 표현할 수 있다. 
        //별도 표현하기
 
        switch (data.id)
        {
            case 1000:
                {
                    this.sliderProgress.value = 1f;
                    this.starCount = 1;
                    break;
                }
            case 1001:
                {
                    this.sliderProgress.value = 0f;
                    this.starCount = 5;
                    break;
                }
            case 1002:
                {
                    this.sliderProgress.value = 0.7f;
                    this.starCount = 2;
                    break;
                }
            case 1003:
                {
                    this.sliderProgress.value = 0.5f;
                    this.starCount = 1;
                    break;
                }
            case 1004:
                {
                    this.sliderProgress.value = 0.3f;
                    this.starCount = 3;
                    break;
                }
        }
        
       
        //게이지 진행도에 따라서 버튼 비활성화, 활성화
        if(this.sliderProgress.value==1)
        {
            for(int i=0; i<this.arrBtns.Length; i++)
            {
                if(this.arrBtns[i].name.Contains("btnGetReward"))
                {
                    this.arrBtns[i].SetActive(true);
                    break;
                }
            }
        }
        else if(this.sliderProgress.value==0)
        {
            for (int i = 0; i < this.arrBtns.Length; i++)
            {
                if (this.arrBtns[i].name.Contains("GotReward"))
                {
                    this.arrBtns[i].SetActive(true);
                    break;
                }
            }
        }
        else
        {
            for (int i = 0; i < this.arrBtns.Length; i++)
            {
                if (this.arrBtns[i].name.Contains("NotComplete"))
                {
                    this.arrBtns[i].SetActive(true);
                    break;
                }
            }
        }
 
        //별 갯수만큼 켜기
        for(int i=0; i<this.starCount; i++)
        {
            this.arrStars[i].SetActive(true);
        }
 
   
    }
}