获取对象在网格布局中的位置
Get position of object in Grid Layout
如何获取对象在网格布局中的实际位置?在我当前的迭代中,symbolPosition 一直返回 0,0,0.
public void OnPointerClick (PointerEventData eventData)
{
// Instantiate an object on click and parent to grid
symbolCharacter = Instantiate(Resources.Load ("Prefabs/Symbols/SymbolImage1")) as GameObject;
symbolCharacter.transform.SetParent(GameObject.FindGameObjectWithTag("MessagePanel").transform);
// Set scale of all objects added
symbolCharacter.transform.localScale = symbolScale;
// Find position of objects in grid
symbolPosition = symbolCharacter.transform.position;
Debug.Log(symbolPosition);
}
直到下一帧才会更新位置值。为了允许您进行大量更改,而不是每个更改都导致对这些元素进行整个重新计算,它存储需要计算的项目,然后在下一帧开始时更新它们。
所以一个选择是使用协程等待一帧然后获取位置
public void OnPointerClick (PointerEventData eventData)
{
// Instantiate an object on click and parent to grid
symbolCharacter = Instantiate(Resources.Load ("Prefabs/Symbols/SymbolImage1")) as GameObject;
symbolCharacter.transform.SetParent(GameObject.FindGameObjectWithTag("MessagePanel").transform);
// Set scale of all objects added
symbolCharacter.transform.localScale = symbolScale;
StartCoroutine(CoWaitForPosition());
}
IEnumerator CoWaitForPosition()
{
yield return new WaitForEndOfFrame();
// Find position of objects in grid
symbolPosition = symbolCharacter.transform.position;
Debug.Log(symbolPosition);
}
几年前这个问题 asked/answered 时,API 可能没有这个问题,但这似乎可以强制 GridLayoutGroup 将子对象放置在子对象所在的框架上被添加,从而消除了对协同程序的需求。
for(int i = 0; i < 25; i++){
GameObject gridCell = Instantiate(_gridCellPrefab);
gridCell.transform.SetParent(_gridLayoutTransform,false);
}
_gridLayoutGroup.CalculateLayoutInputHorizontal();
_gridLayoutGroup.CalculateLayoutInputVertical();
_gridLayoutGroup.SetLayoutHorizontal();
_gridLayoutGroup.SetLayoutVertical();
从这里可以得到'gridCells'同框的位置。
还有一个功能,看起来应该可以实现这个目的,但它对我不起作用。 API 对内部发生的事情非常安静:
LayoutRebuilder.ForceRebuildLayoutImmediate(_gridLayoutTransform);
如何获取对象在网格布局中的实际位置?在我当前的迭代中,symbolPosition 一直返回 0,0,0.
public void OnPointerClick (PointerEventData eventData)
{
// Instantiate an object on click and parent to grid
symbolCharacter = Instantiate(Resources.Load ("Prefabs/Symbols/SymbolImage1")) as GameObject;
symbolCharacter.transform.SetParent(GameObject.FindGameObjectWithTag("MessagePanel").transform);
// Set scale of all objects added
symbolCharacter.transform.localScale = symbolScale;
// Find position of objects in grid
symbolPosition = symbolCharacter.transform.position;
Debug.Log(symbolPosition);
}
直到下一帧才会更新位置值。为了允许您进行大量更改,而不是每个更改都导致对这些元素进行整个重新计算,它存储需要计算的项目,然后在下一帧开始时更新它们。
所以一个选择是使用协程等待一帧然后获取位置
public void OnPointerClick (PointerEventData eventData)
{
// Instantiate an object on click and parent to grid
symbolCharacter = Instantiate(Resources.Load ("Prefabs/Symbols/SymbolImage1")) as GameObject;
symbolCharacter.transform.SetParent(GameObject.FindGameObjectWithTag("MessagePanel").transform);
// Set scale of all objects added
symbolCharacter.transform.localScale = symbolScale;
StartCoroutine(CoWaitForPosition());
}
IEnumerator CoWaitForPosition()
{
yield return new WaitForEndOfFrame();
// Find position of objects in grid
symbolPosition = symbolCharacter.transform.position;
Debug.Log(symbolPosition);
}
几年前这个问题 asked/answered 时,API 可能没有这个问题,但这似乎可以强制 GridLayoutGroup 将子对象放置在子对象所在的框架上被添加,从而消除了对协同程序的需求。
for(int i = 0; i < 25; i++){
GameObject gridCell = Instantiate(_gridCellPrefab);
gridCell.transform.SetParent(_gridLayoutTransform,false);
}
_gridLayoutGroup.CalculateLayoutInputHorizontal();
_gridLayoutGroup.CalculateLayoutInputVertical();
_gridLayoutGroup.SetLayoutHorizontal();
_gridLayoutGroup.SetLayoutVertical();
从这里可以得到'gridCells'同框的位置。
还有一个功能,看起来应该可以实现这个目的,但它对我不起作用。 API 对内部发生的事情非常安静:
LayoutRebuilder.ForceRebuildLayoutImmediate(_gridLayoutTransform);