- Home /
Inventory GUI
Hello I downloaded this script from http://forum.unity3d.com/threads/my-contribution-inventory-system-free.53599/
a very basic inventory script but I have 2 issue with it.
1] I want to open/close the inventory with keydown ''i'' 2] I want to put the inventory box on the right side. It fully customize but the value on x stop at 999 and the box is only at half the screen. I have try a lot of value change but it down work.
//This type of inventory display will be a bag. similair to WoW.
var backDrop:Texture2D;
var windowPosition:Vector2=Vector2(200,800);//where on the screen the window will appear.
//this can easily be and should be updated on the fly incase the screen size changes or what not.
var windowSize:Vector2=Vector2(96.0,96.0);//the size of the window the bag will be displayed.
var itemIconSize:Vector2=Vector2(32.0,32.0);//The size of the item icons
var updateListDelay=1.0;//This will be used to updated the inventory on screen, rather then
//updating it every time OnGUI is called. if you prefer you can directly get what in the list. but i
//dont like having multiple GetComponents >.<.
var lastUpdate=0.0;//last time we updated the display.
var UpdatedList:Transform[];
var associatedInventory:Inventory;
function UpdateInventoryList(){
UpdatedList=associatedInventory.Contents;
}
function OnGUI(){
//THIS BLOCK OF CODE IS JUST FOR PEOPLE TO MOVE THE BOX AROUND.
//If your making a game you dont need anything this this.
windowPosition.x = int.Parse(GUI.TextField(Rect (100, 10, 40, 20), ""+windowPosition.x, 3));
windowPosition.y = int.Parse(GUI.TextField(Rect (100, 30, 40, 20), ""+windowPosition.y, 3));
windowSize.x = int.Parse(GUI.TextField(Rect (100, 50, 40, 20), ""+windowSize.x, 3));
windowSize.y = int.Parse(GUI.TextField(Rect (100, 70, 40, 20), ""+windowSize.y, 3));
itemIconSize.x = int.Parse(GUI.TextField(Rect (100, 90, 40, 20), ""+itemIconSize.x, 3));
itemIconSize.y = int.Parse(GUI.TextField(Rect (100, 110, 40, 20), ""+itemIconSize.y, 3));
GUI.Label(Rect (0, 20, 400, 20), "WindowPosition X:");
GUI.Label(Rect (0, 30, 400, 20), "WindowPosition Y");
GUI.Label(Rect (0, 50, 400, 20), "WindowSize X");
GUI.Label(Rect (0, 70, 400, 20), "WindowSize Y");
GUI.Label(Rect (0, 90, 400, 20), "ItemIconSize X");
GUI.Label(Rect (0, 110, 400, 20), "ItemIconSize Y");
//THIS IS WHERE THE EDITING STUFF ENDS> FROM HERE BEFORE YOU would need.
var currentX=windowPosition.x;//where to put the first items
var currentY=windowPosition.y;
//Draw the backdrop in the windowposition and the size of the windowsize.
GUI.DrawTexture(Rect(windowPosition.x,windowPosition.y,windowSize.x,windowSize.y),backDrop,ScaleMode.StretchToFill);
for(var i:Transform in UpdatedList){//we start a loop for whats in our list. You could
var item=i.GetComponent(Item);//we know that all objects in this list are items, cus we
//will make sure nothing else can go in here, RIGHT? :P
//directly call accocialtedInventory.Contents but i prefer not to since its more work for you and the pc.
//I use a button since its easier to be able to click it and then made a drop down menu to delete or move
if(GUI.Button(Rect(currentX,currentY,itemIconSize.x,itemIconSize.y),item.inventoryIcon)){
associatedInventory.RemoveItem(i);//Remove the item from the list, well its transform neways
item.BeDropped();//Drops the item.
lastUpdate=0.0;//Set the lastupdate to 0 to allow the list to update.
}
currentX+=itemIconSize.x;
if(currentX+itemIconSize.x>windowPosition.x+windowSize.x){
//if the next item icon will be to large for the window.....
currentX=windowPosition.x;//we move it back to its startpoint
currentY+=itemIconSize.y;//and down a row.
if(currentY+itemIconSize.y>windowPosition.y+windowSize.y){//if the row is down to far. we quit the loop
return;
}
}
}
}
function FixedUpdate(){//I will update the display inventory here.
if(Time.time>lastUpdate){
lastUpdate=Time.time+updateListDelay;
UpdateInventoryList();
}
}
Answer by YoungDeveloper · Jul 03, 2015 at 12:44 AM
I haven't seen such bad code for a while. To show/hide create a boolean flag which you toggle on button press.
private var render:boolean = false;
function Update(){
//on button press
render = !render;
}
function OnGUI(){
if(render){
//your inventory
}
}
Your answer
Follow this Question
Related Questions
Auto-snap script? 1 Answer
Drill-like system 0 Answers
An Instantiated object to array problem 2 Answers
Help with Steering 0 Answers
How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers