Welcome back, everyone! As the title suggests, in this post, we’ll explore how to generate entities from prefabs at runtime. This is a crucial aspect of any game engine, as it allows for the dynamic creation of game objects during gameplay. Since this blog focuses on Unity ECS, I will explain how to generate entities at runtime using this system.
Recap of previous posts: If you haven’t yet checked out the earlier posts, I’ve covered how to create and add components in Unity ECS, as well as the differences between standard components and custom ECS components. I highly recommend reading them, especially if you’re a beginner. In this blog, I will guide you on converting your non-ECS Unity project to Unity ECS and cover all related topics.
I hope you’ve opened your ECS project. In the last couple of posts, I explained how to create entities from the editor, so do check those if you’re unfamiliar with the process. Once you’ve done that, you can start from here.
A Complete Guide to Generating Entity Prefabs at Runtime in Unity ECS |
Table of contents
- Transforming the Triangle Game Object into a Prefab for Runtime Instantiation
- Understanding the Theory: Instantiating Entity Prefabs in Unity ECS
- The Standard Unity Prefab Instantiation Process: A Quick Recap
- The ECS Prefab Instantiation Process: Key Differences and Insights
- Implementing Runtime Prefab Generation with Unity ECS: A Step-by-Step Approach
- Creating the Required Scripts for Runtime Entity Prefab Instantiation:
- Code Implementation: Generating Entity Prefabs at Runtime in Unity ECS:
- Instantiating Entities in Unity DOTS: A Practical Code Example:
- Summarising the Process: Generating Entity Prefabs at Runtime in Unity ECS :
Transforming the Triangle Game Object into a Prefab for Runtime Instantiation
In the last post, we created the Triangle entity game object in our subscene. Now, let’s turn that Triangle into a prefab and instantiate it at runtime using Unity ECS.
To create a prefab of the Triangle game object, simply select it, then drag and drop it into your Project window. You’ll notice that the Triangle entity will now appear in blue, indicating it’s a prefab.
If you’re having difficulty creating a prefab, I recommend checking Unity’s official documentation on the process of creating prefab. Additionally, if you still find it challenging, refer to the GIF video below for further guidance.
Understanding the Theory: Instantiating Entity Prefabs in Unity ECS
Let’s first recap the standard Unity prefab instantiation process at runtime. In the typical prefab generation system, we must have a prefab and a script that accesses this prefab from the Project window to instantiate it. I assume you’re already familiar with this.
In other words, to instantiate a prefab, we need a script that is responsible for fetching the prefab from storage (the Project folder) and then creating an instance of it in memory at runtime.
The Standard Unity Prefab Instantiation Process: A Quick Recap:
In Unity ECS, we follow a similar process but divide each task into separate sub-processes. Let’s understand this with a real-world example of Unity ECS. In the standard prefab process, we have a script that fetches the prefab from storage (the Project folder), correct? Next, we use that prefab to instantiate it at runtime, correct? This means we have two separate processes for generating a prefab at runtime. I hope you’re following so far, but if not, feel free to leave a comment, and I’ll respond as soon as possible.
The ECS Prefab Instantiation Process: Key Differences and Insights:
Now that we understand these two processes, let’s implement them using Unity’s Entity Component System (ECS).
To access a prefab from storage (the Project folder) in ECS, we use a baking process through a class called a baker. If you’re unfamiliar with this, I recommend checking out my previous post on creating and adding custom components in Unity ECS.
Next, to instantiate it at runtime, ECS provides ISystem and SystemBase interfaces and classes. I’ll explain it in separate post about when to use ISystem and when to use SystemBase, so stay tuned to the LearnUnityECS101 blog. In short, both function similarly to Start, Update, and OnDestroy methods that we’re familiar with from MonoBehaviour.
I hope this helps clarify how to generate entity prefabs at runtime using Unity ECS and the key differences between the standard prefab generation method and the ECS prefab generation method.
If it’s still unclear, don’t worry. In the next section, I’ll implement it in code, and with a live example, you’ll grasp it better.
Implementing Runtime Prefab Generation with Unity ECS: A Step-by-Step Approach:
Creating the Required Scripts for Runtime Entity Prefab Instantiation:
- Let’s begin by creating the relevant scripts. To access the prefab entity from storage, we need a MonoBehaviour class. In my case, I’ve created a GeneratePrefab MonoBehaviour class.
- To transfer that prefab from MonoBehaviour to the entity world, we must bake it using a baker class. For this, I’ve created a GeneratePrefabAuthoring class that inherits from Baker.
- As we know, we need a component to pass MonoBehaviour data to the entity world, so I’ve created a struct named GeneratePrefabStruct.
- Finally, we need an update loop to write the prefab generation code that will execute at runtime. For this, I’ve created a class called GeneratePrefabSystem that inherits from ISystem.
- In the next section, we’ll write the code to instantiate the entity prefab at runtime in Unity ECS and understand each class and its responsibility.
Code Implementation: Generating Entity Prefabs at Runtime in Unity ECS:
GeneratePrefab MonoBehaviour: Accessing Prefabs from Storage:
This class contains a public GameObject named "prefab". The GeneratePrefab class is a standard Unity MonoBehaviour class, and to access the prefab from storage, we need a public property to retrieve it. See the code example below.# Code by learnunityecs101
public class GeneratePrefab : MonoBehaviour { public GameObject prefab; }
Baking the Prefab: The GeneratePrefabAuthoring Class Explained:
# Code by learnunityecs101
public class GeneratePrefabAuthoring : Baker<GeneratePrefab> { public override void Bake(GeneratePrefab authoring) { var _entity = GetEntity(authoring, TransformUsageFlags.Dynamic); AddComponent(_entity, new GeneratePrefabStruct() { _prefab = GetEntity(authoring.prefab, TransformUsageFlags.Dynamic), }); } }
Introducing GeneratePrefabStruct: Structuring Prefab Data for ECS :
# Code by learnunityecs101
public struct GeneratePrefabStruct : IComponentData { public Entity _prefab; }
Implementing the GeneratePrefabSystem: Using ISystem for Runtime Generation :
Instantiating Entities in Unity DOTS: A Practical Code Example:
# Code by learnunityecs101
public partial struct GeneratePrefabSystem : ISystem { void OnUpdate(ref SystemState state) { foreach (var _prefab in SystemAPI.Query<RefRW<GeneratePrefabStruct>>()) { var _newPrefab = state.EntityManager.Instantiate(_prefab.ValueRW._prefab); } } }
Comments
Post a Comment