首页 > 代码库 > Unity编程标准导引-3.2 MonoBehavior 组件父类重构

Unity编程标准导引-3.2 MonoBehavior 组件父类重构

本文为博主原创文章,欢迎转载。请保留博主链接:http://blog.csdn.net/andrewfan

3.2MonoBehavior 组件父类重构

为了让我们的组件类用起来更方便,我们将上一节的代码进行重构。改写成如下:

using UnityEngine;
using System.Collections;

namespace Assets.AndrewBox.Comp
{
    /// <summary>
    /// 初始化状态
    /// </summary>
    public enum InitState
    {
        NothingInited,
        FirstInited,
        SecondInited
    }
    public abstract class BaseBehavior : AbstractBehavior
    {
        /// <summary>
        /// 当前的初始化状态
        /// </summary>
        protected InitState m_initState = InitState.NothingInited;
        //变换对象
        public Transform m_transform;
        /// <summary>
        /// 在此完成第一阶段的初始化
        /// </summary>
        protected override sealed void Awake()
        {
            m_transform = transform;
            DoInit(InitState.FirstInited);
        }
        /// <summary>
        /// 在此完成第二阶段的初始化
        /// </summary>
        protected override sealed void Start()
        {
            DoInit(InitState.SecondInited);
        }
        /// <summary>
        /// 执行更新
        /// </summary>
        protected override sealed void Update()
        {
            if (m_initState != InitState.SecondInited)
            {
                return;
            }
            OnUpdate();
        }
        /// <summary>
        /// 用于首次初始化,完成外部需访问的公共成员的初始化
        /// </summary>
        protected abstract void OnInitFirst();
        /// <summary>
        /// 用于二次初始化,完成内部私有成员的初始化
        /// </summary>
        protected abstract void OnInitSecond();
        /// <summary>
        /// 执行更新
        /// </summary>
        protected abstract void OnUpdate();
        /// <summary>
        /// 用于外部主动调用初始化
        /// </summary>
        /// <param name="targetState">初始化的目标状态</param>
        public void DoInit(InitState targetState=InitState.SecondInited)
        {
            if (m_initState == InitState.NothingInited&& m_initState<targetState)
            {
                OnInitFirst();
                m_initState = InitState.FirstInited;
            }
            if (m_initState == InitState.FirstInited && m_initState<targetState)
            {
                OnInitSecond();
                m_initState = InitState.SecondInited;
            }
        }


    }

    public abstract class AbstractBehavior : MonoBehaviour
    {
        protected virtual void Awake()
        {
        }

        protected virtual void Start()
        {
        }

        protected virtual void Update()
        {
        }
    }

}

接下来,原先的TestComponent类将变成如下形式:

using UnityEngine;
using System.Collections;
using Assets.AndrewBox.Util;
using Assets.AndrewBox.Comp;

public class TestComponenets : BaseBehavior 
{
    protected override void OnInitFirst()
    {
        Debuger.LogAtFrame("OnInitFirst");
    }

    protected override void OnInitSecond()
    {
        Debuger.LogAtFrame("OnInitSecond");
    }

    protected override void OnUpdate()
    {
        Debuger.LogAtFrame("OnUpdate");
    }

    protected void OnEnable()
    {
        Debuger.LogAtFrame("OnEnable");
    }
    protected void OnDisable()
    {
        Debuger.LogAtFrame("OnDisable");
    }
    protected void OnDestroy()
    {
        Debuger.LogAtFrame("OnDestroy");
    }


}

这样的好处是让继承类严格按照格式进行初始化,也可以在组件创建的早期,手动进行立刻完整初始化。

本文为博主原创文章,欢迎转载。请保留博主链接:http://blog.csdn.net/andrewfan

Unity编程标准导引-3.2 MonoBehavior 组件父类重构