> For the complete documentation index, see [llms.txt](/llms.txt)

# Inventory(GS2 UIKit for Unity) 샘플

GS2 UIKit for Unity를 사용한 GS2-Inventory 기능 구현 샘플 소개



인벤토리 기능을 구현합니다.

## 리포지토리

https://github.com/gs2io/gs2-uikit-for-unity-sample/tree/main/Inventory

## GS2-Deploy 템플릿

https://github.com/gs2io/gs2-uikit-for-unity-sample/blob/main/Inventory/initialize_inventory_template.yaml

## 프로젝트 내용

![img.png](img.png)

프로젝트를 열면 위와 같은 화면이 됩니다.

![img_1.png](img_1.png)

실행하면 오른쪽 위에 인벤토리 용량 사용 현황이 표시되고, 하단에 인벤토리 조작 버튼이 표시됩니다.

![img_2.png](img_2.png)

"Acquire Potion"을 클릭합니다.

![img_3.png](img_3.png)

그러면 포션을 10개 입수할 수 있으며, 화면 상단에 포션이 표시됩니다.
마찬가지로, 다음은 에테르를 입수하기 위해 "Acquire Ether"를 클릭합니다.

![img_4.png](img_4.png)

포션 옆에 에테르가 추가됩니다.
참고로, 오른쪽 위의 인벤토리 사용량도 2/10 이 되어 있습니다.

다음으로, 포션의 최대 스택 수는 99개 로 설정되어 있으므로 "Acquire Potion"을 9번 더 클릭하여 100개를 가지고 있는 상태로 만들어 봅니다.

![img_5.png](img_5.png)

이처럼 포션이 2개의 스택으로 나뉘고, 인벤토리 사용량도 3/10 이 되었습니다.

포션이나 에테르의 아이콘을 클릭하면 아이템을 소비할 수 있습니다.
99개 스택되어 있는 포션을 클릭해 봅시다.

![img_6.png](img_6.png)

아이템은 스택을 명시적으로 지정하여 소비할 수 있으며, 이 경우 채워 넣는 처리는 이루어지지 않으므로
98개 스택된 포션과 1개 스택된 포션이 됩니다.

다음은 1개 스택되어 있는 포션을 클릭해 봅니다.

![img_7.png](img_7.png)

이처럼 스택의 총 수가 2개가 되고, 인벤토리 소비량도 2/10 이 되었습니다.

## 프로젝트 해설

![img_8.png](img_8.png)

씬의 계층 구조에 주목해 주세요.

### Canvas/InventoryWindow

![img_9.png](img_9.png)

**Gs2InventoryOwnInventoryContext**

이 GameObject 이하의 노드에서 다루는 GS2-Inventory 의 인벤토리(스탠다드)를 지정하고 있습니다.

![img_11.png](img_11.png)
![img_10.png](img_10.png)

이번에는 GS2-Inventory 의 inventory-0001 이라는 네임스페이스의 Bag 이라는 인벤토리를 지정하고 있습니다.

### Canvas/InventoryWindow/Capacity

![img_12.png](img_12.png)

**Gs2InventoryInventoryEnabler**

Gs2InventoryOwnInventoryContext 에서 지정한 인벤토리의 로드가 끝나면 InventoryCapacityText GameObject 가 활성화됩니다.

### Canvas/InventoryWindow/Capacity/InventoryCapacityText

![img_13.png](img_13.png)

**Gs2InventoryInventoryLabel**

현재 인벤토리 사용량/현재 인벤토리 최대량 을 Format 에 지정하고 있습니다.
정형된 텍스트는 OnUpdate(String) 로 콜백되어, 거기서부터 Text 컴포넌트에 값을 반영합니다.

### Canvas/InventoryWindow/Inventory/.../Content

![img_15.png](img_15.png)

**Gs2InventoryOwnItemSetListFetcher**

Gs2InventoryOwnInventoryContext 에서 지정된 네임스페이스의 아이템 목록을 가져옵니다.

**Gs2InventoryOwnItemSetList**

Gs2InventoryOwnItemSetListFetcher 로 가져온 아이템을 지정된 프리팹에 할당하여 생성합니다.
Maximum Items 에는 프리팹을 생성할 최대 수를 지정합니다.

![img_14.png](img_14.png)

이번에는 자식 노드에 있는 Item 을 프리팹으로 지정하고 있습니다.
프리팹으로 지정된 GameObject 가 씬 안에 있는 경우, 해당 GameObject 는 비활성화되고, 그 외에 Maximum Items 로 지정된 수만큼 GameObject 가 생성됩니다.

### Canvas/InventoryWindow/Inventory/.../Content/Item

![img_16.png](img_16.png)

**Gs2InventoryOwnItemSetContext**

이 GameObject 이하의 노드에서 다루는 아이템을 지정하고 있습니다.
컨텍스트의 내용은 Gs2InventoryOwnItemSetList 에 의해 할당됩니다.

**Gs2InventoryItemModelFetcher**

Gs2InventoryOwnItemSetContext 에서 지정된 아이템 모델을 가져옵니다.

**Gs2InventoryOwnItemSetFetcher**

Gs2InventoryOwnItemSetContext 에서 지정된 아이템을 가져옵니다.

### Canvas/InventoryWindow/Inventory/.../Content/Item/Icon

![img_18.png](img_18.png)

**Gs2InventoryItemModelNameEnabler**

할당된 아이템 모델의 이름에 따라 활성화할 GameObject 를 전환합니다.
Potion 인 경우에는 PotionImage 를 활성화하고, Ether 인 경우에는 EnterImage 를 활성화합니다.
그 결과, 목록에 표시되는 아이템의 종류에 따라 아이템 아이콘이 바뀝니다.

### Canvas/InventoryWindow/Inventory/.../Content/Item/Name

![img_19.png](img_19.png)

**Gs2InventoryItemModelLabel**

할당된 아이템 모델의 내용을 Format 에서 지정한 형식으로 정형합니다.
정형된 텍스트는 OnUpdate(String) 로 콜백되어, 거기서부터 Text 컴포넌트에 값을 반영합니다.

### Canvas/InventoryWindow/Inventory/.../Content/Item/Count

![img_20.png](img_20.png)

**Gs2InventoryOwnItemSetLabel**

할당된 아이템 세트의 내용을 Format 에서 지정한 형식으로 정형합니다.
정형된 텍스트는 OnUpdate(String) 로 콜백되어, 거기서부터 Text 컴포넌트에 값을 반영합니다.

### Canvas/InventoryWindow/Inventory/.../Content/Item/Use/Gs2InventoryItemSetConsumeAction

![img_21.png](img_21.png)

이 컨텍스트 메뉴에서 프리팹을 추가할 수 있습니다.

**Gs2InventoryItemSetConsumeAction**

Consume Count 로 소비할 수량을 지정할 수 있습니다.

프리팹을 활성화함으로써 Gs2InventoryOwnItemSetContext 에서 지정한 아이템을 소비합니다.

### Canvas/InventoryWindow/Input/Acquire Potion

![img_22.png](img_22.png)

**Gs2ExchangeRateModelContext**

GS2-Inventory 의 Potion 을 입수하기 위한 GS2-Exchange 의 교환 비율을 설정합니다.

![img_24.png](img_24.png)
![img_23.png](img_23.png)

여기서는 GS2-Exchange 의 exchange-0001 이라는 이름의 네임스페이스 의 Potion 이라는 이름의 교환 비율을 지정하고 있습니다.

### Canvas/InventoryWindow/Input/Acquire Potion/Gs2ExchangeExchangeExchangeAction

**Gs2ExchangeExchangeExchangeAction**

Count 로 교환할 수량을 지정할 수 있습니다.

프리팹을 활성화함으로써 Gs2ExchangeRateModelContext 에서 지정한 교환을 실행합니다.

### Canvas/InventoryWindow/Input/Acquire Ether

**Gs2ExchangeRateModelContext**

![img_25.png](img_25.png)

GS2-Inventory 의 Ether 를 입수하기 위한 GS2-Exchange 의 교환 비율을 설정합니다.

![img_27.png](img_27.png)
![img_26.png](img_26.png)

여기서는 GS2-Exchange 의 exchange-0001 이라는 이름의 네임스페이스 의 Potion 이라는 이름의 교환 비율을 지정하고 있습니다.

### Canvas/InventoryWindow/Input/Acquire Ether/Gs2ExchangeExchangeExchangeAction

![img_28.png](img_28.png)

**Gs2ExchangeExchangeExchangeAction**

Count 로 교환할 수량을 지정할 수 있습니다.

프리팹을 활성화함으로써 Gs2ExchangeRateModelContext 에서 지정한 교환을 실행합니다.

### Process/Gs2AutoLogin

로그인 처리를 실행하는 프리팹이 설정되어 있습니다.

[Account 샘플]() 에서 설명하고 있으니 그쪽을 참조해 주십시오.




