small_inventory_UI.cs
4.73 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class small_inventory_UI : MonoBehaviour
{
public GameObject inventoryUI; // 전체 UI
public GameObject slot_template; // 슬롯 템플릿
private List<GameObject> slots;
public List<havingitem> inventory; //진짜 DB에 저장된 인벤토리!! iD, NUM만 들어있는애!
public delegate void OnItemChanged(); // 대리자 타입선언
public OnItemChanged onItemChangedCallback; // 대리자 변수 선언
private itemDatabase db; //DB
public Item.ItemType smallitemtype; // 아이템타입마다 분리해서 보여주고싶음~!
void Start()
{
slots = new List<GameObject>();
//DB 가져옴
db = GameObject.FindGameObjectWithTag("Item Database").GetComponent<itemDatabase>();
//내 itemLIst 가져옴..
inventory = DataController.Instance.gameData.Item_list;
Debug.Log("인벤토리 오픈! 유저의 실제 인벤토리에 들어있는 아이템 갯수 : " + inventory.Count);
onItemChangedCallback += UpdateUI;
}
public void OnButton_OpenInventory()
{
string buttonname = EventSystem.current.currentSelectedGameObject.name;
Debug.Log(buttonname + "인벤토리 열림!");
if (buttonname == "Button_AP")
smallitemtype = Item.ItemType.Exp_potion;
else if (buttonname == "Button_EP")
smallitemtype = Item.ItemType.Evol_Piece;
else
{
Debug.Log("not allocated button yet. show default.");
}
inventoryUI.SetActive(true);
UpdateUI();
}
public void OnButton_CloseInventory()
{
Debug.Log("인벤토리 닫힘!");
inventoryUI.SetActive(false);
}
public void OnButton_RemoveItem(Item item)
{
// 중요! 임시 인벤토리에서 삭제하는것.
//그냥서치. 바이너리 서치로 바꾸면 더 좋겠지..만 굳이 필요없을듯. 인벤토리에 많이있어봤자..
for (int i = 0; i < inventory.Count; i++)
{
if (inventory[i].id == item.itemID)
{
//아이템이 1개 이하면 인벤토리에서 아이템 삭제
if (inventory[i].num <= 1)
{ inventory.Remove(inventory[i]); i--; } // 중요! 이거... 일단 해두는데 혹시 필요없을수도있음. 뒤에있는게 하나 밀리는게 아니면..
//아니면 갯수만 1개 줄임!
else
inventory[i].num -= 1; // 여러개 줄이고싶음 이거 조절...
break;
}
}
//UI트리거링
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
public void UpdateUI()
{
if (slots.Count > 0) //기존 슬롯들 다 삭제
{
foreach (GameObject slot in slots)
Destroy(slot.gameObject);
slots.Clear();
}
//임시 인벤토리에 들어있는것 대로 슬롯 다시생성
Debug.Log("인벤토리 아이템 갯수 : " + inventory.Count);
for (int i = 0; i < inventory.Count; i++)
{
//아이템 DB에서 찾아서 깊은복사, 아이템 숫자도 바꿔줌
Item newitem;
newitem = db.items[FindDB_index_byID(inventory[i].id)].Clone(); //앗 클론을 안해서 그랬네...
newitem.change_itemnum(inventory[i].num);
if (newitem.itemtype == smallitemtype)
//Item_slot 복제.
{ GameObject oneslot = Instantiate(slot_template) as GameObject;
oneslot.SetActive(true);
//객체의 스크립트 가져와서 함수실행
oneslot.GetComponent<small_slot>().AddItemtoSlot(newitem);
//새로 생성된 버튼의 부모 재설정해줌. false는 위치 그대로 유지시켜줌
oneslot.transform.SetParent(slot_template.transform.parent, false);
//관리하기위해 slots List에 넣어줌
slots.Add(oneslot);
}
}
}
public int FindDB_index_byID(int id)
{
//id갖고 바이너리 서치로 item 탐색
int first = 0;
int last = db.items.Count - 1;
int mid;
while (first <= last)
{
mid = (first + last) / 2;
if (db.items[mid].itemID == id)
{
return mid;
}
else
{
if (db.items[mid].itemID > id)
last = mid - 1;
else
first = mid + 1;
}
}
return -1;
}
}