统一:父母的适合大小孩子的大小

问题描述 投票:0回答:3

我有在具有多个对象为儿童帆布空。随着比赛的进行更多的孩子加入到父。我希望父的大小适合它的孩子。

我已经读了很多关于这个话题,但我每次跨越ContentSizeFitter,来到时间,这可惜不为我工作,因为它需要一个布局组,我没有,因为孩子们没有一个订单,并疯狂放置。

我使用统一2018.3.4f1个人。

没有大小父:

Example

大小父(红色:新型,绿色:旧):

Example 2

结构体:

Canvas
 |- Empty
     |- Child 1
     |- Child 2
     |- Child X
unity3d parent-child scale
3个回答
1
投票

我写了一个小的一段代码,它的工作呢,还是我想要的方式,这是足以让我:

using UnityEngine;

public class SizeFitter : MonoBehaviour {
    public void CheckForChanges() {
        RectTransform children = transform.GetComponentInChildren<RectTransform>();

        float min_x, max_x, min_y, max_y;
        min_x = max_x = transform.localPosition.x;
        min_y = max_y = transform.localPosition.y;

        foreach (RectTransform child in children) {
            Vector2 scale = child.sizeDelta;
            float temp_min_x, temp_max_x, temp_min_y, temp_max_y;

            temp_min_x = child.localPosition.x - (scale.x / 2);
            temp_max_x = child.localPosition.x + (scale.x / 2);
            temp_min_y = child.localPosition.y - (scale.y / 2);
            temp_max_y = child.localPosition.y + (scale.y / 2);

            if (temp_min_x < min_x)
                min_x = temp_min_x;
            if (temp_max_x > max_x)
                max_x = temp_max_x;

            if (temp_min_y < min_y)
                min_y = temp_min_y;
            if (temp_max_y > max_y)
                max_y = temp_max_y;
        }

        GetComponent<RectTransform>().sizeDelta = new Vector2(max_x - min_x, max_y - min_y);
    }
}

0
投票

像这样的东西应该工作。它通过循环您所选择的对象的所有孩子,然后找到那些对象的渲染边界和为它去更新最小值和最大值。在启动()函数结束,变量最小值和最大值保持坐标定义边界框的角落。

public class NewBehaviourScript : MonoBehaviour
{
    void Start ()
    {
        Vector2 min = new Vector2 (0, 0);
        Vector2 max = new Vector2 (0, 0);

        GetParentBounds (gameObject, out min, out max);
        Debug.Log (min);
        Debug.Log (max);

    }

    void GetParentBounds (GameObject GO, out Vector2 pMin, out Vector2 pMax)
    {
        Transform[] ts = GO.GetComponentsInChildren<Transform> ();

        Vector2 parentMin = new Vector2 (float.MaxValue, float.MaxValue);
        Vector2 parentMax = new Vector2 (float.MinValue, float.MinValue);

        foreach (Transform t in ts) {
            Renderer rend = t.GetComponent<Renderer> ();
            if (rend != null) {
                Vector2 min = rend.bounds.min;
                Vector2 max = rend.bounds.max;
                Debug.Log (string.Format ("[{0}, {1}], [{2}, {3}]", min.x, min.y, max.x, max.y));
                parentMin.x = Mathf.Min (parentMin.x, min.x);
                parentMin.y = Mathf.Min (parentMin.y, min.y);
                parentMax.x = Mathf.Max (parentMax.x, max.x);
                parentMax.y = Mathf.Max (parentMax.y, max.y);
            }
        }

        pMin = parentMin;
        pMax = parentMax;
    }
}

0
投票

我认为你可以找到leftiest边境和边境rightiest然后添加值的差值作为sizeDeltaRectTransform的。

这应该可以解决大小(顶部和底部的Y的重复)。

对于位置你可以得到leftiest边框和上边框左上角的qazxsw POI,比你设置矢量qazxsw POI父Vector2

需要注意的是父母的支点必须在左上角这个工作。

此代码仅走近一些,但还不够好。

anchoredPosition

GIF HERE

RectTransform

© www.soinside.com 2019 - 2024. All rights reserved.