using System; using System.Collections; using System.Collections.Generic; using TMPro; using Unity.Mathematics; using UnityEngine; using UnityEngine.XR.Interaction.Toolkit; [ExecuteAlways] public class InventoryItem : MonoBehaviour { public GameObject itemPrefab; private GameObject _childPrefab; private TMP_Text itemCounterText; private ItemData itemData; public ItemSaveData saveData; private void OnEnable() { if (itemPrefab == null) { enabled = false; //Debug.LogError("Item has no attached prefab"); return; } enabled = true; if (itemCounterText == null) itemCounterText = GetComponentInChildren(); _childPrefab = GetComponentInChildren()?.prefab.gameObject; //Only attaches the prefab if it doesn't already have one ([ExecuteAlways] causes one to be spawned in editor). if (_childPrefab != null) return; _childPrefab = Instantiate(itemPrefab, transform); //todo switch to multiply instead so items with non 1,1,1 scale will retain their proportions _childPrefab.transform.localPosition = GetComponentInChildren().positionInContainer; _childPrefab.transform.localScale = GetComponentInChildren().scaleInContainer; _childPrefab.transform.rotation = quaternion.identity; //Disable prefab components that arent relevant. var colliderComponent = _childPrefab.GetComponent(); if (colliderComponent != null) colliderComponent.enabled = false; var rigidbodyComponent = _childPrefab.GetComponent(); if (rigidbodyComponent != null) { rigidbodyComponent.isKinematic = true; rigidbodyComponent.useGravity = false; } var xrGrabInteractableComponent = _childPrefab.GetComponent(); if (xrGrabInteractableComponent != null) xrGrabInteractableComponent.enabled = false; } private void Start() { itemData = _childPrefab.GetComponent(); if (itemData == null) Debug.LogError("Item prefab has no attached ItemData", _childPrefab); Debug.Log(saveData); if (string.IsNullOrEmpty(saveData.assetPath)) { saveData.assetPath = itemData.assetLocation; saveData.count = saveData.count == 0 ? 1 : saveData.count; } UpdateText(); } private void UpdateText() { itemCounterText.text = itemData.canStack ? saveData.count.ToString() : ""; } /** * Changes how many items are in this itemcontainer. * Returns false if attempted to remove more items than exists in the container. */ public bool ChangeCount(int amount) { if (saveData.count + amount < 0) return false; saveData.count += amount; if (saveData.count == 0) { //Destroys this gameobject if the count is 0 or lower Destroy(gameObject); return true; } UpdateText(); return true; } public void PopItem() { GameObject item = Instantiate(itemData.prefab, transform.position + transform.forward, Quaternion.identity); item.transform.localScale = Vector3.one; item.transform.name = itemData.itemName; var colliderComponent = item.GetComponent(); if (colliderComponent != null) colliderComponent.enabled = true; var rigidbodyComponent = item.GetComponent(); if (rigidbodyComponent != null) { rigidbodyComponent.isKinematic = false; rigidbodyComponent.useGravity = true; } var xrGrabInteractableComponent = item.GetComponent(); if (xrGrabInteractableComponent != null) xrGrabInteractableComponent.enabled = true; ChangeCount(-1); } public int GetItemid() { return itemData.itemId;; } public override string ToString() { return $"{base.ToString()}, Count: {saveData.count}, ItemData: {itemData}"; } }