12.18 인벤토리만들기

C#/실습 2019. 12. 18. 16:48

Scene: TestPopup 

코드:

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class TestHQ : MonoBehaviour
{
    public UIPopup_SelectedStage popup_SelectedStage;
    public Button stageBtn;
    public Image dim;
    public List<UserItem> userItems = new List<UserItem>();
    UserItem[] arrUserItems;
    ItemData[] ItemDatas;
    private void Awake()
    {
        string json = File.ReadAllText("ItemData.json"); 
        ItemDatas = JsonConvert.DeserializeObject<ItemData[]>(json); // 아이템 row 데이터 로드
        if (File.Exists("UserItemData.json")) //유저가 갖고있던 아이템 로드
        {
            Debug.Log("Real user Items");
            string userItemJson = File.ReadAllText("UserItemData.json");
            arrUserItems = JsonConvert.DeserializeObject<UserItem[]>(userItemJson);            
            foreach(UserItem userItem in arrUserItems)
            {
                userItems.Add(userItem);
            }
        }
        else//기존 유저 아이템 파일이 없다면
        {
            Debug.Log("Sample user Items");
            arrUserItems = new UserItem[3];
            arrUserItems[0= new UserItem(ItemDatas[0].id, ItemDatas[0].name, ItemDatas[0].iconName, 2);
            arrUserItems[1= new UserItem(ItemDatas[1].id, ItemDatas[1].name, ItemDatas[1].iconName, 2);
            arrUserItems[2= new UserItem(ItemDatas[2].id, ItemDatas[2].name, ItemDatas[2].iconName, 2);
            foreach (UserItem userItem in arrUserItems)
            {
                userItems.Add(userItem);
            }
        }
        
 
    }
    void Start()
    {
 
        popup_SelectedStage.Init(new UserInfo("홍길동"315753603920), userItems);
        stageBtn.onClick.AddListener(() =>
        {
            popup_SelectedStage.Show();
            dim.gameObject.SetActive(true);
        });
        popup_SelectedStage.closeBtn.onClick.AddListener(() =>
        {
            popup_SelectedStage.Hide();
            dim.gameObject.SetActive(false);
        });
    }
 
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class ItemData
{
    public int id;
    public string name;
    public string iconName;
}
 
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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
public class UIPopup_SelectedStage : MonoBehaviour
{
    public Button closeBtn;
    public Text text;
    UserInfo userInfo;
    public InventoryItems[] inventoryItems;
    public SpriteAtlas atlas;
 
    public void Init(UserInfo userInfo, List<UserItem> userItems)
    {
        for(int i =0; i<inventoryItems.Length; i++)//초기화
        {
            if(i<9)
            {
                inventoryItems[i].iconStatus = eItemIconStatus.none; //열려있는 아이템칸 설정
            }
            else
            {
                inventoryItems[i].iconStatus = eItemIconStatus.locked; // 닫혀있는 아이템 칸 설정
            }
        }
        this.userInfo = userInfo;
        this.text.text = "UserName: \t" + userInfo.userName + "\n Attack: \t\t" + userInfo.atk + "\n Defense: \t" + userInfo.def + "\n Health: \t\t" + userInfo.hp;        
        for(int i=0; i<userItems.Count; i++// 아이템칸에 차례대로 내 아이템 넣기.
        {
            inventoryItems[i].iconStatus = eItemIconStatus.have;
            inventoryItems[i].userItems = userItems[i];
            inventoryItems[i].gameObject.transform.GetChild(1).gameObject.transform.GetChild(0).GetComponent<Text>().text = inventoryItems[i].userItems.count.ToString();
        }
        for(int i = 0; i<inventoryItems.Length; i++// 아이콘의 상태에 따라 스프라이트 바꾸기
        {
            switch(inventoryItems[i].iconStatus)
            {
                case eItemIconStatus.locked :
                    {
                        inventoryItems[i].gameObject.GetComponent<Image>().sprite = Instantiate(Resources.Load<Sprite>("MyInfo/inventory_item_lock"));
                        inventoryItems[i].gameObject.transform.GetChild(0).gameObject.SetActive(false);
                        inventoryItems[i].gameObject.transform.GetChild(1).gameObject.SetActive(false);
                        break;
                    }
                case eItemIconStatus.have:
                    {
                        for(int j = 0; j< userItems.Count; j++)
                        {
                            string iconName = string.Format("MyInfo/{0}",inventoryItems[j].userItems.iconName);
                            inventoryItems[j].gameObject.transform.GetChild(0).gameObject.GetComponent<Image>().sprite = Instantiate(Resources.Load<Sprite>(iconName));
                        }
                        break;
                    }
                case eItemIconStatus.none:
                    {
                        inventoryItems[i].gameObject.GetComponent<Image>().sprite = Instantiate(Resources.Load<Sprite>("MyInfo/inventory_item_bg"));
                        inventoryItems[i].gameObject.transform.GetChild(0).gameObject.SetActive(false);
                        inventoryItems[i].gameObject.transform.GetChild(1).gameObject.SetActive(false);
                        break;
                    }
            }
        }
 
    }
    public void Show()
    {
        this.gameObject.SetActive(true);
    }
    public void Hide()
    {
        this.gameObject.SetActive(false);
    }
}
 
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;
 
public class UserInfo
{
    public string userName;
    public int level;
    public int atk;
    public int def;
    public int hp;
    public UserInfo(string userName, int level, int atk, int def, int hp)
    {
        this.userName = userName;
        this.level = level;
        this.atk = atk;
        this.def = def;
        this.hp = hp;
 
    }
}
 
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 eItemIconStatus { have, none, locked}
public class InventoryItems : MonoBehaviour
{
    public eItemIconStatus iconStatus;
    public UserItem userItems;
    
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class UserItem 
{
    public int id;
    public string name;
    public string iconName;
    public int count;
    public UserItem(int id, string name, string iconName, int count)
    {
        this.id = id;
        this.name = name;
        this.iconName = iconName;
        this.count = count;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

: