Search for “FIX_ME”. I need to know what to do with the var inventory : Array, because only Javascript can use that class. And there’s one issue with FIX_ME newItem= new InventoryItem(). I think that might need “InventoryItem” before it, but I’m not sure.
Original Javascipt
//Our inventory array
var inventory : Array;
public var emptyTex : Texture; //This will be drawn when a slot is empty
public var inventorySizeX = 8; //the size of the inventory in x and y dimension
public var inventorySizeY = 5;
var iconWidthHeight = 20; //The pixel size (height and width) of an inventory slot
var spacing = 4; //Space between slots (in x and y)
public var offSet = Vector2( 100, 100 ); //set the position of the inventory
// TEST VARIABLES
// Assign these to test adding Items with mouse clicks (see Update())
public var testTex : Texture;
public var testTex2 : Texture;
//Our Representation of an InventoryItem
class InventoryItem
{
//GameObject this item refers to
var worldObject : GameObject;
//What the item will look like in the inventory
var texRepresentation : Texture;
}
function Awake()
{
inventory = new Array(inventorySizeX);
for( var i = 0; i < inventory.length; i ++ )
{
inventory *= new Array(inventorySizeY);*
}
}
function OnGUI()
{
- var texToUse : Texture;*
- var currentInventoryItem : InventoryItem;*
//Go through each row
for( var i = 0; i < inventory.length; i ++ )
{
// and each column
for( var k = 0; k < inventory*.length; k ++ )*
{
* texToUse = emptyTex;*
_ currentInventoryItem = inventory*[k];
//if there is an item in the i-th row and the k-th column, draw it*
if( inventory*[k] != null )*
{
texToUse = currentInventoryItem.texRepresentation;
}
GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
}
}
}
function AddItem( item : InventoryItem )
{
* //Go through each row*
for( var i = 0; i < inventory.length; i ++ )
{
// and each column
for( var k = 0; k < inventory*.length; k ++ )*
{
* //If the position is empty, add the new item and exit the function*
if( inventory*[k] == null )
{
inventory[k] = item;
return;
}
}
}
//If we got this far, the inventory is full, do somethign appropriate here
}
function AddItem( worldObject : GameObject, texRep : Texture )
{
var newItem = new InventoryItem();
newItem.worldObject = worldObject;
newItem.texRepresentation = texRep;
AddItem( newItem );
}
function Update()
{
if( Input.GetMouseButtonDown( 0 ) )
{
AddItem( gameObject, testTex );
}*_
* if( Input.GetMouseButtonDown( 1 ) )*
* {*
* AddItem( gameObject, testTex2 ); *
* } *
}
C#
using UnityEngine;
using System.Collections;
public class InventoryTestCS : MonoBehaviour {
//Our inventory array
FIX_ME Array inventory;
public Texture emptyTex; //This will be drawn when a slot is empty
public int inventorySizeX= 8; //the size of the inventory in x and y dimension
public int inventorySizeY= 5;
int iconWidthHeight= 20; //The pixel size (height and width) of an inventory slot
int spacing= 4; //Space between slots (in x and y)
public Vector2 offSet = Vector2 (100, 100); //set the position of the inventory
// TEST VARIABLES
// Assign these to test adding Items with mouse clicks (see Update())
public Texture testTex;
public Texture testTex2;
//Our Representation of an InventoryItem
class InventoryItem
{
//GameObject this item refers to
GameObject worldObject;
//What the item will look like in the inventory
Texture texRepresentation;
}
// Create the Inventory
void Awake()
{
inventory = new Array(inventorySizeX);
for (int i= 0; i < inventory.length; i++)
{
inventory = new Array(inventorySizeY);
}
}
void OnGUI()
{
Texture texToUse;
InventoryItem currentInventoryItem;
//Go through each row
for (int i= 0; i < inventory.length; i++)
{
// and each column
for (int k= 0; k < inventory*.length; k++)*
{
texToUse = emptyTex;
currentInventoryItem = inventory*[k];*
//if there is an item in the i-th row and the k-th column, draw it
if (inventory*[k] != null)*
{
texToUse = currentInventoryItem.texRepresentation;
}
GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
}
}
}
void AddItem(InventoryItem item)
{
//Go through each row
for (int i= 0; i < inventory.length; i++)
{
// and each column
for (int k= 0; k < inventory*.length; k++)*
{
//If the position is empty, add the new item and exit the function
if (inventory*[k] == null)*
{
inventory*[k] = item;*
return;
}
}
}
//If we got this far, the inventory is full, do somethign appropriate here
}
void AddItem(GameObject worldObject, Texture texRep){
FIX_ME newItem= new InventoryItem();
newItem.worldObject = worldObject;
newItem.texRepresentation = texRep;
AddItem(newItem);
}
void Update()
{
if( Input.GetMouseButtonDown( 0 ) )
{
AddItem( gameObject, testTex );
}
if( Input.GetMouseButtonDown( 1 ) )
{
AddItem( gameObject, testTex2 );
}
}
}