Skip to content

Instantly share code, notes, and snippets.

@LuviKunG
Created February 7, 2024 17:36
Show Gist options
  • Save LuviKunG/fb994b3e7980d3b25007ae2ec7f4c0a3 to your computer and use it in GitHub Desktop.
Save LuviKunG/fb994b3e7980d3b25007ae2ec7f4c0a3 to your computer and use it in GitHub Desktop.
User Interface Behaviour. Inherit from this class to create a User Interface Behaviour which will have a reference to the RectTransform.
namespace UnityEngine
{
using EventSystems;
/// <summary>
/// User Interface Behaviour.
/// Inherit from this class to create a User Interface Behaviour
/// which will have a reference to the <see cref="RectTransform"/>.
/// </summary>
public abstract class UserInterfaceBehaviour : UIBehaviour
{
/// <summary>
/// Get the <see cref="RectTransform"/> of the current <see cref="GameObject"/>.
/// </summary>
public ref RectTransform rectTransform
{
get
{
if (m_rectTransform is null)
{
m_rectTransform = GetComponent<RectTransform>();
}
return ref m_rectTransform;
}
}
private RectTransform m_rectTransform;
protected override void OnBeforeTransformParentChanged()
{
m_rectTransform = GetComponent<RectTransform>();
}
protected override void OnRectTransformDimensionsChange()
{
m_rectTransform = GetComponent<RectTransform>();
}
protected override void OnTransformParentChanged()
{
m_rectTransform = GetComponent<RectTransform>();
}
protected override void Reset()
{
m_rectTransform = GetComponent<RectTransform>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment