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 ultrasilence · Apr 04, 2014 at 02:54 PM · guiviewport

Correct space GUI elements?

How do I go about correctly spacing gui elements, say textures, that are 32x32 pixel blocks. I've got an orthogonal camera set up, size of 540.

How do I ensure that the elements will be correctly spaced? As of now, I can't even find a way to space them so they're side by side. That is, I can't find a way to convert pixel size to viewport coordinates.

Comment
Add comment · Show 4
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 koray1396 · Apr 04, 2014 at 03:51 PM 0
Share

for gui elements, set position, rotation and scale to 0. then obtain the following values,

 int screenWidth = Screen.width;
 int screenHeight = Screen.height;

then

 GUITexture someGui;

then

 someGui.pixelInset = new Rect(left, top, width, height);

you need to set left, top, width and height accordingly with screen size. like screenWidth * 0.1f etc...

This is a proper and safe way to place gui elements.

In order to fill some area with these elements, is to use loops, for height and for width.

on importing the texture, you set pixels to units. if you set it to 32, then your block width and height would be 1.

I'm not very sure what you are asking, but I hope above would help.

Good luck

avatar image ultrasilence · Apr 04, 2014 at 04:41 PM 0
Share

What I'm trying to accomplish is make is so say 2 32x32 blocks are directly side by side, no overlap regardless of screen size. But I'm unsure how to get the correct spacing of these 32x32 blocks when I'm dealing with the viewport which is 0...1 on the x and y.

avatar image koray1396 · Apr 04, 2014 at 05:18 PM 0
Share

forget the code.

  • on GUITexture, change transform position, rotation and scale to 0.

  • change pixel inset values as the following;

  • for the 1st, 100,100,32,32;

  • for the second, 132,100,32,32;

try this and you can understand how GUITexture works. also check this;

https://docs.unity3d.com/Documentation/Components/class-GuiTexture.html

avatar image ultrasilence · Apr 04, 2014 at 05:35 PM 0
Share

That's perfect! That pixel inset is exactly what I needed. Thanks for the heads up and the link!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ermarrero · Apr 04, 2014 at 04:02 PM

Hi, not tested but you can try something like this:

         public Rect guiRect;
         public float spacing;
         public Texture[] texture;
     
         
         void OnGUI()
         {
             for(int i = 0; i < texture.Length; i++)
             {
                 GUI.DrawTexture(new Rect(Screen.width / guiRect.x * (i * spacing), Screen.width / guiRect.y, texture[i].width + guiRect.width, texture[i].height + guiRect.height), texture[i],ScaleMode.StretchToFill);
             }
         }
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

22 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Can the Camera be Positioned on the Game Screen through code? 0 Answers

Detecting camera viewport on mouse down? 1 Answer

Hide GUI if it's out of camera Viewport? 1 Answer

what concept is used here ? 1 Answer

Scale canvas to camera's viewport rect? 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