Unity Game FrameWork-module use-Entity entity

Entity function introduction

Official explanation: We define all dynamically created objects in the game scene as entities. This module provides functions for managing entities and entity groups, such as showing hidden entities, attaching entities (such as attaching a weapon, mount, or grabbing another entity), etc. After the entity is used, it may not be destroyed immediately, so as to wait for the next reuse.
First look at the parameters of the entity component:
[image]
EnableShowEntityUpdateEvent: Whether to display entity update events
EnableShowEntityDependencyAssetEvent: Whether to display entity loading dependent resource events
InstanceRoot: the root object of the entity group
EntityHelper: Entity helper, the entity helper can be customized, and needs to be inherited from EntityHelperBase. The main function is the logic of instantiating, creating, and releasing entities.
EntityGroupHelper: Entity group helper, entity helper can be customized, need to inherit from EntityGroupHelperBase. At present, there are no actual functions, and there is no need for expansion.
EntityGroups: Entity groups, entities need to be grouped here before use, set the following properties
1. entityGroupName entity group name.
2. instanceAutoReleaseInterval The interval in seconds between the automatic release of releasable objects by the entity instance object pool.
3. instanceCapacity Entity instance object pool capacity.
4. instanceExpireTime The number of seconds in which the entity instance object pool object expires.
5. instancePriority The priority of the entity instance object pool.
The attributes of the entity group correspond to the life cycle of the entity as follows
[image]
instanceCapacity parameter explanation
Object pool capacity refers to the minimum stable capacity. When creating an entity, it has nothing to do with the capacity of the object pool, and it can be created when the business logic needs to be created. When hiding entities, it is related to the capacity of the object pool. After hiding, if the number of entities is greater than the capacity of the object pool, the entities hidden just now will be destroyed immediately. If it is less than or equal to it, keep it and wait for it to expire.
Note: After the entity is hidden, if there is a need to create it, the life cycle of the entity will be ended directly, and the life cycle of the next entity will be entered.

Entity usage

Let’s take Demo’s asteroid entity as an example

Entity and attribute table settings

First put the created entity in the following directory
[image]
Add the asteroid resource name and number in the entity table
[image]
Asteroids additionally have an asteroid table Asteroid

The Entity entity table and the Asteroid asteroid table are what we need to pay attention to.
Click the button below to create the bytes file and the attribute class (DR class) corresponding to the data table (entity table, asteroid table)
The attribute data in the DR class is the row data of the original data table
[image]
[image]

Create entity data class and logic class

The AsteroidData asteroid data class is newly created in the demo, and the DR class data is encapsulated
Asteroid is the logic class of asteroids, which can be used to handle business logic such as asteroid generation, death, recycling, display, polling life cycle logic, and collision between entities.
[image]
generate entity data
The preloading process ProcedurePreload will preload the data tables we need, and the table names need to be added to the DataTableNames array.

public static readonly string[] DataTableNames = new string[]
{<!-- -->
    "Aircraft",
    "Armor",
    "Asteroid",
    "Entity",
    "Music",
    "Scene",
    "Sound",
    "Thruster",
    "UIForm",
    "UISound",
    "Weapon",
};

The following is the code to load the data table

private void LoadDataTable(string dataTableName)
{
    //dataTableAssetName: Path name with suffix (starting from Assets)
    string dataTableAssetName = AssetUtility. GetDataTableAsset(dataTableName, true);
    m_LoadedFlag.Add(dataTableAssetName, false);
    GameEntry.DataTable.LoadDataTable(dataTableName, dataTableAssetName, this);
}

Get entity data and generate entity
// Get asteroid data table
IDataTable dtAsteroid = GameEntry. DataTable. GetDataTable();
//Get the data corresponding to the asteroid number in the asteroid data table
DRAsteroid drAsteroid = dtAsteroid.GetDataRow(TypeId);//TypeId is the asteroid number
Below is the code to display the entity

/// <summary>
/// Display the entity.
/// </summary>
/// <param name="entityId">Entity ID. </param>
/// <param name="entityLogicType">entity logic type. </param>
/// <param name="entityAssetName">Entity resource name. </param>
/// <param name="entityGroupName">Entity group name. </param>
/// <param name="priority">The priority of loading entity resources. </param>
/// <param name="userData">User-defined data. </param>
public void ShowEntity(int entityId, Type entityLogicType, string entityAssetName, string entityGroupName, int priority, object userData)
{<!-- -->
    if (entityLogicType == null)
    {<!-- -->
        Log.Error("Entity type is invalid.");
        return;
    }
    m_EntityManager.ShowEntity(entityId, entityAssetName, entityGroupName, priority, ShowEntityInfo.Create(entityLogicType, userData));
}

The required parameters are entity number, entity logic class, entity resource path and name, and entity group name.
The following EntityExtension script makes an extended package for the display of the entity, we do not need to pass in the path, name, etc.

 public static void ShowAsteroid(this EntityComponent entityCompoennt, AsteroidData data)
    {
        entityCompoennt.ShowEntity(typeof(Asteroid), "Asteroid", Constant.AssetPriority.AsteroiAsset, data);
    }
    private static void ShowEntity(this EntityComponent entityComponent, Type logicType, string entityGroup, int priority, EntityData data)
    {
        if (data == null)
        {
            Log.Warning("Data is invalid.");
            return;
        }
        IDataTable<DREntity> dtEntity = GameEntry. DataTable. GetDataTable<DREntity>();
        DREntity drEntity = dtEntity.GetDataRow(data.TypeId);
        if (drEntity == null)
        {
            Log.Warning("Can not load entity id '{0}' from data table.", data.TypeId.ToString());
            return;
        }
        entityComponent.ShowEntity(data.Id, logicType, AssetUtility.GetEntityAsset(drEntity.AssetName), entityGroup, priority, data);
    }

Finally, we only need the following line of code to create the asteroid entity. We only need to pass in the entity ID and entity number of the asteroid data class. The entity ID is obtained from the entity component to ensure that the ID of each entity will not be repeated. The entity number uniquely corresponds to the name of the entity through the data table.
GameEntry.Entity.ShowAsteroid(new AsteroidData(GameEntry.Entity.GenerateSerialId(), 60002));

Shadow hidden entities

There are two ways, you can pass in the ID or the entity logic class. Logic classes are mounted on entities when they are created.
GameEntry. Entity. HideEntity(Id);
GameEntry.Entity.HideEntity(entity); //entity is the Entity class, which inherits the Entity class

Get entity

The only thing that determines the entity is the ID, and the created entity can be obtained from the entity component through the ID.
GameEntry. Entity. GetEntity(Id);