Skip to content

Basic Concepts ​

This document introduces the core code usage in ETOfflineToolkit which is also applicable to ET.

Component Mounting Usage in ETOfflineToolkit ​

Writing Components ​

Important Note

ET simplifies ECS into ES, meaning Entity = Component, Component = Entity

csharp
// Define a movement component
public class MoveComponent : Entity, IAwake, Iupdate, IDestroy
{
    // Can add some basic properties
    public float Speed;
    public bool IsMove;
}

Writing Systems ​

MoveComponentSystem Implementation
csharp
// Here is a component's lifecycle, implemented by inheriting the corresponding interfaces
[EntitySystem]
public class MoveComponentAwakeSystem : AwakeSystem<MoveComponent>
{
    protected override void Awake(MoveComponent self)
    {
        self.Speed = 10f;
        self.IsMove = true;
    }
}

[EntitySystem]
public class MoveComponentUpdateSystem : UpdateSystem<MoveComponent>
{
    protected override void Update(MoveComponent self)
    {
        if(!self.IsMove)
            return;
        self.Move();
    }
}

[EntitySystem]
public class MoveComponentDestroySystem : DestroySystem<MoveComponent>
{
    protected override void Destroy(MoveComponent self)
    {
        self.Speed = 0f;
        self.IsMove = false;
    }
}

// Define a system to handle player movement
public static class MoveComponentSystem
{
    // Handle player movement logic
    public static void Move(this MoveComponentSystem self, Vector3 direction)
    {
        // Movement related logic
        Debug.Log("Moving...")
    }
    // Handle player stop movement logic
    public static void StopMove(this MoveComponentSystem self, Vector3 direction)
    {
        // Movement related logic
        Debug.Log("Stop Move")
        self.IsMove = false;
    }
}

Practical Usage Examples ​

csharp
using System;
using UnityEngine;

namespace MH
{
    [Event(SceneType.Main)]
    public class AfterUnitCreate_EventView : AEvent<Scene, AfterUnitCreate>
    {
        protected override async ETTask Run(Scene scene, AfterUnitCreate a)
        {
            var unit = a.Unit;
            //...

            //Any entity that needs this movement component can add it
            unit.AddComponent<MoveComponent>();

            scene.AddComponent<MoveComponent>(); 
            //You can also do scene.Root.AddComponent<MoveComponent>(); Because this event's SceneType is Main, so scene is Root
            
            //Here you need to ensure Scene is not null, because the current Root might not have CurrentScene yet
            scene.Scene.AddComponent<MoveComponent>();
            //...
        }
    }
}
csharp
namespace MH
{
    [Event(SceneType.Main)]
    public class MainInit_EventView : AEvent<Scene, Main_Init>
    {
        protected override async ETTask Run(Scene scene, Main_Init a)
        {
            //...
            //Can be added anywhere you want
            scene.Scene.AddComponent<MoveComponent>();
            scene.Root.AddComponent<MoveComponent>();


            //Can also be accessed from anywhere (tips: must be mounted first)
            var moveComponent1 = scene.Root.GetComponent<MoveComponent>();
            var moveComponent2 = scene.Scene.GetComponent<MoveComponent>();
            moveComponent1.StopMove();
            moveComponent2.StopMove();
        }
    }
}

Next is the usage of Entity as a child entity of components, still using the MoveComponent defined above as an example

Writing Entities ​

csharp
// Define a movement info entity
public class MoveInfo : Entity, IAwake<float, float, float>, IDestroy
{
    // Can add some basic properties
    public float X;
    public bool Y;
    public bool Z;
}

Back to MoveComponent.cs add:

csharp
// Define a movement component
public class MoveComponent : Entity, IAwake, Iupdate, IDestroy
{
    // Can add some basic properties
    public float Speed;
    public bool IsMove;
    public List<MoveInfo> MoveInfos = new List<MoveInfo>();
}

Then back to MoveComponentSystem.cs add:

csharp
//...
public static class MoveComponentSystem
{
    //...
    public static void Add(this MoveComponentSystem self)
    {
        var moveInfo = self.AddChild<MoveInfo>();
        self.MoveInfos.Add(moveInfo);
    }
}

Similarly, Entity, whether as a component or a child entity, has its own corresponding System

MoveInfoSystem Implementation
csharp
[EntitySystem]
public class MoveInfoAwakeSystem : AwakeSystem<MoveInfo>
{
    protected override void Awake(MoveInfo self)
    {

    }
}
//...

// Define a system to handle player movement
public static class MoveInfoSystem
{

}

Important Note

However, note that an entity can only have one component, but can have multiple child entities

Lifecycle ​

Currently Supported Lifecycles

IAwake
csharp
public interface IAwake
{
}
public interface IAwake<A>
{
}

public interface IAwake<A, B>
{
}
public interface IAwake<A, B, C>
{
}
IDeserialize
csharp
public interface IDeserialize
{
}
IDestroy
csharp
public interface IDestroy
{
}
ILateUpdate
csharp
public interface ILateUpdate
{
}
IUpdate
csharp
public interface IUpdate
{
}

Technical Support ​

Get Help

  • 💬 Join QQ group for discussion (ET Framework Group): 474643097
  • ⭐ Follow the project on GitHub for the latest updates

Released under the MIT License.