unity3d - 如何在 RectTransform 中计算 sizeDelta?

标签 unity3d unityscript unity3d-ui

我编写了自定义布局所需的自定义内容拟合器。所以,我需要控制 RectTransform.sizeDelta当 anchor 不同时的属性,但我无法得到显示此值的属性。
我不需要 Unity3D API 引用,我读了它并没有得到任何东西,因为它只说:

The size of this RectTransform relative to the distances between the anchors. If the anchors are together, sizeDelta is the same as size. If the anchors are in each of the four corners of the parent, the sizeDelta is how much bigger or smaller the rectangle is compared to its parent.



谁能用普通语言解释一下这是什么意思?当 anchor 不同时,我如何手动计算它?

最佳答案

这个定义确实有点令人困惑。
sizeDelta ,基本上,返回 UI 元素的实际矩形与 anchor 定义的矩形之间的差异。

例如,给定一个 300x200 的矩形:

anchor 定在与矩形角相同的位置:sizeDelta是 (0,0)
enter image description here

矩形一半宽度处的左 anchor 或右 anchor :sizeDelta是 (150,0)
enter image description here
enter image description here

一个点中的所有四个 anchor :sizeDelta是 (300,200)(即:与矩形大小相同)
enter image description here
enter image description here

如您所见, anchor 定义的矩形的中心在哪里根本无关紧要,唯一重要的是元素矩形和 anchor 矩形的宽度和高度之间的差异。

在伪代码中,它是这样的:

sizeDelta.x = UIElementRectangle.width - AnchorsRectangle.width;
sizeDelta.y = UIElementRectangle.height - AnchorsRectangle.height;

因此,如果 UI 矩形的尺寸大于 anchor 的尺寸,sizeDelta为正,如果较小,则 sizeDelta是否定的。

关于unity3d - 如何在 RectTransform 中计算 sizeDelta?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44471568/

相关文章:

c# - 使用 DontDestroyOnLoad Unity C# 在场景之间保存对象

unity3d - 如何下载和导入在 Unity Collab 上与您共享的 Unity 项目

c++ - 将 Cubemap 转换为 Equirectangular Panorama

mysql - 使用 php 在 mysql 中更新记录为 1

c# - Unity3D UI,计算拖动项目的位置?

c# - Unity3D,3D 和 UI Raycaster 是否同时响应触摸?

unity-game-engine - 如何统一添加文字轮廓

android - 我是否需要在 unity 的同时更新 Unity 组件

java - 如何配置 Unity 2017.4 以面向 Android 并避免在 OSX 上构建失败?

android - UnityPlayerActivity getContext() 替换