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 Skylermaki · Aug 20, 2013 at 09:20 AM · gui

Inventory smoothing

Hi, i wrote this drag and drop code for my inventory, it's working but not smooth. I'm stuck because I have no idea how to put this party in the Update() function.

The the "for" make the game laggy when it's in a onGUI function.

 //Drag/Drop
 var column:int = 6;
 for(x = 0; x < column; x++){ 
     for(y = 0; y < 3; y++){
         box[x+(y*column)] = Rect(-5 + (x * 36),5 + (y * 36), 36, 36);
         GUI.Box(box[x+y*column], Slot[x+(y*column)]);
         
         if(Event.current.type == EventType.MouseDown){    
             if(box[x+(y*column)].Contains(Event.current.mousePosition)){
                 UI.toggle = true;
                 dragging = true;
                 first_Number = x+(y*column);    
                 first_Name = inv.Name[first_Number];
                 first_Quantity = inv.Quantity[first_Number];
             }                
         }
         if(box[x+(y*column)].Contains(Event.current.mousePosition)){
             over_Number = x+(y*column);            
         }            
         if(Event.current.type == EventType.MouseUp){
             dragging = false;
             inv.Name[first_Number] = inv.Name[over_Number];
             inv.Quantity[first_Number] = inv.Quantity[over_Number];
             Change();
         }
     }
 }


Here is the [[Full Code]][1]

If you have any idea, thank you.

[1]: http://hastebin.com/pakonicifo.avrasm

Comment
Add comment · Show 5
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 Joyrider · Aug 20, 2013 at 09:21 AM 0
Share

Don't dubble post please. If your question is in moderation, wait for it to get moderated, no need to repost.

avatar image Joyrider · Aug 20, 2013 at 09:26 AM 0
Share

The problem of OnGUI is that it is called multiple times per frame. So it rapidly gets very heavy to process.

If you really have to use OnGUI inGame (which is not at all recommended) You'd be better of checking all this in an update function and just use the OnGUI for the actual interface rendering. (just boolean checks, no mouse checks and events, computing, array manipulations, etc.)

avatar image Skylermaki · Aug 20, 2013 at 09:26 AM 0
Share

The problem is that "Contains(Input.mousePosition)" seems to be not working:

         if(box[x+(y*column)].Contains(Input.mousePosition)){
             over_Number = x+(y*column);        
             Debug.Log("test");
         }

 

And i don't how to retrieve the "for" internal variable to draw the Box in my GUI :

         GUI.Box(box[x+y*column], Slot[x+(y*column)]);
avatar image Joyrider · Aug 20, 2013 at 09:42 AM 0
Share

It should work, but if I'm not mistaken mousePosition and OnGUI drawing have an inverted use of the Y coordinate.

So maybe ins$$anonymous$$d of checking for Input.mousePosition, you should try

 new Vector3(Input.mousePosition.x,Screen.height-Input.mousePosition.y,Input.mousePosition.z) 
avatar image Skylermaki · Aug 20, 2013 at 10:59 AM 0
Share

I've resolved my problem, i don't need to do this. I've reduced the "for" number in the Upddate function and it's smooth now. Thanks for your help :)

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers

Limit on GUI Components? 0 Answers

Trying to view paper object on button press GUI Texture 3 Answers

Floating Enemy Health Bars? 1 Answer

Setting Scroll View Width GUILayout 1 Answer


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