< Summary

Class:Inventory
Assembly:Test
File(s):E:/Unity/Unity Project/EscapeTheRoomVR/Assets/Test 1/Inventory.cs
Covered lines:28
Uncovered lines:24
Coverable lines:52
Total lines:86
Line coverage:53.8% (28 of 52)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:9
Method coverage:77.7% (7 of 9)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Inventory()0%000100%
Start()0%000100%
Update()0%00050%
ToggleVisibility()0%000100%
AddItem(...)0%00068.75%
RemoveItem(...)0%0000%
SelectItem(...)0%0000%

File(s)

E:/Unity/Unity Project/EscapeTheRoomVR/Assets/Test 1/Inventory.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.UI;
 5
 6public class Inventory : MonoBehaviour {
 7    private bool isVisible;
 8    public const int numItemSlots = 9;
 29  public Image[] itemImages = new Image[numItemSlots];
 210  public Item[] items = new Item[numItemSlots];
 111  public Item SelectedItem { get; private set; }
 12
 113  void Start() {
 114    isVisible = false;
 115    SelectedItem = null;
 116  }
 17
 589318  void Update() {
 19        // 如果 App 按钮被按下,则显示或隐藏背包
 589320        if (Input.GetKeyDown(KeyCode.JoystickButton1)) {
 021            ToggleVisibility();
 022        }
 589323  }
 24
 225  public void ToggleVisibility() {
 26        // Get canvas where inventory is drawn
 227    CanvasGroup cg = GetComponent<CanvasGroup>();
 328        if (isVisible) { // Hide and let controller's laser pointer through
 129      cg.alpha = 0;
 130      cg.blocksRaycasts = false;
 231        } else { // Show and don't let controller's laser pointer through
 132      cg.alpha = 1;
 133      cg.blocksRaycasts = true;
 134    }
 35        // Toggle visibility
 236    isVisible = !isVisible;
 237  }
 38
 239  public void AddItem(Item itemToAdd) {
 40        // Find first empty slot in inventory to place item
 941    for (int i = 0; i < items.Length; i++) {
 542      if (items[i] == null) {
 43                // Store item information
 244        items[i] =  itemToAdd;
 45
 46                // Get corresponding cell of empty slot in inventory grid
 247        GameObject cell = GameObject.Find("Cell" + (i+1));
 248        itemImages[i] = cell.GetComponentInChildren<Image>();
 49
 50                // Set item details for appearance in grid
 051        itemImages[i].sprite = itemToAdd.sprite;
 052        itemImages[i].enabled = true;
 053        itemImages[i].color = Color.white;
 54
 055        return;
 56      }
 157    }
 058  }
 59
 060  public void RemoveItem(Item itemToRemove) {
 061    for (int i = 0; i < items.Length; i++) {
 62            // Find the correct item to remove
 063      if (items[i] == itemToRemove) {
 064        items[i] = null; // Clear item
 65
 66                // Reset correct item details for removing its appearance in grid
 067        itemImages[i].sprite = null;
 068        itemImages[i].enabled = false;
 069        itemImages[i].color = Color.clear;
 70
 71                // If the item to remove is select, deselect it
 072                if (itemToRemove == SelectedItem) {
 073                    SelectedItem = null;
 074                }
 75
 076        return;
 77      }
 078    }
 079  }
 80
 081  public void SelectItem(int i) {
 82        // Set item as selected for later use with Usable game object
 083    SelectedItem = items[i];
 084    Debug.Log("Selected item: " + i);
 085  }
 86}