Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // InventoryItem.js
- // Icon texture for GUI.
- var icon:Texture2D;
- // Label for GUI.
- var iconLabel:string;
- // other item data which you may need...
- function Activate() {
- // Activation logic for inventory item.
- gameObject.SetActive(true);
- }
- function Deactivate() {
- // Deactivation logic for inventory item.
- gameObject.SetActive(false);
- }
- // Inventory.js
- import System.Collections.Generic;
- // Fixed length array of items, though you may want to use
- // the generic `List` class if you wanted varying length.
- var items:List.<InventoryItem> = List.<InventoryItem>();
- // Represents the active inventory item.
- var activeItem:InventoryItem;
- // Temporary content instance.
- private var _itemButtonContent:GUIContent = new GUIContent();
- function OnGUI() {
- var restoreColor : Color = GUI.contentColor;
- GUILayout.BeginArea(new Rect(0, 0, Screen.width, 70));
- GUILayout.BeginHorizontal();
- // Number of item slots in GUI.
- var slotCount:int = 10;
- // Draw buttons for inventory items.
- for (var i = 0; i < slotCount; ++i) {
- // Show empty non-clickable button?
- if (i >= this.items.Count || this.items[i] == null) {
- GUI.enabled = false;
- GUILayout.Button(GUIContent.none);
- GUI.enabled = true;
- }
- else {
- var item : InventoryItem = this.items[i];
- // Show clickable button.
- _itemButtonContent.image = item.icon;
- _itemButtonContent.text = item.iconLabel;
- // Highlight active item.
- GUI.contentColor = (item == this.activeItem)
- ? Color.red
- : restoreColor;
- if (GUILayout.Button(_itemButtonContent))
- this.ActivateItem(item);
- }
- }
- GUILayout.EndHorizontal();
- GUILayout.EndArea();
- GUI.contentColor = restoreColor;
- }
- function ActivateItem(item:InventoryItem) {
- // Don't bother, this item is already active!
- if (item == this.activeItem)
- return;
- this.DeactivateItem();
- // Active inventory item!
- this.activeItem = item;
- item.Activate();
- }
- function DeactivateItem() {
- // No item is active, bail!
- if (this.activeItem == null)
- return;
- activeItem.Deactivate();
- this.activeItem = null;
- }
- // CollectableTorchObject.js
- // The associated inventory item.
- var item:InventoryItem;
- function OnTriggerEnter(other:Collider) {
- // Has player collided with collectable torch object?
- if (other.tag == "Player") {
- // Add item to inventory.
- var inventory:Inventory = GameObject.Find("Inventory").GetComponent.<Inventory>();
- inventory.items.Add(this.item);
- // Automatically activate new inventory item?
- inventory.ActivateItem(this.item);
- // Destroy collectable object!
- Destroy(this.gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement