uGUI之继承结构

  1. UIBehaviour

    继承自MonoBehaviour ,代表ui 组件(区分渲染的ui组件)

    1. 组件流程方法
          //流程方法
            protected virtual void Awake()
            {}
    
            protected virtual void OnEnable()
            {}
    
            protected virtual void Start()
            {}
    
            protected virtual void OnDisable()
            {}
    
            protected virtual void OnDestroy()
            {}
    1. ui 组件结构变动回调函数

            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()
              {}
    2. 添加 IsActive 方法

          /// <summary>
            /// Returns true if the GameObject and the Component are active. 
            /// isActiveAndEnabled 变量存在于 Behaviour中,组件都有的两个变量,命名规则 isxx 都是只读变量 
            /// 1. enabled  2. isActiveAndEnabled
            /// </summary>
            public virtual bool IsActive()
            {
                return isActiveAndEnabled;
            }
    1. 添加 IsDestory 方法(todo)

              /// <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()
              {
                  // Workaround for Unity native side of the object
                  // having been destroyed but accessing via interface
                  // won't call the overloaded ==
                  return this == null;
              }
    2. 完整代码

         public abstract class UIBehaviour : MonoBehaviour
        {
            //流程方法
            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. 
            /// isActiveAndEnabled 变量存在于 Behaviour中,组件都有的两个变量,命名规则 isxx 都是只读变量 
            /// 1. enabled  2. isActiveAndEnabled
            /// </summary>
            public virtual bool IsActive()
            {
                return isActiveAndEnabled;
            }
    
    #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()
            {
                // Workaround for Unity native side of the object
                // having been destroyed but accessing via interface
                // won't call the overloaded ==
                return this == null;
            }
        }

文章作者: Yonggang Long
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Yonggang Long !
 上一篇
2022-08-10 Yonggang Long
下一篇 
2022-08-10 Yonggang Long
  目录