Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Snake111 · Jul 03, 2015 at 12:11 AM · script.

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();
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
    }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges