美文网首页
Unity UGUI使所选物体锚点吸附自身顶点

Unity UGUI使所选物体锚点吸附自身顶点

作者: runinnn | 来源:发表于2020-08-19 21:49 被阅读0次

在Unity编辑器中实现:根据UIGUI物体自身的位置和大小,以及其父物体的大小,和UGUI物体自身的中心点,计算物体自身的锚点实现选中的UGUI物体,让锚点快速定位到组件的四个顶点。

(计算自身相对于父物体的锚点(也就等于是自身四个顶点所在位置相对于父物体大小的百分比))

(当然计算锚点的方法也可以在运行状态下使用,大致可用于需要动态加载的UI,设置其锚点,使其可以跟随父物体的大小)

实现的逻辑(详细代码):

#

using System.Collections;

using System.Collections.Generic;

using System.Runtime.InteropServices;

using UnityEditor;

using UnityEngine;

public class CalAnchorsEditor : EditorWindow

{

const string TitleName = "UI设置/设置锚点适应自身";

/// <summary>

/// 是否设置子节点

/// </summary>

bool IsSetChildTransform = false;

[MenuItem(TitleName)]

static void InitWindow()

{

Rect wr = new Rect(0, 0, 500, 500);

var windows = EditorWindow.GetWindowWithRect(typeof(CalAnchorsEditor), wr, true, "设置锚点适应自身");

windows.Show();

}

void OnGUI()

{

IsSetChildTransform = GUILayout.Toggle(IsSetChildTransform, "是否设置子节点", GUILayout.Width(200));

EditorGUILayout.LabelField("选择的物体数量", Selection.gameObjects.Length.ToString());

if (GUILayout.Button("保存", GUILayout.Width(200)))

{

SetSelectGameobjectItemAnchors();

}

}

/// <summary>

/// 设置锚点

/// </summary>

private void SetSelectGameobjectItemAnchors()

    {

var uiItems = Selection.gameObjects;

        for (int i = 0; i < uiItems.Length; i++)

        {

var item = uiItems[i];

var rect = item.GetComponent<RectTransform>();

CalAnchors(rect);

            if (IsSetChildTransform)

            {

var rects = item.GetComponentsInChildren<RectTransform>();

                for (int j = 0; j < rects.Length; j++)

                {

var child = rects[j];

CalAnchors(child);

}

}

}

}

/// <summary>

/// 计算锚点

/// 计算自身相对于父物体的锚点(也就是自身四个顶点所在位置相对于父物体自身大小的百分比)

/// </summary>

private void CalAnchors(Transform transform)

    {

var parent = transform.parent;

RectTransform parentRect = null;

        if (parent)

        {

parentRect = parent.GetComponent<RectTransform>();

}

RectTransform transformRect = transform.GetComponent<RectTransform>();

Vector2 pivot = transformRect.pivot;

        if (parentRect)

        {

float parentWidth = parentRect.rect.width;

float parentHeight = parentRect.rect.height;

float selfWidth = transformRect.rect.width;

float selfHeight= transformRect.rect.height;

float x = transformRect.localPosition.x;

float y= transformRect.localPosition.y;

float anchorsMinx = (parentWidth / 2 + x - (selfWidth * pivot.x)) / parentWidth;//

float anchorsMaxx = (parentWidth / 2 + x + (selfWidth * (1 - pivot.x))) / parentWidth;

float anchorsMiny = (parentHeight / 2 + y - (selfHeight * pivot.y)) / parentHeight;

float anchorsMaxy = (parentHeight / 2 + y + (selfHeight * (1 - pivot.y))) / parentHeight;

transformRect.anchorMin = new Vector2(anchorsMinx, anchorsMiny);

transformRect.anchorMax = new Vector2(anchorsMaxx, anchorsMaxy);

transformRect.offsetMin = Vector2.zero;

transformRect.offsetMax = Vector2.zero;

}

}

}

#

相关文章

网友评论

      本文标题:Unity UGUI使所选物体锚点吸附自身顶点

      本文链接:https://www.haomeiwen.com/subject/hhgdjktx.html