116 lines
3.0 KiB
C#
116 lines
3.0 KiB
C#
using UnityEngine;
|
|||
|
|
using System.Collections;
|
||
|
|
using UnityEngine.EventSystems;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public class InventoryUIController : UiController {
|
||
|
|
|
||
|
|
public Inventory inventory;
|
||
|
|
public ItemsView itemsView;
|
||
|
|
public UnityEngine.UI.Image selectionImage;
|
||
|
|
public UnityEngine.UI.Text selectionText;
|
||
|
|
public UnityEngine.UI.Button removeButton;
|
||
|
|
public UnityEngine.UI.Button equipButton;
|
||
|
|
private ItemViewController _selectedItem;
|
||
|
|
|
||
|
|
public void Rebuild() {
|
||
|
|
itemsView.Items = inventory.Items;
|
||
|
|
ResetFocus();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ResetFocus() {
|
||
|
|
var es = EventSystemManager.currentSystem;
|
||
|
|
if (itemsView.Views.Count > 0) {
|
||
|
|
es.SetSelectedGameObject(itemsView.Views[0].itemButton.gameObject, new BaseEventData(es));
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
// Use this for initialization
|
||
|
|
void Start () {
|
||
|
|
if (itemsView == null) {
|
||
|
|
Debug.LogWarning("Items view not set up! Inventory UI would not function properly!");
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
itemsView.SelectionChanged += itemsView_SelectionChanged;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (equipButton != null) {
|
||
|
|
equipButton.onClick.AddListener(OnEquipClick);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
Debug.LogWarning("Equip button not assigned!");
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnEquipClick(UnityEngine.UI.Button button) {
|
||
|
|
EquippedChange();
|
||
|
|
}
|
||
|
|
|
||
|
|
void EquippedChange() {
|
||
|
|
var item = itemsView.Selection.item;
|
||
|
|
if (item != null && item.description.IsEquippable) {
|
||
|
|
if (item.equipped) {
|
||
|
|
inventory.UnequipItem(item);
|
||
|
|
} else {
|
||
|
|
inventory.EquipItem(item);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void itemsView_SelectionChanged(ItemsView container) {
|
||
|
|
if (_selectedItem == container.Selection) {
|
||
|
|
EquippedChange();
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
_selectedItem = container.Selection;
|
||
|
|
selectionImage.sprite = container.Selection.item.inventoryIcon;
|
||
|
|
selectionText.text = container.Selection.item.description.Description;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
UpdateButtons();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void UpdateButtons() {
|
||
|
|
if (itemsView == null || itemsView.Selection == null || itemsView.Selection.item == null) {
|
||
|
|
if (equipButton != null) {
|
||
|
|
equipButton.gameObject.SetActive(false);
|
||
|
|
}
|
||
|
|
if (removeButton != null) {
|
||
|
|
removeButton.gameObject.SetActive(false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
var item = itemsView.Selection.item;
|
||
|
|
|
||
|
|
//update equip button
|
||
|
|
if (!item.description.IsEquippable) {
|
||
|
|
equipButton.gameObject.SetActive(false);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
equipButton.gameObject.SetActive(true);
|
||
|
|
if (item.equipped) {
|
||
|
|
equipButton.GetComponentInChildren<Text>().text = "Unequip";
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
equipButton.GetComponentInChildren<Text>().text = "Equip";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update () {
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|