Skip to main content

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

Welcome to our 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.

Let me remind you of the full form of ECS: Entity Component System. It consists of three key elements: 1. Entity, 2. Component, and 3. System. In this blog post, I will specifically focus on how to create and add components, and explain the differences between Unity's standard components and ECS components. If you haven’t already, be sure to check out my blog post on creating your first entity and setting up a subscene.

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

What is ECS Component :

Can you describe what a component is in Unity? Please use the comments section to share your knowledge. Unity operates on a component-based system. For instance, we have components like Rigidbody, Collider, Transform, as well as custom components, such as scripts, to create a game. I hope this gives you a clear understanding of Unity Standard components.

Now, let’s explore the difference between Unity’s standard components and ECS components. You can follow the official documentation for a technical definition of ECS components. If you find it difficult to understand, let me clarify it for you. In ECS architecture, we use components to describe our data. For example, if we want to create a rotation system, we first need to determine the type of data required. We need the object's transform to rotate and an input speed to control how fast the object will rotate. I believe this makes things clearer now.

Create Your First Component :

Let’s create your first component so you can understand the concept of data I’ve been talking about. But before that, take a look at the image below. I assume you already have your first entity in your subscene. If not, please refer to my blog post on how to create your first entity and set up a subscene.


The red lines in the image above represent ECS components. Local Transform, LocalToWorld, and others are examples of ECS components.

Let’s create your first component! Follow the instructions, and you’ll master creating components in ECS, haha! Right-click in the Project window, select 'Create' and then 'C# Script'. Open it in your preferred IDE.


If your script isn't using MonoBehaviour, please add it. Next, create a new structure in the space below, named 'TankRotateStruct', and implement the 'IComponentData' interface to make it an ECS component. Your structure should look like the image below.


Don’t worry if you're unsure why we’re using a struct instead of a class right now. Just focus on this one point: to create a component in ECS architecture, you must use the 'IComponentData' interface. You can also refer to the official documentation on creating your first component. But there’s no need to rush – in upcoming sections, I’ll cover this in depth with real-time game examples.

Adding Component to Your Entity :

Check your triangle entity, and you’ll notice it doesn’t have this component yet. That’s because we haven’t added it to the triangle entity. To do this, we need to follow the baking process. Don’t be intimidated by the term ‘baking’ – it’s nothing complex. Right now, it’s not important to understand the baking process, as I will cover it fully in later blog posts. unity automatic bake your component when editor reload or compile code.

Let’s add your 'TankRotateStruct' to the triangle entity using the baking process. To bake any component, you need to define a baker class. Check the code in the image below.


To add your component to the entity, we need a baker class to handle the baking process. Check the syntax of the baker class and override the Bake method.


Please review the code in the image above, which is written in the Bake method. You’ll notice new terms such as GetEntity and AddComponent. Let’s discuss them.

GetEntity: This method is used to retrieve the entity from our triangle entity. In the parameters of GetEntity, I’ve passed authoring and TransformUsageFlags.Dynamic. Let’s understand both terms:

Authoring: This refers to your MonoBehaviour class and its associated object. GetEntity will fetch the MonoBehaviour object’s transform and use it as an entity.

TransformUsageFlags.Dynamic: I’ll cover this in more detail later, but for now, consider TransformUsageFlags as how you intend to use this entity in your game. Dynamic means that you will be changing this transform’s position, scale, and rotation.

I hope these terms make sense. If not, try rewriting the code, and you’ll understand better through practice.

AddComponent: In Unity, we have methods like AddComponent and RemoveComponent. Do you remember those? We use AddComponent in non-ECS Unity projects to add Rigidbody or other Unity components at runtime.

In ECS, AddComponent works similarly, but here you need to pass data from the editor into the component during the baking process.

Let’s understand the parameters of AddComponent. As the name suggests, this method requires two things: the entity you want to add the component to, and the component you want to add—in our case, TankRotateStruct.

We use TankRotateStruct with the new keyword to create it and pass authoring._rotationSpeed as RotationSpeed, because we want to transfer the MonoBehaviour’s rotation speed to the component’s rotation speed.

Now save the files and open your editor. Select your triangle game object, go to the Inspector window, and change the component view type to 'Authoring' (Authoring refers to the MonoBehaviour type). Add your MonoBehaviour script to this object and set the RotationSpeed to 100.




Next, change the component view type back to 'Realtime', scroll down, and find the 'TankRotateStruct' component. I hope you’ve been able to follow along so far and have successfully created and added your component to the entity.



Summarizing Creating and Adding Components in Unity ECS:

This blog post explains how to create and add components in Unity using ECS (Entity Component System). It outlines the key differences between Unity's standard components and ECS components. The post details the process of adding a component to an entity through baking, including the use of GetEntity and AddComponent. It provides step-by-step instructions for setting up components in the Unity editor and demonstrates how to configure and view these components in both authoring and runtime modes.

Comments

Popular posts from this blog

What is ECS in Unity ?

Today, I will address common questions asked by Unity developers: 'What is ECS in Unity?', 'Is ECS worth using in your next project?', 'Can I convert my old project to Unity ECS?', and 'Where should I start learning Unity ECS?'. Stay with me as I cover all these answers in this post. Welcome back! If you're not aware of why I created this blog, let me clarify. I'm converting an old Unity project to the new DOTS technologies. First, I’ll be using ECS to re-architect the project, and then I’ll delve into the physics aspects. Stay with me and enhance your skills by learning Unity ECS. When I began learning Unity DOTS, I noticed several frequently asked questions and discussions posted on Reddit, Unity forums, and Unity Discord channels. Let me share my experiences and answer these questions. Table of Content : What is ECS in Unity? What is Entity ? What is Component ? What is System ? Is ECS worth using in your next projec

Setting Up Unity DOTS: Creating and Configuring Your First Entity

Welcome back, Unity developers! Today, I will be setting up a non-ECS Unity project to run using the Entity Component System (ECS), Moreover you will setup your first subscene and create your first entity. I will address entity not visible issue. If you haven't yet checked the commonly asked questions about the Entity Component System , you should take a look first, where I've explained the basic definition of ECS in Unity. Setting Up Unity DOTS Creating and Configuring Your First Entity by LearnUnityECS101.Blogspot.com Table of Contents : Project Clone Instruction ECS Package Install and Setup Guide Setup ECS Scene Create Your First Entity Entity is not visible error Verify Your First Entity   Domain Reload Setting   Summarizing Project Clone Instruction: First of all, you need a Unity project in working condition. If you already have such a project, you can use it, but I would recommend using the project I am about to convert to Unity DOTS - ECS. Ple