Unity UGUI锚点快速定位(自适应)

放在Editor文件夹中的编辑器的工具类,用于GUI画布内组件。让锚点快速定位到组件的四个顶点

using UnityEngine;
using System.Collections;
using UnityEditor;

/************************************************************
  FileName: AnchorsAdapt.cs
  Author:菜菜       Version :1.0          Date: 2019.3.7
  Description:放在Editor文件夹中的编辑器的工具类,用于GUI画布内组件。让锚点快速定位到组件的四个顶点
************************************************************/

public class AnchorsAdapt
{
    [MenuItem("Tools/AnchorsAdapt")]
    private static void SelectionM()
    {
        GameObject[] gos = Selection.gameObjects;
        for (int i = 0; i < gos.Length; i++)
        {
            if (gos[i].GetComponent<RectTransform>() == null)
                continue;
            Adapt(gos[i]);
        }
    }

    private static void Adapt(GameObject go)
    {
        //位置信息
        Vector3 partentPos = go.transform.parent.position;
        Vector3 localPos = go.transform.position;
        //------获取rectTransform----
        RectTransform partentRect = go.transform.parent.GetComponent<RectTransform>();
        RectTransform localRect = go.GetComponent<RectTransform>();
        float partentWidth = partentRect.rect.width;
        float partentHeight = partentRect.rect.height;
        float localWidth = localRect.rect.width * 0.5f;
        float localHeight = localRect.rect.height * 0.5f;
        //---------位移差------
        float offX = localPos.x - partentPos.x;
        float offY = localPos.y - partentPos.y;

        float rateW = offX / partentWidth;
        float rateH = offY / partentHeight;
        localRect.anchorMax = localRect.anchorMin = new Vector2(0.5f + rateW, 0.5f + rateH);
        localRect.anchoredPosition = Vector2.zero;

        partentHeight = partentHeight * 0.5f;
        partentWidth = partentWidth * 0.5f;
        float rateX = (localWidth / partentWidth) * 0.5f;
        float rateY = (localHeight / partentHeight) * 0.5f;
        localRect.anchorMax = new Vector2(localRect.anchorMax.x + rateX, localRect.anchorMax.y + rateY);
        localRect.anchorMin = new Vector2(localRect.anchorMin.x - rateX, localRect.anchorMin.y - rateY);
        localRect.offsetMax = localRect.offsetMin = Vector2.zero;
    }
}

效果:
创建一个UI,选择AnchorsAdapt并点击效果如图

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值