[GameFramework]教程12 Unity实体隐藏与显示的坑

在业务逻辑层通常会遇到加载实体,与隐藏实体的情况,最近遇到一个特别的坑。就是我加载实体后,但是实体还没加完完全,此时调用GameEntry.Entity.HasEntity 方法是取不到实体的,但是此时我有需要隐藏实体...(zzz,真是坑)

熟不知E神已经做了这方面的处理,Unity在加载实体的时候是不能终止的。所以做了一个IsLoadingEntity状态,如果你的实体在IsLoadingEntity状态,这个时候就会预先加载到 m_EntitiesToReleaseOnLoad 队列,等待实体加载完成后,直接给隐藏

public void HideEntity(int entityId, object userData)
{
    if (IsLoadingEntity(entityId))
    {
        m_EntitiesToReleaseOnLoad.Add(entityId);
        return;
    }

    EntityInfo entityInfo = GetEntityInfo(entityId);
    if (entityInfo == null)
    {
        throw new GameFrameworkException(string.Format("Can not find entity '{0}'.", entityId.ToString()));

    }

    InternalHideEntity(entityInfo, userData);

}


private void LoadEntitySuccessCallback(string entityAssetName, object entityAsset, float duration, object userData)
{
    ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
    if (showEntityInfo == null)
    {
        throw new GameFrameworkException("Show entity info is invalid.");
    }


    m_EntitiesBeingLoaded.Remove(showEntityInfo.EntityId);
    if (m_EntitiesToReleaseOnLoad.Contains(showEntityInfo.EntityId))
    {
        Log.Debug("Release entity '{0}' on loading success.", showEntityInfo.EntityId.ToString());
        m_EntitiesToReleaseOnLoad.Remove(showEntityInfo.EntityId);
        m_EntityHelper.ReleaseEntity(entityAsset, null);
        return;
    }


    EntityInstanceObject entityInstanceObject = new EntityInstanceObject(entityAssetName, entityAsset, m_EntityHelper.InstantiateEntity(entityAsset), m_EntityHelper);
    showEntityInfo.EntityGroup.RegisterEntityInstanceObject(entityInstanceObject, true);


    InternalShowEntity(showEntityInfo.EntityId, entityAssetName, showEntityInfo.EntityGroup, entityInstanceObject.Target, true, duration, showEntityInfo.UserData);
}


猜你喜欢

转载自blog.csdn.net/zphshiwo/article/details/80685139