UGUI系统研究讲解-----》UIBehaviour功能说明

UGUI的基础组件都最终继承自UIBehaviour,而UIBehaviour又继承自MonoBehaviour

所以我的讲解将从UGUI最基类UIBehaviour开始,并结合MonoBehaviour去研究。

首相直接上UIBehaviour代码截图。

上面除了框选的方法外都是运行是可调用的虚方法。而这些方法又是通过UIBehaviour的基类MonoBehaviour通过消息发送调用用的。类似MonoBehaviour的SendMessage()方法。具体调用方法在底层,估计是C++调用,具体过程无从查起,不做解释。直接上代码,我将继承MonoBehaviour而不是UIBehaviour。 用来说明这些方法都是通过MonoBehaviour接收信息回调的,UIBehaviour只不过是继承MonoBehaviour而已。

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Behaver :MonoBehaviour//: UIBehaviour
{
    protected void OnBeforeTransformParentChanged()
    {
        Debug.Log("OnBeforeTransformParentChanged--->父节点变化前");
    }

    protected void OnCanvasGroupChanged()
    {
        Debug.Log("OnCanvasGroupChanged");
        //base.OnCanvasGroupChanged();
    }

    protected void OnCanvasHierarchyChanged()
    {
        Debug.Log("OnCanvasHierarchyChanged");
    }


    protected void OnDidApplyAnimationProperties()
    {
        Debug.Log("OnDidApplyAnimationProperties");
    }


    protected void OnRectTransformDimensionsChange()
    {
        Debug.Log("OnRectTransformDimensionsChange-->RectTransform尺寸变化");
    }

    protected void OnTransformParentChanged()
    {
        Debug.Log("OnTransformParentChanged-->父节点变化了");

    }
}

然后在场景中创建如下节点并挂在组件

 

1.当我修改RectTansform参数如Width时将会调用OnRectTransformDimensionsChange。作用是当RectTransform尺寸变化是调用

官方解释

 

2.当修改CanvasGroup参数如Alpha时将会调用OnCanvasGroupChanged。作用是CanvasGroup参数改变时调用。

 

3.当设置Canvas激活状态和非激活状态时将会调用OnCanvasHierarchyChanged

 

4.上面还有一个方法OnDidApplyAnimationProperties(),此方法依赖于LayoutGroup.OnDidApplyAnimationProperties();

另写个脚本继承自GridLayoutGroup ,(GridLayoutGroup 继承自LayoutGroup

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GroupTest : GridLayoutGroup
{
    protected override void OnDidApplyAnimationProperties()
    {
        Debug.Log("OnDidApplyAnimationProperties");
        base.OnDidApplyAnimationProperties();
    }
}

添加下图节点,添加动画测试

OnDidApplyAnimationProperties作用时LayoutGroup播放animator动画时调用此方法。动画停止,OnDidApplyAnimationProperties结束调用。官方解释如下

 

LayoutGroup.OnDidApplyAnimationProperties

Other Versions

Leave feedback

protected void OnDidApplyAnimationProperties();

Description

Callback for when properties have been changed by animation.

此外还有两个方法OnBeforeTransformParentChanged(父节点变化前调用)和OnTransformParentChanged(父节点更改了调用)。在场景中拖拽红框改变父节点将会调用这两个方法

 

 

附上官方说明文档

UIBehaviour

class in UnityEngine.EventSystems

/

Inherits from:MonoBehaviour

Other Versions

Leave feedback

Description

Base behaviour that has protected implementations of Unity lifecycle functions.

Public Methods

IsActive

Returns true if the GameObject and the Component are active.

IsDestroyed

Returns true if the native representation of the behaviour has been destroyed.

Protected Methods

Awake

See MonoBehaviour.Awake.

OnBeforeTransformParentChanged

See MonoBehaviour.OnBeforeTransformParentChanged.

OnCanvasGroupChanged

See MonoBehaviour.OnCanvasGroupChanged.

OnCanvasHierarchyChanged

Called when the state of the parent Canvas is changed.

OnDestroy

See MonoBehaviour.OnDestroy.

OnDidApplyAnimationProperties

See LayoutGroup.OnDidApplyAnimationProperties.

OnDisable

See MonoBehaviour.OnDisable.

OnEnable

See MonoBehaviour.OnEnable.

OnRectTransformDimensionsChange

This callback is called if an associated RectTransform has its dimensions changed.

OnTransformParentChanged

See MonoBehaviour.OnRectTransformParentChanged.

OnValidate

See MonoBehaviour.OnValidate.

Reset

See MonoBehaviour.Reset.

Start

See MonoBehaviour.Start.

 

 

猜你喜欢

转载自blog.csdn.net/fengya1/article/details/84032082