UGUI 计算RectTransform的Bounds

论坛 期权论坛 脚本     
匿名网站用户   2020-12-19 16:49   15   0

NGUIMathf里有个calculateRelativeWidgeBounds(Transform content) 的函数 用来计算 content下(包括content)的bounds UGUI没有找到类似的功能 于是自己仿照这写了个UGUIMathf



using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class UGUIMathf : MonoBehaviour
{


    /// <summary>
    /// 计算content相对父级的bounds
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static Bounds calculateRelativeBounds(RectTransform content)
    {
        return calculateRelativeBounds(content.parent, content);   
    }


    /// <summary>
    /// 计算相对于relativeTo的bounds
    /// </summary>
    /// <param name="relativeTo"></param>
    /// <param name="content"></param>
    /// <returns></returns>
    public static Bounds calculateRelativeBounds(Transform relativeTo, RectTransform content)
    {
        Bounds bounds=calculateWorldBounds(content);
        
        if (relativeTo != null)
        {
            Vector3 temp;

            temp=relativeTo.InverseTransformPoint(bounds.size);
            temp.z = 0;
            bounds.size = temp; 
            temp=relativeTo.InverseTransformPoint(bounds.center);
            temp.z = 0;
            bounds.center = temp; 
            
            bounds.extents = bounds.size / 2f;
            bounds.min = bounds.center - bounds.extents;
            bounds.max = bounds.center + bounds.extents;
            
        }

        return bounds;
    }

    /// <summary>
    /// 计算content相对世界坐标的bounds
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static Bounds calculateWorldBounds(RectTransform content)
    {
        Bounds bounds=new Bounds();
        Vector2 min = new Vector2(float.MaxValue,float.MaxValue);
        Vector2 max = new Vector2(float.MinValue,float.MinValue);

        
            getRect(content, ref min, ref max);

        bounds.size = max - min;
        bounds.center = (max + min) / 2f;
        bounds.extents = bounds.size / 2f;
        bounds.min = bounds.center - bounds.extents;
        bounds.max = bounds.center + bounds.extents;

        return bounds;
    }

    /// <summary>
    /// 计算content的最大边框
    /// </summary>
    /// <param name="content"></param>
    /// <param name="min"></param>
    /// <param name="max"></param>
    public static void getRect(RectTransform content,ref Vector2  min,ref Vector2  max)
    {
        Vector3[] corners = new Vector3[4];
        content.GetWorldCorners(corners);
        min.x = Mathf.Min(new float[] { min.x, corners[0].x, corners[2].x });
        min.y = Mathf.Min(new float[] { min.y, corners[0].y, corners[2].y });
        max.x = Mathf.Max(new float[] { max.x, corners[0].x, corners[2].x });
        max.y = Mathf.Max(new float[] { max.y, corners[0].y, corners[2].y });

        for (int i = 0, imax = content.childCount; i < imax; i++)
        {
            getRect(content.GetChild(i)as RectTransform, ref min, ref max);
        }
        
    }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP