0

In the unity editor, it is possible to set the position based on anchor presets such as :

enter image description here

My goal is to be able to do this via code. The final result should position some buttons inside a parent panel element (one at top left, top right, center left, center right, bottom left, bottom right).

Now, I have managed to succeed partially by implementing a solution where I set anchorMin and anchorMax on the RectTransform as suggested in this post. Such as :

public static void SetAnchor(this RectTransform source, AnchorPresets allign)
    {
        source.anchoredPosition = new Vector3(0, 0, 0);

        switch (allign)
        {
            case (AnchorPresets.TopLeft):
            {
                source.anchorMin = new Vector2(0, 1);
                source.anchorMax = new Vector2(0, 1);
                break;
            }
    ...

However, when I run the code, I get something like this:

enter image description here

When I use the editor, everything is smooth as I want it to be : enter image description here

I've researched the issue, but could only find a partial solution to the problem in these posts :

Any idea on how to properly solve this would be greatly appreciated. :)

Rose
  • 2,619
  • 4
  • 20
  • 27
  • 1
    I guess that you just need to calculate the height and width of the object. In Order to compensate for pivot of the Object (assuming that you have pivot of (0.5, 0.5) set to all of your object). – Ghost The Punisher Aug 04 '20 at 03:53

2 Answers2

4

I just made some code for this.

Result.gif

It's just test code. So it made very roughly but I think you can get the point. This code work properly only when AnchorMax == AnchorMin

public class TEST : MonoBehaviour
{
    [SerializeField] RectTransform source;
    
    void Update() {    
        Vector2 shiftDirection = new Vector2(0.5f - source.anchorMax.x, 0.5f - source.anchorMax.y);
        source.anchoredPosition = shiftDirection * source.rect.size;    
    }
}

But, Why don't you use GridLayout rather than this? check below link https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-GridLayoutGroup.html

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
  • Using `GridLayout` is a big no-no and must be used in rarest of cases - they are very performance-heavy. [link](https://create.unity3d.com/Unity-UI-optimization-tips) – tsvedas Aug 04 '20 at 07:23
  • @WonJae Choi Your solution works very well for me. :) Haven't checked `GridLayout` yet, but since your solution works like a charm, I dont think i will need it for now. Not quite sure what the performance impact of using it could be as elTomis mentionned. Thanks a lot for taking the time. – Rose Aug 04 '20 at 17:18
0

It is because your pivot is set to Vector2(0.5f, 0.5f) (center of the UI element) by default. To fix this issue, you could do something like:

switch (allign)
{
    case (AnchorPresets.TopLeft):
    {
        source.anchorMin = new Vector2(0, 1);
        source.anchorMax = new Vector2(0, 1);
        source.pivot = new Vector2(0, 1);
        break;
    }

    // Just an additional example
    case (AnchorPresets.BottomRight):
    {
        var anchorPoint = new Vector2(1, 0);

        source.anchorMin = anchorPoint;
        source.anchorMax = anchorPoint;
        source.pivot = anchorPoint;
        source.sizeDelta = Vector2.zero; // Not sure if this is needed
    }
    ...
}
tsvedas
  • 1,049
  • 7
  • 18