- Home /
How to listen for UI Resize Events
I hear that the new 4.6 UI's layout is event driven.
I am having great difficulty finding the event to listen to / handler to interface for listening when a RectTransform changes.
Answer by Malkyne · Aug 09, 2015 at 09:40 AM
For future reference, there is a function that seems to work pretty well for this:
UI.Graphic.OnRectTransformDimensionsChange
I've used it to do responsive stacking columns and other neat tricks.
Thanks for the link it help to find my way around :) Looks like the UI events changed a bit in 5.x. The functionally has caped the same This are the new corresponding documentation links. http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTransformParentChanged.html http://docs.unity3d.com/ScriptReference/EventSystems.UIBehaviour.OnTransformParentChanged.html
@fff$$anonymous$$alzbier: Those new functions are pretty useful -- but not for watching for size changes.
I actually linked the wrong version of that function. I should have linked the one on UIBehaviour, ins$$anonymous$$d:
I just tested this in Unity 5.2.2f1, and it still works just fine.
Here's the code I used in my test:
using UnityEngine;
using UnityEngine.UI;
namespace FeelFreeToBorrowThisCode
{
[RequireComponent(typeof(HorizontalLayoutGroup))]
public class ResponsiveColumns : $$anonymous$$onoBehaviour
{
RectTransform _leftSide;
RectTransform _rightSide;
RectTransform _column2;
bool _vertical = true;
void Start()
{
RectTransform rtrans = (RectTransform)transform;
_leftSide = (RectTransform)rtrans.GetChild(0);
_rightSide = (RectTransform)rtrans.GetChild(1);
_column2 = (RectTransform)_leftSide.GetChild(1);
UpdateLayout();
}
void OnRectTransformDimensionsChange()
{
UpdateLayout();
}
void UpdateLayout()
{
if (_leftSide == null) return;
if (Screen.width > Screen.height)
{
if (_vertical)
{
_column2.SetParent(_rightSide);
_rightSide.gameObject.SetActive(true);
_vertical = false;
}
}
else if (!_vertical)
{
_column2.SetParent(_leftSide);
_rightSide.gameObject.SetActive(false);
_vertical = true;
}
}
}
}
It changes the parent of a column, based on whether the device is in portrait or landscape mode.
If you use OnRectTransformDimensionsChange, make sure to enable Pixel Perfect on your Canvas. Otherwise, OnRectTransformDimensionsChange might not detect changes to the screen size.
An example: OnRectTransformDimensionsChange doesn't get called when I play in Unity Editor and I toggle between "1280x720 Portrait" and "1280x720 Landscape" if Pixel Perfect is disabled on Canvas.
Answer by KpjComp · Oct 11, 2014 at 08:57 PM
Hi there is a tranform.hasChanged, but this seems to get called constantly..
But this script seems to work, btw. used localToWorldMatrix so that it would detect movement if attached as a child of another object that says rotates, ..
using UnityEngine;
using System.Collections;
public class TransFormChange : MonoBehaviour {
private Matrix4x4 old;
// Use this for initialization
void Start () {
old = transform.localToWorldMatrix;
}
// Update is called once per frame
void Update () {
//if (transform.hasChanged)
if (!old.Equals(transform.localToWorldMatrix))
{
Debug.Log (transform.position);
old = transform.localToWorldMatrix;
}
}
}
Answer by MrMatthias · Jun 06, 2019 at 04:39 PM
you have to set it to false again.
void Update () {
if (transform.hasChanged)
{
Debug.Log (transform.position);
transform.hasChanged = false;
}
}