如何锁定 RectTransform 的字段
How to Lock RectTransform's fields
我正在创建自定义布局组,我想控制 child Objects 上的 RectTransform
。我想锁定 child 的 RectTransform
上的一些字段,就像在使用 canvas 或 Unity 的水平或垂直组时一样,这样它就无法修改。
我需要同样的效果。您可以在 child 的 RectTransform
之上看到此消息:一些值由 HorizontalLayoutGroup
驱动
我找到了一半:
添加 [ExecuteInEditMode]
然后:
public void Update()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
/* Todo => update child's positions here. */
}
#endif
}
还有其他想法吗?
这是通过 DrivenRectTransformTracker
API.
完成的
来自doc:
Driving a RectTransform means that the values of the driven
RectTransform are controlled by that component. These driven values
cannot be edited in the Inspector (they are shown as disabled). They
also won't be saved when saving a scene, which prevents undesired
scene file changes.
Whenever the component is changing values of driven RectTransforms, it
should first call the Clear method and then use the Add method to add
all RectTransforms it is driving. The Clear method should also be
called in the OnDisable callback of the component.
文档中没有示例,但下面是如何使用它:
public RectTransform targetRC;
UnityEngine.Object driver;
void Start()
{
DrivenRectTransformTracker dt = new DrivenRectTransformTracker();
dt.Clear();
//Object to drive the transform
driver = this;
dt.Add(driver, targetRC, DrivenTransformProperties.All);
}
链接到 targetRC
变量的 RectTransform
现在将被锁定,无法从编辑器中修改。它现在应该显示类似 "Some values are driven by another object" 的内容。您可以使用 DrivenTransformProperties
指定要锁定的变量。
这是执行此代码后的样子:
我正在创建自定义布局组,我想控制 child Objects 上的 RectTransform
。我想锁定 child 的 RectTransform
上的一些字段,就像在使用 canvas 或 Unity 的水平或垂直组时一样,这样它就无法修改。
我需要同样的效果。您可以在 child 的 RectTransform
之上看到此消息:一些值由 HorizontalLayoutGroup
我找到了一半:
添加 [ExecuteInEditMode]
然后:
public void Update()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
/* Todo => update child's positions here. */
}
#endif
}
还有其他想法吗?
这是通过 DrivenRectTransformTracker
API.
来自doc:
Driving a RectTransform means that the values of the driven RectTransform are controlled by that component. These driven values cannot be edited in the Inspector (they are shown as disabled). They also won't be saved when saving a scene, which prevents undesired scene file changes.
Whenever the component is changing values of driven RectTransforms, it should first call the Clear method and then use the Add method to add all RectTransforms it is driving. The Clear method should also be called in the OnDisable callback of the component.
文档中没有示例,但下面是如何使用它:
public RectTransform targetRC;
UnityEngine.Object driver;
void Start()
{
DrivenRectTransformTracker dt = new DrivenRectTransformTracker();
dt.Clear();
//Object to drive the transform
driver = this;
dt.Add(driver, targetRC, DrivenTransformProperties.All);
}
链接到 targetRC
变量的 RectTransform
现在将被锁定,无法从编辑器中修改。它现在应该显示类似 "Some values are driven by another object" 的内容。您可以使用 DrivenTransformProperties
指定要锁定的变量。
这是执行此代码后的样子: