Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 syjgin · Apr 14, 2016 at 06:45 AM · ui2d game

Place UI objects on the top and bottom of screen object(2D sprite)

Hello! I have two panels, which is currently stretched from one edge of screen to another. If screen height is bigger than width (portrait view in smartphones), it's right, but in opposite situation height of panel dramatically grows(canvas is in "Scale with screen size" mode). I'm trying to get coordinates of my main game field sprite and convert it to screen space (no problem with that), and place panels above the max y of field and under the min y respectively. For this task I must know current size of panel sprites. But logically it size equals full screen size - _bottomPanel.rectTransform.rect.width returns full screen height. I know sprite sizes for panels and canvas reference resolution. So, currently I'm trying to place it directly behind screen, but result still depends of resolution. How can I determine UI Image screen size?

     [SerializeField]
     Image _bottomPanel;
 
     [SerializeField]
     Image _topPanel;
 
     [SerializeField]
     PolygonCollider2D _floor;
 
     [SerializeField]
     Camera _camera;
 
     // Use this for initialization
     void Start () {
         float aspect = ((float)Screen.width / (float)Screen.height) - 1;
         if(aspect > 0) {
 
             float scaleCoef = (float)Screen.width / 1280.0f;
 
             float scaledTopHeight = _topPanel.rectTransform.rect.height*scaleCoef;
             float scaledBottomHeight = _bottomPanel.rectTransform.rect.height*scaleCoef;//trying to get ui element resolution here
 
             float floorMaxPosition = Screen.height - _camera.WorldToScreenPoint (_floor.bounds.max).y;
             float floorMinPosition = _camera.WorldToScreenPoint (_floor.bounds.min).y;
 
             Vector3 oldBottomPosition = _bottomPanel.rectTransform.position;
             Vector3 oldTopPosition = _topPanel.rectTransform.position;
             _bottomPanel.rectTransform.position = new Vector3 (oldBottomPosition.x,oldBottomPosition.y - scaledBottomHeight/* + floorMinPosition*/, oldBottomPosition.z);
             _topPanel.rectTransform.position = new Vector3 (oldTopPosition.x, oldTopPosition.y + scaledTopHeight/* + floorMaxPosition*/, oldTopPosition.z);
         }
     }
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Make only certain UI elements block raycast/touch 3 Answers

Is making a 2d tile based game all in UI a bad solution or is it viable? 1 Answer

why is my ui being different compare to the scene after build? 0 Answers

Second camera does not show UI Image element (but does show sprite) 0 Answers

How to make a map switching system like Corpse Party Book of Shadows? 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