-
[๊ตฌํ] ์ ๋ํฐ๋ก ์ฝ๊ฒ ์ธ๋ฒคํ ๋ฆฌ ๊ตฌํํ๊ธฐ 2UNITY/PROJECT 2021. 12. 8. 17:08
1. ์ธ๋ฒคํ ๋ฆฌ ์๋จ ์นดํ ๊ณ ๋ฆฌ ์ค์์น
๊ฐ ๋ฒํผ์๋ SetIndex๋ผ๋ ํจ์๋ฅผ ๋ฃ์ด ์ค๋ค.
๋๋ฅธ ๋ฒํผ์ด ๋ช ๋ฒ์ธ์ง๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์ ํด๋น ์์ ๋ง๋ ์นดํ ๊ณ ๋ฆฌ๋ก ๋ณ๊ฒฝํ๋ค.
public class SelectIndex : MonoBehaviour { public InventoryIndex index = InventoryIndex.Item; public GameObject indexes; private LoadItems loadItems; private GameObject[] indexObj; private Color deselect = new Color(0.8f, 0.8f, 0.8f); private void Start() { loadItems = GetComponent<LoadItems>(); indexObj = new GameObject[3]{ indexes.transform.GetChild(0).gameObject, indexes.transform.GetChild(1).gameObject, indexes.transform.GetChild(2).gameObject }; MarkIndex(); } public void SetIndex(int selectedIndex) { int beforeIndex = (int)index; switch (selectedIndex) { case 1: index = InventoryIndex.Item; break; case 2: index = InventoryIndex.Hair; break; case 3: index = InventoryIndex.Outfit; break; } MarkIndex(); loadItems.ResetGoods(beforeIndex); loadItems.Refresh(); } public void MarkIndex() { indexObj[((int)index)].GetComponent<Image>().color = Color.white; indexObj[(((int)index) + 1) % 3].GetComponent<Image>().color = deselect; indexObj[(((int)index) + 2) % 3].GetComponent<Image>().color = deselect; GetComponent<TurnIcon>().CheckIndexItems(); } }
loadItems๋ ์ธ๋ฒคํ ๋ฆฌ์ ์์ดํ ์ ๋ถ๋ฌ์ค๋ ๊ฒ๊ณผ ๊ด๋ จ ์๋ ์ฝ๋์ด๋ค.
index๋ ํ์ฌ ๋ณด์ฌ์ง๊ณ ์๋ ์นดํ ๊ณ ๋ฆฌ๊ฐ ๋ช ๋ฒ ์ธ์ง๋ฅผ ๋ณด์ฌ์ฃผ๋ ๋ณ์์ด๋ค.
๋ชจ๋ ์ด ๋ณ์๋ฅผ ๊ฐ์ ธ๋ค๊ฐ ํ์ฌ ์นดํ ๊ณ ๋ฆฌ๋ฅผ ํ์ธํ ๊ฒ์ด๋ค.
๋๋ฆฐ ๋ฒํผ์ด ๋ช ๋ฒ์ด์๋์ง์ ๋ฐ๋ผ index๋ฅผ ๋ณ๊ฒฝํ๋ค.
MarkIndex๋ ๋๋ฆฐ ๋ฒํผ ์ธ ๋ค๋ฅธ ๋ฒํผ๋ค์ ์์์ ํฐ์์์ ํ์์ผ๋ก ๋ณ๊ฒฝํ๋ ํจ์์ด๋ค.
2. Item ์ธ๋ฒคํ ๋ฆฌ๋ก ๋ถ๋ฌ์ค๊ธฐ
์ฐ๋ฆฌ๋ ์์ ๋ ํ ๊ณํ์ด๊ธฐ ๋๋ฌธ์
์์ ๊ณผ ์ธ๋ฒคํ ๋ฆฌ์ ๊ณตํต์ ์ธ
์นดํ ๊ณ ๋ฆฌ๋ฅผ ๋ฐ๊ฟ ๋ ์ฐ๋ฆฌ ๋ฌผ๊ฑด ์์น๋ฅผ ์ด๊ธฐํ ํ๋ ์คํฌ๋ฆฝํธ์,
๊ฐ์๊ฐ ํ ๋ฉด์ ๋ณด์ฌ์ค ์ ์๋ ๊ฐ์๋ฅผ ๋๊ฒผ์ ๋ ์ธ๋ฒคํ ๋ฆฌ์ scrollview๋ฅผ ํ์ฅํ๋ ์คํฌ๋ฆฝํธ๋ง ์์ฑํ์ฌ
LoadItem์คํฌ๋ฆฝํธ๋ฅผ ๋ง๋ค ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ฅผ ์์๋ฐ์
๊ฐ์์ ํนํ๋ LoadInventory, LoadStore ์คํฌ๋ฆฝํธ๋ฅผ ๋ง๋ค ๊ฒ ์ด๋ค.
์ฝ์ด ๋ณด๋ฉด ์ดํด๊ฐ ๊ฐ๋ ์ฝ๋์ผ ๊ฒ์ด๋ค.
์ ์ ํ ๊ณณ์ Refreshํจ์๋ฅผ ๋ฃ์ด์ฃผ๋ฉฐ ์ฌ์ฉํ๋ค.
public class LoadItems : MonoBehaviour { ... public virtual void Refresh() { } //๋ฌผ๊ฑด ์์น ์ด๊ธฐํ public void ResetGoods(int indexNumToReset) { goods = GameObject.FindGameObjectsWithTag(ItemIndex.itemsIndexString[indexNumToReset]); for (int i = 0; i < goods.Length; i++) { goods[i].transform.SetParent(itemInitPosition.transform.GetChild(ItemIndex.itemsIndexInt[indexNumToReset])); goods[i].transform.position = new Vector3(-1000, 0, 0); } } //์์ดํ ๊ฐ์์ ๋ง์ถฐ ์ธ๋ฒคํ ๋ฆฌ ํ์ฅ ๋ฉ์๋ public void ExpandInventory() { if (inventorySlotBox.transform.childCount > goodsMaxWatch) { { int goodsNeedExpand = goods.Length - goodsMaxWatch; for (; goodsNeedExpand > 0; goodsNeedExpand -= goodsOneLine) { inventorySlotBox.transform.parent.GetComponent<RectTransform>().sizeDelta = new Vector2(inventoryWidth, inventoryHeight += 130); } } } else { inventorySlotBox.transform.parent.GetComponent<RectTransform>().sizeDelta = new Vector2(inventoryWidth, inventoryHeightOri); inventoryHeight = 400; } } }
๋ค์์ Load Inventory๋ค.
public class LoadInventory : LoadItems { private GameObject[] goods; private SelectIndex selectIndex; private ItemCount itemCount; private void Start() { selectIndex = GetComponent<SelectIndex>(); } //์ธ๋ฒคํ ๋ฆฌ ์์ดํ ๋ก๋ public override void Refresh() { goods = GameObject.FindGameObjectsWithTag(selectIndex.index.ToString()); itemCount = GetComponent<ItemSaveLoad>().GetItemCount(); int selectIndexNum = (int)selectIndex.index; if (selectIndexNum == ((int)InventoryIndex.Item)) RefreshItem(); else RefreshGoods(selectIndexNum); ExpandInventory(); } private void RefreshItem() { for (int i = 0; i < goods.Length; i++) { int id = goods[i].GetComponent<ItemInfo>().id - 1; int count; if ((count = itemCount.itemCounts[id]) > 0) { if (GetComponent<PackingItems>().GetPackedItems().Contains(goods[i]))//์ด ์์ดํ ์ด ๊ฐ๋ฐฉ์ ์๋ ์์ดํ ์ด๋ฉด count--; goods[i].transform.GetChild(1).GetChild(0).gameObject.GetComponent<UnityEngine.UI.Text>().text = count.ToString(); if (count > 0) goods[i].transform.SetParent(inventorySlotBox.transform); } } } private void RefreshGoods(int selectIndexNum) { for (int i = 0; i < goods.Length; i++) { int id = goods[i].GetComponent<ItemInfo>().id - 1; if (itemCount.have[selectIndexNum - 1].haveList[id]) goods[i].transform.SetParent(inventorySlotBox.transform); } } }
Refresh์์ ํ์ฌ ์นดํ ๊ณ ๋ฆฌ๊ฐ ์์ดํ ์ธ ๊ฒฝ์ฐ, ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ์ ๋ฐ๋ผ ๋ค๋ฅธ ํจ์๋ฅผ ์คํ์ํจ๋ค.
์์ดํ ์ธ ๊ฒฝ์ฐ RefreshItem์ ์คํ์ํค๋๋ฐ,
์ฐ๋ฆฌ ๊ฒ์์์๋ ์์ดํ ์ ๊ฐ๋ฐฉ์ ๋ฃ๋ ๊ธฐ๋ฅ์ด ์๋ค.
๋ฐ๋ผ์ ์์ดํ ์ด ๊ฐ๋ฐฉ์ ๋ค์ด์๋ ๊ฒฝ์ฐ์๋ ๊ฐ๋ฐฉ์ ๋ค์ด์๋ ์์ดํ ํฌํจํ์ฌ ๊ฐ์๊ฐ ์ ์ฅ๋์ด ์์ ๊ฒ์ด๋ค.
๊ทธ๋์ ๊ฐ๋ฐฉ์ ๋ค์ด์๋ ์์ดํ ์ ์ด ์คํฌ๋ฆฝํธ์์ ๊ฐ์๋ฅผ 1 ๋นผ์ ์คํฌ๋ฆฐ์ ๋ณด์ฌ์ค๋ค.
๊ทผ๋ฐ ๋ญ ์ด ๊ธฐ๋ฅ์ด ํ์ ์๋ ๊ฒ์์ ์์์ ๋นผ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
์ผ๋จ ํฌ๊ฒ๋ 0๊ฐ ์ด์ ๊ฐ์ง๊ณ ์๋ ์์ดํ ์ ๊ฐ์๋ฅผ ๋ถ๋ฌ์ slot์ ๋ฃ์ด์ฃผ์๋ค.
RefreshGoods์์๋ ๊ทธ๋ฅ ๊ฐ์ง๊ณ ์๋๊ฒ ํ์ธ์ด ๋๋ฉด slot์ ๋ฃ์ด์ฃผ์๋ค.
์ด๋ ๊ฒ ๋๋ฉด ์ธ๋ฒคํ ๋ฆฌ ๋ง๋ค๊ธฐ ๋!
์ธ๋ฒคํ ๋ฆฌ์ ์์ดํ ์ ๋๋ ์ ๋ ์ผ์ด๋๋ ์ผ์ ๊ฐ์์ ๋ง์ถฐ์ ๊ตฌํํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
๋๊ฐ์ ๊ฒฝ์ฐ๋ ๋จธ๋ฆฌ, ์์์ ์ฐฉ์ฉ์ด ๋๊ณ ์์ดํ ์ ๊ฐ๋ฐฉ์ ๋ค์ด๊ฐ๋๋ก ๊ตฌํํ๋ค.
'UNITY > PROJECT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ตฌํ] ์ ๋ํฐ๋ก ์ฝ๊ฒ ์์ ๊ตฌํํ๊ธฐ (0) 2021.12.08 [๊ตฌํ] ์ ๋ํฐ๋ก ์ฝ๊ฒ ์ธ๋ฒคํ ๋ฆฌ ๊ตฌํํ๊ธฐ 1 (0) 2021.12.08 [๊ตฌํ] ์ ๋ํฐ ํ ์คํธ ํ์ดํ ํจ๊ณผ (0) 2021.12.07 [๊ตฌํ] json ์ด์ฉ ์ ๋ํฐ ๋ด ๋ํ ๊ฐ๋จํ๊ฒ ๊ตฌํํ๊ธฐ 3 (0) 2021.12.07 [๊ตฌํ] json ์ด์ฉ ์ ๋ํฐ ๋ด ๋ํ ๊ฐ๋จํ๊ฒ ๊ตฌํํ๊ธฐ 2 (0) 2021.12.07