je travaille sur un système d'inventaire pour mon jeux mais je rencontre un problème:
J'ai créé 2 SCriptableobject:
1. "Inventory item" pour représenter les objets d'inventaire
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName ="New Item", menuName ="Inventory/Items")]
public class InventoryItem : ScriptableObject
{
public string itemName;
public string itemDescription;
public Sprite itemImage;
public int numberHeld;
public bool usable;
public bool unique;
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Inventory", menuName = "Inventory/Player Inventory")]
public class PlayerInventory : ScriptableObject
{
public List<InventoryItem> myInventory = new List<InventoryItem>();
}
1. InventorySlot qui sert a faire le lien entre le gameobject dans la scène et les Scriptable object créé grâce au script vu précédemment:
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class InventorySlot : MonoBehaviour
{
[Header("UI Stuff to change")]
[SerializeField] private TextMeshProUGUI itemNumberText;
[SerializeField] private Image itemImage;
[Header("Variable from the item")]
public InventoryItem thisItem;
public InventoryManager thisManager;
public void Setup(InventoryItem newItem, InventoryManager newManager)
{
thisItem = newItem;
thisManager = newManager;
if(thisItem)
{
itemImage.sprite = thisItem.itemImage;
itemNumberText.text = ""+thisItem.numberHeld;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class InventoryManager : MonoBehaviour
{
[Header("Inventory Information")]
public PlayerInventory playerInventory;
[SerializeField] private GameObject blankInventorySlot;
[SerializeField] private GameObject inventoryPanel;
[SerializeField] private TextMeshProUGUI descriptionText;
public void setTextAndButton(string description, bool buttonActive)
{
descriptionText.text = description;
if(buttonActive)
{
blankInventorySlot.SetActive(true);
}
else
{
blankInventorySlot.SetActive(true);
}
}
void MakeInventorySlots()
{
if(playerInventory)
{
for(int i = 0; i < playerInventory.myInventory.Count; i++)
{
GameObject temp = Instantiate(blankInventorySlot, inventoryPanel.transform.position, Quaternion.identity);
temp.transform.SetParent(inventoryPanel.transform);
temp.transform.localScale = new Vector3(1, 1, 1);
InventorySlot newSlot = temp.GetComponent<InventorySlot>();
if(newSlot)
{
newSlot.Setup(playerInventory.myInventory[i], this);
}
}
}
}
// Start is called before the first frame update
void Start()
{
MakeInventorySlots();
setTextAndButton(" ", true);
}
// Update is called once per frame
void Update()
{
}
}
L'inventaire affiche le bon nombre d'objet (contenu dans le player inventory) mais ceux-ci ne reprennent pas les propriétés (l'image, le nombre, la description, etc...)