Unity Game FrameWork—Component Extension—Entity Component Extension

Entity function extension

I wrote an extension of 3DUI earlier, similar to the extension of 3DUI, it is to make the game object out of the frame, as a sub-object of the GameObject in the scene.
The code is directly uploaded below, and the function is also very simple. The object component in the scene calls ChangeGroupPos to change the parent object of the entity group. When the scene is switched and the objects in the scene are destroyed, call ReGroupPos to return the entity group to the framework.

using GameFramework;
using GameFramework.Entity;
using GameFramework.ObjectPool;
using GameFramework.Resource;
using System;
using System.Collections.Generic;
using UnityEngine;


namespace UnityGameFramework.Runtime
{
    
    
    /// <summary>
    /// 实体组件。
    /// </summary>
    [DisallowMultipleComponent]
    [AddComponentMenu("Game Framework/EntityFree")]
    public class EntityFreeComponent : EntityComponent
    {
    
    
        private Dictionary<string, Transform> CustomEntityGroupHelpers;
        private float Scale = 1f;

        /// <summary>
        /// 游戏框架组件初始化。
        /// </summary>
        protected override void Awake()
        {
    
    
            base.Awake();
            CustomEntityGroupHelpers = new Dictionary<string, Transform>();
        }

        /// <summary>
        /// 增加实体组。
        /// </summary>
        /// <param name="entityGroupName">实体组名称。</param>
        /// <param name="instanceAutoReleaseInterval">实体实例对象池自动释放可释放对象的间隔秒数。</param>
        /// <param name="instanceCapacity">实体实例对象池容量。</param>
        /// <param name="instanceExpireTime">实体实例对象池对象过期秒数。</param>
        /// <param name="instancePriority">实体实例对象池的优先级。</param>
        /// <returns>是否增加实体组成功。</returns>
        public override bool AddEntityGroup(string entityGroupName, float instanceAutoReleaseInterval, int instanceCapacity, float instanceExpireTime, int instancePriority)
        {
    
    
            if (m_EntityManager.HasEntityGroup(entityGroupName))
            {
    
    
                return false;
            }

            EntityGroupHelperBase entityGroupHelper = Helper.CreateHelper(m_EntityGroupHelperTypeName, m_CustomEntityGroupHelper, EntityGroupCount);
            if (entityGroupHelper == null)
            {
    
    
                Log.Error("Can not create entity group helper.");
                return false;
            }

            entityGroupHelper.name = Utility.Text.Format("Entity Group - {0}", entityGroupName);
            Transform transform = entityGroupHelper.transform;
            transform.SetParent(m_InstanceRoot);
            transform.localPosition = Vector3.zero;
            transform.localEulerAngles = Vector3.zero;
            transform.localScale = Vector3.one * Scale;
            CustomEntityGroupHelpers.Add(entityGroupName, transform);
            return m_EntityManager.AddEntityGroup(entityGroupName, instanceAutoReleaseInterval, instanceCapacity, instanceExpireTime, instancePriority, entityGroupHelper);
        }
        /// <summary>
        /// 改变界面组坐标
        /// </summary>
        /// <param name="uiGroupName">界面组名称。</param>
        /// <param name="depth">界面组父物体。</param>
        public bool ChangeGroupPos(string EntityGroupName, Transform parents)
        {
    
    
            if (!m_EntityManager.HasEntityGroup(EntityGroupName))
            {
    
    
                return false;
            }
            Transform transform = CustomEntityGroupHelpers[EntityGroupName];
            if (transform == null)
            {
    
    
                Log.Error("Can not find entity group helper.");
                return false;
            }
            transform.SetParent(parents);
            transform.localPosition = Vector3.zero;
            transform.localEulerAngles = Vector3.zero;
            transform.localScale = Vector3.one * Scale;
            return true;
        }
        /// <summary>
        /// 恢复界面组层级
        /// </summary>
        /// <param name="uiGroupName">界面组名称。</param>
        /// <param name="depth">界面组父物体。</param>
        public bool ReGroupPos(string EntityGroupName)
        {
    
    
            if (!m_EntityManager.HasEntityGroup(EntityGroupName))
            {
    
    
                return false;
            }
            Transform transform = CustomEntityGroupHelpers[EntityGroupName];
            if (transform == null)
            {
    
    
                Log.Error("Can not find entity group helper.");
                return false;
            }
            transform.SetParent(m_InstanceRoot);
            transform.localPosition = Vector3.zero;
            transform.localEulerAngles = Vector3.zero;
            transform.localScale = Vector3.one;
            return true;
        }
    }
}

After the code is completed, the original Entity component is replaced, and the registration logic in the code is also changed to EntityFree.
GameEntry. Builtin

        /// <summary>
        /// 获取实体组件。
        /// </summary>
        public static EntityFreeComponent EntityFree
        {
    
    
            get;
            private set;
        }

insert image description here

Guess you like

Origin blog.csdn.net/qq_37619255/article/details/129287321