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 /
  • Help Room /
avatar image
0
Question by BartiriX · Dec 26, 2016 at 09:43 PM · c#canvasnewbieanchor

How to make Y-Anchor Max scale?

Hi everyone,

Today I programmed a little feature where if you clicked the inventory button, it would slide up if it was down, and down if it was up. I have a bit of a problem here, though.

The Y-Anchor Max has to scale with the screen.localresolution.height, but I don't know how to do this in my code. I tried loads of variables and numbers but I just couldn't get it right. Hopefully someone can help me out with this one!

Here is my code, by the way: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class inventorySlider : MonoBehaviour {
     Animator anim; 
     public bool slideUp;
 
     void Start(){
         anim = GetComponent<Animator> ();
         slideUp = false;
     }
 
     void OnMouseOver(){
         if (Input.GetKeyDown (KeyCode.Mouse0) && slideUp == false) {
             anim.SetBool ("slideBoolUp", true);
             slideUp = true;
         } else if (Input.GetKeyDown (KeyCode.Mouse0) && slideUp == true) {
             anim.SetBool ("slideBoolDown", true);
             slideUp = false;
         }
     }
 
     void SetSlideUp(){
         anim.SetBool ("slideBoolUp", false);
         gameObject.GetComponent<RectTransform> ().anchorMax = new Vector2 (0, .73f); //This one is the problem. Idk what value the .73f should be to have it scale.
         gameObject.GetComponent<RectTransform> ().anchoredPosition = new Vector2 (10, -90);
         gameObject.GetComponent<RectTransform> ().SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, 240);
     }
 
     void SetSlideDown(){
         anim.SetBool ("slideBoolDown", false);
         gameObject.GetComponent<RectTransform> ().anchorMax = new Vector2 (0, 0);
         gameObject.GetComponent<RectTransform> ().anchoredPosition = new Vector2 (10, -100 * Screen.height);
         gameObject.GetComponent<RectTransform> ().SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, 240);
     }
 }
 

If more information is required, I will gladly provide it. Thanks already, even if all you did was read!

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 TBruce · Dec 26, 2016 at 10:01 PM 0
Share

If you want to change the scale the changing the anchor$$anonymous$$ax value is not what you want to change. You need to change the localScale value like so

 gameObject.transform.localScale = new Vector3(someXValue, someYValue, 1);
avatar image BartiriX TBruce · Dec 26, 2016 at 10:15 PM 0
Share

Hey there, Thanks for your answer, but I don't want the object to scale. I only want the anchor to scale, so that the object's locations stay smooth regardless of what resolution the player is playing it on.

avatar image UnityCoach BartiriX · Dec 26, 2016 at 10:18 PM 0
Share

Then don't use the anchors, they're meant to provide resolution independent positioning. You can use the x/y local offset position ins$$anonymous$$d, given in pixels.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BartiriX · Dec 27, 2016 at 03:33 PM

Thanks to @UnityCoach for helping me out! It turned out that what I was doing wrong was that I was only changing the max-anchor. Now that I also added the min-anchor for the same axis, it seems to work properly. A great many thanks to you sir!

Comment
Add comment · Show 1 · 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 UnityCoach · Dec 27, 2016 at 11:11 PM 0
Share

You're welcome. I know that this whole system is very powerful, yet a little tricky to wrap our heads around at first sight.

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

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

Related Questions

Use a canvas prefab multiple times at the same time 0 Answers

Problem with Awake 2 Answers

Anchor point changes automatically on play 0 Answers

Do you need to have specific script types for different mobile platforms? 1 Answer

Drag and drop a plane coping terrain 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