Advertisement
Guest User

Untitled

a guest
Aug 26th, 2013
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // InventoryItem.js
  2.  
  3. // Icon texture for GUI.
  4. var icon:Texture2D;
  5. // Label for GUI.
  6. var iconLabel:string;
  7.  
  8. // other item data which you may need...
  9.  
  10. function Activate() {
  11.     // Activation logic for inventory item.
  12.     gameObject.SetActive(true);
  13. }
  14.  
  15. function Deactivate() {
  16.     // Deactivation logic for inventory item.
  17.     gameObject.SetActive(false);
  18. }
  19.  
  20.  
  21. // Inventory.js
  22.  
  23. import System.Collections.Generic;
  24.  
  25. // Fixed length array of items, though you may want to use
  26. // the generic `List` class if you wanted varying length.
  27. var items:List.<InventoryItem> = List.<InventoryItem>();
  28.  
  29. // Represents the active inventory item.
  30. var activeItem:InventoryItem;
  31.  
  32. // Temporary content instance.
  33. private var _itemButtonContent:GUIContent = new GUIContent();
  34.  
  35. function OnGUI() {
  36.     var restoreColor : Color = GUI.contentColor;
  37.  
  38.     GUILayout.BeginArea(new Rect(0, 0, Screen.width, 70));
  39.     GUILayout.BeginHorizontal();
  40.    
  41.     // Number of item slots in GUI.
  42.     var slotCount:int = 10;
  43.  
  44.     // Draw buttons for inventory items.
  45.     for (var i = 0; i < slotCount; ++i) {
  46.         // Show empty non-clickable button?
  47.         if (i >= this.items.Count || this.items[i] == null) {
  48.             GUI.enabled = false;
  49.             GUILayout.Button(GUIContent.none);
  50.             GUI.enabled = true;
  51.         }
  52.         else {
  53.             var item : InventoryItem = this.items[i];
  54.  
  55.             // Show clickable button.
  56.             _itemButtonContent.image = item.icon;
  57.             _itemButtonContent.text = item.iconLabel;
  58.  
  59.             // Highlight active item.
  60.             GUI.contentColor = (item == this.activeItem)
  61.                 ? Color.red
  62.                 : restoreColor;
  63.  
  64.             if (GUILayout.Button(_itemButtonContent))
  65.                 this.ActivateItem(item);
  66.         }
  67.     }
  68.  
  69.     GUILayout.EndHorizontal();
  70.     GUILayout.EndArea();
  71.  
  72.     GUI.contentColor = restoreColor;
  73. }
  74.  
  75. function ActivateItem(item:InventoryItem) {
  76.     // Don't bother, this item is already active!
  77.     if (item == this.activeItem)
  78.         return;
  79.  
  80.     this.DeactivateItem();
  81.  
  82.     // Active inventory item!
  83.     this.activeItem = item;
  84.     item.Activate();
  85. }
  86.  
  87. function DeactivateItem() {
  88.     // No item is active, bail!
  89.     if (this.activeItem == null)
  90.         return;
  91.  
  92.     activeItem.Deactivate();
  93.     this.activeItem = null;
  94. }
  95.  
  96.  
  97. // CollectableTorchObject.js
  98.  
  99. // The associated inventory item.
  100. var item:InventoryItem;
  101.  
  102. function OnTriggerEnter(other:Collider) {
  103.     // Has player collided with collectable torch object?
  104.     if (other.tag == "Player") {
  105.         // Add item to inventory.
  106.         var inventory:Inventory = GameObject.Find("Inventory").GetComponent.<Inventory>();
  107.         inventory.items.Add(this.item);
  108.        
  109.         // Automatically activate new inventory item?
  110.         inventory.ActivateItem(this.item);
  111.        
  112.         // Destroy collectable object!
  113.         Destroy(this.gameObject);
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement