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 davooo0 · Nov 07, 2013 at 11:26 AM · inventoryefficient

how can i make this code more efficient for my inventory system

This is for an inventory system i need help converting some code to make it go through a for loop and make the slots based on a pre defined number of slots

     GUI.Box(Rect(SlotOne),"1");

     GUI.Box(Rect(SlotTwo),"2");
 
     GUI.Box(Rect(SlotThree),"3");
 
     GUI.Box(Rect(SlotFour),"4");
 
     GUI.Box(Rect(SlotFive),"5");
 
     GUI.Box(Rect(SlotSix),"6");
 
     GUI.Box(Rect(SlotSeven),"7");
 
     GUI.Box(Rect(SlotEight),"8");

thats just for the quick slots now i want the inventory to be like minecraft and writing out the boxes one by one will take too long

thanks

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
1
Best Answer

Answer by Azrapse · Nov 07, 2013 at 01:18 PM

I'm not proficent at UnityScript, but I have tried to translate it by checking the reference manual. Please, be aware of possible minor syntax errors.

 import System.Linq;
 
 var slots : Rect[];
 var slotCount: int;
 
 function Start()
 {
     var halfCount = slotCount/2;
     slots = Enumerable.Range(0, slotCount)
         .Select(function(i) 
             Rect((Screen.width/ 2) + (i-halfCount)*100  * Xratio,
                 Screen.height / 2 + 450.0 * Yratio, 82.0 * Xratio, 82.0 * Yratio)
         )
         .ToArray();
 
 }
 
 function OnGUI()
 {
     for(int i=0; i<slots.Length; i++)
     {
         GUI.Box(slots[i], (i+1).ToString());
     }
 }
Comment
Add comment · Show 11 · 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
avatar image tw1st3d · Nov 07, 2013 at 01:23 PM 0
Share

Built for C#,

 using UnityEngine;
 using System.Collections;
 
 sealed class Inventory : $$anonymous$$onoBehavior
 {
     protected Rect[] slots = {};
     
     private void Start()
     {
         slots = { SlotOne, SlotTwo, SlotThree, SlotFour, SlotFive, SlotSix, SlotSeven, SlotEight };
     }
     
     public void OnGUI()
     {
         for(Rect i in slots)
         {
             GUI.Box(new Rect(i), i.ToString());
         }
     }
 }
avatar image Azrapse · Nov 07, 2013 at 01:27 PM 0
Share

Are you using a for ins$$anonymous$$d of a foreach there? Also, in that case i.ToString() won't show a number, but a rectangle data.

avatar image davooo0 · Nov 08, 2013 at 05:32 AM 0
Share

thanks for the answer, @Azapse yes its for the sizes and position

avatar image davooo0 · Nov 08, 2013 at 05:43 AM 0
Share

is there a way i can do this in javascript

avatar image davooo0 · Nov 08, 2013 at 05:46 AM 0
Share

this is the calculations of positions and sizes for the hotbar

SlotOne = Rect((Screen.width/ 2) - 400 Xratio, Screen.height / 2 + 450.0 Yratio, 82.0 Xratio, 82.0 Yratio);

     SlotTwo =  Rect((Screen.width/ 2) - 300  * Xratio,
     Screen.height / 2 + 450.0 * Yratio, 82.0 * Xratio,
     82.0 * Yratio);
 
     SlotThree =  Rect((Screen.width/ 2) - 200 * Xratio,
     Screen.height / 2 + 450.0 * Yratio, 82.0  * Xratio,
     82.0 * Yratio);
Show more comments

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is this a good way to script an inventory system? 2 Answers

saving inventory to database 3 Answers

Inventory Drop Function Problem 1 Answer

How to make a simple inventory?!? Beginner coder JS 1 Answer

Public variable not appearing in the inspector (C#) 2 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