| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using UnityEngine; |
| | | 4 | | using UnityEngine.UI; |
| | | 5 | | |
| | | 6 | | public class Inventory : MonoBehaviour { |
| | | 7 | | private bool isVisible; |
| | | 8 | | public const int numItemSlots = 9; |
| | 2 | 9 | | public Image[] itemImages = new Image[numItemSlots]; |
| | 2 | 10 | | public Item[] items = new Item[numItemSlots]; |
| | 1 | 11 | | public Item SelectedItem { get; private set; } |
| | | 12 | | |
| | 1 | 13 | | void Start() { |
| | 1 | 14 | | isVisible = false; |
| | 1 | 15 | | SelectedItem = null; |
| | 1 | 16 | | } |
| | | 17 | | |
| | 2441 | 18 | | void Update() { |
| | | 19 | | // 如果 App 按钮被按下,则显示或隐藏背包 |
| | 2441 | 20 | | if (Input.GetKeyDown(KeyCode.JoystickButton1)) { |
| | 0 | 21 | | ToggleVisibility(); |
| | 0 | 22 | | } |
| | 2441 | 23 | | } |
| | | 24 | | |
| | 2 | 25 | | public void ToggleVisibility() { |
| | | 26 | | // Get canvas where inventory is drawn |
| | 2 | 27 | | CanvasGroup cg = GetComponent<CanvasGroup>(); |
| | 3 | 28 | | if (isVisible) { // Hide and let controller's laser pointer through |
| | 1 | 29 | | cg.alpha = 0; |
| | 1 | 30 | | cg.blocksRaycasts = false; |
| | 2 | 31 | | } else { // Show and don't let controller's laser pointer through |
| | 1 | 32 | | cg.alpha = 1; |
| | 1 | 33 | | cg.blocksRaycasts = true; |
| | 1 | 34 | | } |
| | | 35 | | // Toggle visibility |
| | 2 | 36 | | isVisible = !isVisible; |
| | 2 | 37 | | } |
| | | 38 | | |
| | 2 | 39 | | public void AddItem(Item itemToAdd) { |
| | | 40 | | // Find first empty slot in inventory to place item |
| | 9 | 41 | | for (int i = 0; i < items.Length; i++) { |
| | 5 | 42 | | if (items[i] == null) { |
| | | 43 | | // Store item information |
| | 2 | 44 | | items[i] = itemToAdd; |
| | | 45 | | |
| | | 46 | | // Get corresponding cell of empty slot in inventory grid |
| | 2 | 47 | | GameObject cell = GameObject.Find("Cell" + (i+1)); |
| | 2 | 48 | | itemImages[i] = cell.GetComponentInChildren<Image>(); |
| | | 49 | | |
| | | 50 | | // Set item details for appearance in grid |
| | 0 | 51 | | itemImages[i].sprite = itemToAdd.sprite; |
| | 0 | 52 | | itemImages[i].enabled = true; |
| | 0 | 53 | | itemImages[i].color = Color.white; |
| | | 54 | | |
| | 0 | 55 | | return; |
| | | 56 | | } |
| | 1 | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | public void RemoveItem(Item itemToRemove) { |
| | 0 | 61 | | for (int i = 0; i < items.Length; i++) { |
| | | 62 | | // Find the correct item to remove |
| | 0 | 63 | | if (items[i] == itemToRemove) { |
| | 0 | 64 | | items[i] = null; // Clear item |
| | | 65 | | |
| | | 66 | | // Reset correct item details for removing its appearance in grid |
| | 0 | 67 | | itemImages[i].sprite = null; |
| | 0 | 68 | | itemImages[i].enabled = false; |
| | 0 | 69 | | itemImages[i].color = Color.clear; |
| | | 70 | | |
| | | 71 | | // If the item to remove is select, deselect it |
| | 0 | 72 | | if (itemToRemove == SelectedItem) { |
| | 0 | 73 | | SelectedItem = null; |
| | 0 | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | return; |
| | | 77 | | } |
| | 0 | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | public void SelectItem(int i) { |
| | | 82 | | // Set item as selected for later use with Usable game object |
| | 0 | 83 | | SelectedItem = items[i]; |
| | 0 | 84 | | Debug.Log("Selected item: " + i); |
| | 0 | 85 | | } |
| | | 86 | | } |