This is my first post on here, so excuse me if my formatting is bad.
I have a Vertical Layout Group encompassing a few buttons.
I’m using keys to select (Input Action Asset maps to Event System). In the Build, making a selection causes the button positions to momentarily reset but then collapse again. In the Editor, it functions correctly.
In the Build:
In the Editor:
On each button (all based on the same prefab) is a script that moves its position slightly to the right or left when selected.
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class MenuButtonMove : MonoBehaviour, ISelectHandler, IDeselectHandler
{
public float moveAmount = 100.0f;
public float speed = 100.0f;
public bool isMoveRight = false; // used to control whether button moves left or right on select
private Vector3 leftPos;
private Vector3 rightPos;
private Vector3 currPos;
private void Start()
{
if (isMoveRight)
{
rightPos = transform.position;
leftPos = transform.position + Vector3.left * moveAmount;
currPos = rightPos;
} else // !isMoveRight
{
leftPos = transform.position;
rightPos = transform.position + Vector3.right * moveAmount;
currPos = leftPos;
}
}
private void Update()
{
transform.position = Vector3.MoveTowards(transform.position, currPos, speed * Time.deltaTime);
}
public void OnSelect(BaseEventData eventData)
{
if (isMoveRight) { currPos = leftPos; }
else { currPos = rightPos; }
}
public void OnDeselect(BaseEventData eventData)
{
if (isMoveRight) { currPos = rightPos; }
else { currPos = leftPos; }
}
}
When the script is deactivated or the vertical layout group deactivated, the problem disappears, so it has something to do with one of these two things.
I’m gonna guess that the anchors on the VerticalLayoutGroup’s RectTransform are not correct and on the build, given different screen dimensions, something degenerate is happening to the overall size, causing it to not be able to flow properly.
Easy enough to prove/disprove: make a custom editor Game resolution that matches what you used in the build and test!
Here are some notes on UI Anchoring, Scaling, CanvasScaler, etc:
Usually you need to choose a suitable ScaleMode and MatchMode in the Canvas Scaler and stick with it 100%. Generally if you change those settings you will often need to redo your UI entirely.
I also use this CanvasScalerOrientationDriver utility to make sharing UI for Landscape / Portrait easier. Read what it does carefully.
I have tried many different scales and resolutions. The problem still occurs.
I don’t think the canvas scaling is the culprit. I could be wrong, but it seems to be a parent-child transform inheritance issue. I know that the anchor of the Vertical Layout Group overwrites the anchor of the child objects. Every object in the Vertical Layout Group drifts back to the anchor of the parent object. Changing the anchor of the layout group confirmed this, as all buttons drifted to it no matter where it was. Then, every button in the group reacts whenever any button is selected, so it seems like any change to one child’s transform affects the entire group.
This being the case, what can I do to make sure I am only changing the transform local to each button?