我正在处理一个组件,我需要在 Start 函数中正确定位 HorizontalLayoutGroup 的子项。
我可以通过执行以下操作强制 HorizontalLayoutGroup 重建:
public HorizontalLayoutGroup horizLayoutGroup;
public RectTransform exampleChild;
private void Start()
{
horizLayoutGroup.CalculateLayoutInputHorizontal();
horizLayoutGroup.CalculateLayoutInputVertical();
horizLayoutGroup.SetLayoutHorizontal();
horizLayoutGroup.SetLayoutVertical();
Debug.Log(exampleChild.anchoredPosition);
}
Run Code Online (Sandbox Code Playgroud)
但这样做似乎很奇怪:
public RectTransform horizRectTransform;
public RectTransform exampleChild;
private void Start()
{
LayoutRebuilder.ForceRebuildLayoutImmediate(horizRectTransform);
Debug.Log(exampleChild.anchoredPosition);
}
Run Code Online (Sandbox Code Playgroud)
不起作用,因为据我从源代码中可以看出 LayoutRebuilder.ForceRebuildLayoutImmediate 正在调用相同的函数:
public static void ForceRebuildLayoutImmediate(RectTransform layoutRoot)
{
var rebuilder = s_Rebuilders.Get();
rebuilder.Initialize(layoutRoot);
rebuilder.Rebuild(CanvasUpdate.Layout);
s_Rebuilders.Release(rebuilder);
}
public void Rebuild(CanvasUpdate executing)
{
switch (executing)
{
case CanvasUpdate.Layout:
// It's unfortunate that we'll perform the same GetComponents querys for the tree 2 times, …Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,我的主管希望我能够通过注释处理器获取枚举常量的值。例如,来自以下枚举定义:
public enum Animal {
LION(5),
GIRAFFE(7),
ELEPHANT(2),
private int value;
Animal(int value) {
this.value = value;
}
public int Value() {
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
他们要我编译一个 [5, 7, 2] 的数组。
请注意,因为我在注释处理器中工作,所以我使用基于元素的反射(而不是基于类的反射)。
我对VariableElement文档的阅读使我相信这是不可能的。
请注意,并非所有最终字段都具有常量值。特别是,枚举常量不被视为编译时常量。
有谁知道有什么方法可以让它工作吗?
感谢您抽出时间来阅读!——贝卡