うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】UIBehaviour について

MonoBehaviour を継承した UIBehaviour というクラスが存在する

EventSystems.UIBehaviour - Unity スクリプトリファレンス

CanvasScalerGraphic など uGUI で利用するクラスの多くはこれを継承して作られている

実装されているメソッドは以下の通りで基本的には override して利用する

Github のコードはこちら
uGUI/UIBehaviour.cs at 2017.1 · Unity-Technologies/uGUI · GitHub

protected virtual void Awake()
 
protected virtual void OnEnable()
 
protected virtual void Start()
 
protected virtual void OnDisable()
  
protected virtual void OnDestroy()
 
/// <summary>
/// Returns true if the GameObject and the Component are active.
/// </summary>
public virtual bool IsActive()
 
#if UNITY_EDITOR
 
protected virtual void OnValidate()
 
protected virtual void Reset()
 
#endif
 
/// <summary>
/// This callback is called if an associated RectTransform has its dimensions changed. The call is also made to all child rect transforms, even if the child transform itself doesn't change - as it could have, depending on its anchoring.
/// </summary>
protected virtual void OnRectTransformDimensionsChange()
 
protected virtual void OnBeforeTransformParentChanged()
 
protected virtual void OnTransformParentChanged()
 
protected virtual void OnDidApplyAnimationProperties()
 
protected virtual void OnCanvasGroupChanged()
 
/// <summary>
/// Called when the state of the parent Canvas is changed.
/// </summary>
protected virtual void OnCanvasHierarchyChanged()
 
/// <summary>
/// Returns true if the native representation of the behaviour has been destroyed.
/// </summary>
/// <remarks>
/// When a parent canvas is either enabled, disabled or a nested canvas's OverrideSorting is changed this function is called. You can for example use this to modify objects below a canvas that may depend on a parent canvas - for example, if a canvas is disabled you may want to halt some processing of a UI element.
/// </remarks>
public bool IsDestroyed()

uGUI で利用するときに便利なのかと思うが OnRectTransformDimensionsChange 等のイベントは
MonoBehaviour を継承したクラスに 定義すると利用できる

どんなイベントが利用できるか知る分にはいいかもしれないが、 個人的には現状の UIBehaviour を利用する必要性は無いと思っています

すべての UI に使われているコンポーネントをこれを継承して作る等のルールがあれば
スクリプトを見れば UI 用かどうか分かるので 多少便利かもしれません・・・