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!
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);
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.
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.
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!
You're welcome. I know that this whole system is very powerful, yet a little tricky to wrap our heads around at first sight.