A Step-by-Step Guide to Creating and Adding Components in Unity ECS

A Step-by-Step Guide to Creating and Adding Components in Unity ECS


Welcome to this guide on creating and adding components in Unity using ECS (Entity Component System). In this post, we’ll explore the differences between Unity’s standard components and ECS components and walk you through the process of adding components to entities. We’ll cover key concepts like baking and demonstrate how to set up and view components in both authoring and runtime modes. By the end of this tutorial, you’ll have a clear understanding of how to work with ECS components and implement them in your Unity projects.

Original in-depth Post Published here in Creating and Adding Components in Unity ECS: A Step-by-Step Guide by LearnUnityECS101



What is Unity ECS?

Before diving into creating components, let’s briefly review ECS, which stands for Entity Component System. It comprises three core elements:

  1. Entity – The identity of an object.
  2. Component – The data that describes the object.
  3. System – The logic that processes the data.

This post will focus specifically on components—how they differ from Unity’s standard components and how to create and add them to entities in ECS.



Unity’s Standard Components vs. ECS Components

Unity operates on a component-based system. Components like Rigidbody, Collider, Transform, and even custom scripts are examples of standard Unity components. These components provide behavior to GameObjects, such as enabling physics or attaching a script.

However, ECS components are data containers. They don’t provide behavior themselves but store the data that systems use to execute logic. For instance, to rotate an object, an ECS component might store the object’s RotationSpeed, while a system handles the actual rotation logic.



Creating Your First ECS Component

Let’s create an ECS component to give you a hands-on understanding of how data drives ECS systems. Assume you already have your first entity created in a subscene. If not, refer to my previous post on setting up entities in Unity ECS.

To create a component, follow these steps:

  1. Right-click in the Project window and choose Create > C# Script.
  2. Open the script and delete the MonoBehaviour inheritance. Replace it with the IComponentData interface.
  3. Define your ECS component as a struct instead of a class. Here’s an example:
public struct TankRotateStruct : IComponentData
{
    public float RotationSpeed;
}
Enter fullscreen mode

Exit fullscreen mode

Using struct and implementing IComponentData makes this a valid ECS component. Don’t worry about the struct vs class distinction for now—just remember that ECS components are always structs.



Adding Components to Entities with Baking

Now that we’ve created the TankRotateStruct, let’s add it to an entity. This is done through a process called baking. Baking allows us to convert Unity’s MonoBehaviour components into ECS components during the editor build process.

Here’s how to bake the component:

  1. Create a Baker class that overrides the Bake method. This method converts MonoBehaviour components into ECS components:
public class TankRotateBaker : Baker<TankRotateAuthoring>
{
    public override void Bake(TankRotateAuthoring authoring)
    {
        Entity entity = GetEntity(authoring, TransformUsageFlags.Dynamic);
        AddComponent(entity, new TankRotateStruct { RotationSpeed = authoring._rotationSpeed });
    }
}
Enter fullscreen mode

Exit fullscreen mode



Key Terms Explained:

  • GetEntity: Retrieves the entity corresponding to your GameObject. The authoring parameter refers to your MonoBehaviour script, and TransformUsageFlags.Dynamic indicates that the object’s transform will change dynamically (e.g., rotation).
  • AddComponent: This method attaches the TankRotateStruct component to the entity, passing in the RotationSpeed value from the MonoBehaviour.



Setting Up Components in Unity Editor

  1. Save your script and switch back to the Unity editor.
  2. Select the GameObject (your triangle entity).
  3. Add the MonoBehaviour script (TankRotateAuthoring) to the GameObject and set the RotationSpeed (e.g., 100).
  4. Change the view type to Realtime in the Inspector window, and you’ll see the TankRotateStruct component added to the entity.



Conclusion

This post guided you through creating and adding components in Unity ECS. We explored the differences between Unity’s standard components and ECS components, created a simple ECS component, and added it to an entity using baking. Understanding these steps will empower you to build more complex and efficient game logic using ECS.

For further insights and game development tips, don’t forget to check out my other posts on Unity ECS!



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.