Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by JMC17 · Aug 10, 2015 at 12:02 PM · c#scripting problemresizeanchor

Resizing button with script, wrong anchor!

So I'm trying to resize a button depending on the screen size and aspect ratio, which isn't really the problem. The problem is that in the editor I set my button's anchor to center but when resized with a script, it folds back unto the bottom left corner, which ultimately makes the button move away from its spot. In this example i'm using localscale but I tried with rectTransform.sizeDelta too and the same thing happened.

My question is, how do I get the button to resize on itself instead of bottom left? Does it have anything to do with the anchor?

Here's what I got right now :

         const float ratio = 1.777777777777778f;
         float w = Screen.width;
         float h = Screen.height;
         float dif = w / h;
         float CalculatedHeight = w / ratio ;
         float CalculatedWidth = h * ratio ;
         
         float CalculatedWidthB=w/1024f;
         float CalculatedHeightB=h/768f;
         Transform SBt= GameObject.Find ("StartButton").transform;
         Image SBi = GameObject.Find ("StartButton").GetComponent<Image> ();
         RectTransform t = GameObject.Find ("MainMenuOff").transform as RectTransform;

        if (dif<=1.777777777777778f) {
             SBi.color=Color.red;
             SBt.localScale=new Vector3(t.localScale.x*CalculatedWidthB,t.localScale.y*CalculatedHeightB,t.localScale.z);
             GameObject.Find ("StartButton").GetComponent<Image>().rectTransform.position=new Vector2(t.position.x-(SBdim.x/2f),((t.position.y)-(SBdim.y/2f)+(CalculatedHeight/2)*0.66f));

         } else {
             SBt.localScale=new Vector3(t.localScale.x*CalculatedWidthB,t.localScale.y*CalculatedHeightB,t.localScale.z);
             GameObject.Find ("StartButton").GetComponent<Image>().rectTransform.position=new Vector2(t.position.x-(SBdim.x/2f),((t.position.y)-(SBdim.y/2f)+(Screen.height/2)*0.66f));
             SBi.color=Color.green;
         }



I'm sorry if things don't make too much sense or if the code isn't clean, i've been tinkering with this script for awhile trying to debug problems and I just pasted stuff in there so that snippet would actually be somewhat complete.

Right now if I put that in update and remove the scaling, my button does stay on target no matter the aspect ratio but as soon as I re-enable the resize code, it goes off target pretty easily.

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 allenallenallen · Aug 10, 2015 at 12:06 PM

Set your RectTransform to this:

alt text

You can copy the values for the Anchors and Pivot but you can also click on the anchor icon and press ALT + SHIFT and click on the center choice to make this happen.

Comment
Add comment · Show 2 · 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 JMC17 · Aug 10, 2015 at 12:15 PM 0
Share

Thank you, I think it's working but my script is a mess, I have to spend some time fixing it then I'll know if it has truly resolved my problem. I accepted this as an answer in the meantime!

avatar image JMC17 · Aug 10, 2015 at 12:23 PM 0
Share

It's working flawlessly! Since the pivot point moved, I just had to remove a variable(SBdim.x/2f) from the position's equation and now it's spot on! Thank you so much.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Set default length for an array of elements of a custom class in inspector 0 Answers

How to rotate a projectile to face the player in 2D? 1 Answer

How do I make a variable of type C# class? 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