UGUI 源码之 UIBehaviour

UIBehaviour 中定义的生命周期要理解透彻(到底什么情况下被调用的)。

因为它的众多子类的行为严重依赖了这些生命周期。

需要结合实际测试,因为光看命名有歧义,很容易理解错误。

------------------------------------------------------------------------

注意:

1、源码中 OnCanvasHierarchyChanged 的部分注释写到了 IsDestroyed 上(错误)。

2、学到一个知识点:父类可以为子类提供子类应该实现的接口。

------------------------------------------------------------------------

更多 UGUI 注释已放入  https://github.com/NRatel/uGUI

namespace UnityEngine.EventSystems
{
    // Base behaviour that has protected implementations of Unity lifecycle functions.
    // 拥有一些 protected 的、Unity生命周期方法的 UI 基本行为类。
    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()
        {}

        // Returns true if the GameObject and the Component are active.
        // 若游戏对象和组件处于激活状态,则返回true。
        public virtual bool IsActive()
        {
            return isActiveAndEnabled;
        }

#if UNITY_EDITOR
        //编辑器下,脚本被加载、或 Inspector 中的任何值被修改时,方法被调用
        protected virtual void OnValidate()
        {}

        //编辑器下,脚本被加载、或 Inspector 上的Reset被点击时,方法被调用
        protected virtual void Reset()
        {}
#endif

        // 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 itselfdoesn't change - as it could have, depending on its anchoring.
        // 当关联的RectTransform的大小发生了变化时,方法被调用
        // 调用还会对所有子RectTransform生效,即使子RectTransform本身未发生变化。
        // (原因:子RectTransform的大小可能随父RectTransform的大小变化而变化,由锚点决定)
        protected virtual void OnRectTransformDimensionsChange()
        {}
        
        // 来自 Monobehaviour
        // 当关联的 Transform 的父物体变化前,方法被调用。
        // 指 Hieraychy 上的父子层级关系变化。
        // 可由 拖拽调整父子关系 或 调用 transform.SetParent()触发。
        // 若A是B的子物体,现调整为A是C的子物体。则OnBeforeTransformParentChanged()被调用,回调内A的父物体是B(即调整前的父物体)。(NRatel亲测)
        protected virtual void OnBeforeTransformParentChanged()
        {}
        
        // 来自 Monobehaviour
        // 当关联的 Transform 的父物体变化后,方法被调用。
        // 指 Hieraychy 上的父子层级关系变化。
        // 可由 拖拽调整父子关系 或 调用 transform.SetParent()触发。
        // 若A是B的子物体,现调整为A是C的子物体。则 OnTransformParentChanged() 被调用,回调内A的父物体是C(即调整后的父物体)。(NRatel亲测)
        protected virtual void OnTransformParentChanged()
        {}

        // <summary>
        // 当动画属性变化时,方法被调用。
        // </summary>
        protected virtual void OnDidApplyAnimationProperties()
        {}
        
        // 当关联的 CanvasGroup 变化时,方法被调用。
        // 指当 CanvasGroup 被启用或禁用(包括增加或移除)、或 CanvasGroup 的参数改变时被调用。(NRatel亲测)
        protected virtual void OnCanvasGroupChanged()
        {}

        // Called when the state of the parent Canvas is changed.
        // Returns true if the native representation of the behaviour has been destroyed.
        // 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.
        // 当关联的 Canvas 在 Hierarchy 上变化时,方法被调用。
        // 指当父 Canvas 被启用或禁用(包括增加或移除)或者嵌套 Canvas 的 OverrideSorting 发生更改时,系统会调用此函数。(NRatel亲测)
        // 注意:从 父Canvas 中拖出,或 拖入某Canvas,不会被调用。(NRatel亲测)
        // 例如,您可以使用此函数来修改Canvas下的对象(这可能取决于父Canvas)。
        // 例如,如果 Canvas 被禁用,您可能需要停止对 UI 元素的某些处理。
        // </summary>
        protected virtual void OnCanvasHierarchyChanged()
        {}

        // Returns true if the native representation of the behaviour has been destroyed.
        // 若本身表现已被销毁,则返回true。
        // UIBehaviour 帮其子类实现了一个默认的、ICanvasElement 接口定义的 IsDestroyed() 方法。(若子类实现 ICanvasElement 接口,无须再自己实现这个接口方法)
        public bool IsDestroyed()
        {
            // Workaround for Unity native side of the object
            // having been destroyed but accessing via interface
            // won't call the overloaded ==
            // “Unity本地对象已经被销毁,但通过接口访问不会调用重载的==”的变通方法。
            return this == null;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NRatel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值