제작 레시피
Coddy Unity 여정의 Unity 게임 아키텍처 섹션에 포함된 레슨. 64개 중 63번째.
챌린지
어려움CraftingBench에 레시피를 추가하세요.
- 직렬화할 수 있는
Ingredient(publicitem,amount)와Recipe(publicresult,List<Ingredient> needs), 그리고 publicList<Recipe> recipes를 추가하세요. Start에서recipeButtons[i]가Craft(i)를 호출하도록 하세요.public void Craft(int index): 필요한 재료가 모두 있으면 재료를 제거하고 결과물 하나를 추가한 다음,Crafted Axe를 로그에 출력하세요.Refresh는 해당 레시피를 제작할 수 있을 때만 버튼을 클릭할 수 있게 합니다.
‘플레이’를 누르고 뗏목을 제작하세요. 그런 다음 ‘확인’을 누르세요.
직접 해보기
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class CraftingBench : MonoBehaviour
{
[SerializeField] private Inventory inventory;
[SerializeField] private Button[] recipeButtons;
[SerializeField] private Button woodButton;
[SerializeField] private Button stoneButton;
[SerializeField] private TMP_Text inventoryText;
void OnEnable()
{
inventory.Changed += Refresh;
}
void OnDisable()
{
inventory.Changed -= Refresh;
}
void Start()
{
woodButton.onClick.AddListener(() => inventory.Add("Wood", 1));
stoneButton.onClick.AddListener(() => inventory.Add("Stone", 1));
// 레시피: 레시피 버튼 i를 누르면 레시피 i를 제작합니다
Refresh();
}
void Refresh()
{
// 레시피: 해당 레시피를 제작할 수 있을 때만 레시피 버튼을 클릭할 수 있습니다
inventoryText.text = "Inventory: " + inventory.Describe();
}
}
Unity 게임 아키텍처의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드