Skip to content

Instantly share code, notes, and snippets.

@jordanske jordanske/Interactable.cs Secret
Created Aug 7, 2017

Embed
What would you like to do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public abstract class Interactable : MonoBehaviour
{
[SerializeField]
protected UnityEvent OnInteractEvent;
public abstract void Interact();
}
using UnityEngine;
using System.Collections;
[AddComponentMenu("Redemption/Interactables/Pickup")]
public class InteractablePickup : Interactable
{
private bool isInteracting;
[SerializeField]
private InventoryItem inventoryItem;
public void Start()
{
isInteracting = false;
}
public override void Interact()
{
if(isInteracting) {
return;
}
OnInteractEvent.Invoke();
Debug.Log("Pickup item : " + inventoryItem.name);
InventoryManager.instance.AddItem(inventoryItem);
Destroy(gameObject);
}
}
using UnityEngine;
[CreateAssetMenu(fileName="InventoryItem", menuName="Redemption/Inventory Item")]
public class InventoryItem : ScriptableObject {
[SerializeField]
[Tooltip("Item icon that is shown in the inventory")]
private Sprite itemIcon;
[SerializeField]
[Tooltip("Item name that is shown in the tooltip")]
private string itemName;
[SerializeField]
[TextArea(3, 10)]
[Tooltip("Item description that is shown in the tooltip")]
private string itemDescription;
[SerializeField]
[Tooltip("Gameobject to show when item is held in hand")]
private GameObject itemHandPrefab;
public Sprite ItemIcon {
get {
return itemIcon;
}
}
public string ItemName {
get {
return itemName;
}
}
public string ItemDescription {
get {
return itemDescription;
}
}
public GameObject ItemHandPrefab {
get {
return itemHandPrefab;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryManager : MonoBehaviour
{
public static InventoryManager instance;
[SerializeField]
private GameObject inventory;
[SerializeField]
private GameObject itemSlotPrefab;
private int inventorySize;
[SerializeField]
private int inventoryColumns;
[SerializeField]
private int inventoryRows;
//[SerializeField]
private Image[] itemImages;
[SerializeField]
private InventoryItem[] items;
private bool inventoryActive;
public bool InventoryActive {
get {
return inventoryActive;
}
}
void Start ()
{
inventoryActive = false;
inventory.SetActive(false);
instance = this;
}
void Update()
{
}
public void AddItem(InventoryItem item)
{
for(int i = 0; i < inventorySize; i++) {
if(items[i] == null) {
items[i] = item;
itemImages[i].sprite = item.ItemIcon;
itemImages[i].enabled = true;
return;
}
}
}
public void RemoveItem(InventoryItem item)
{
for(int i = 0; i < inventorySize; i++) {
if(items[i] == item) {
items[i] = null;
itemImages[i].sprite = null;
itemImages[i].enabled = false;
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.