Entity功能扩展
前面写过一篇3DUI的扩展,与3DUI的扩展类似,都是为了使游戏对象脱离框架,作为场景中GameObject的子物体。
下面直接上代码,功能也很简单,场景中的物体组件调用ChangeGroupPos改变实体组的父物体。当场景切换,场景中的物体被销毁时,调用ReGroupPos将实体组再还给框架。
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;
}
}
}
代码完成后,替换掉原来的Entity组件,代码里的注册逻辑对应也改成EntityFree。
GameEntry.Builtin
/// <summary>
/// 获取实体组件。
/// </summary>
public static EntityFreeComponent EntityFree
{
get;
private set;
}