ECS Proxy in Unity

So far, I was creating entities by using EntityManager and Archetypes. However, you can also create GameObject with ECS components but using a proxy and later convert it into an entity.

So am I saying that we can convert GameObjects into Entities?!

Yes, we can!

We can either make GameObject into Game Object Entity or convert it into an Entity and destroy original GameObject.

But there is a catch!

Regular components won’t work as ECS components, and we can’t just attach ECS component onto GameObject. (There are few exceptions to this like MeshRenderer, but I won’t dive deeper into it here)

So how can we configure such an object? We have to make proxies for components that we want to attach!

Wait, a proxy?

Yup! Let me explain what a proxy mean here. Proxy in an ECS component overlay and it is almost like MonoBehaviour in a sense that you can attach it onto GameObject and configure visible variables in the Inspector.

Implementation

When we have the ECS component already created, making proxy is a very easy process.

Let’s use our tweening system from last post and Hover component as a base.

using Unity.Entities;
using Unity.Mathematics;
using Unity.Collections;

/// <summary>
/// ECS component for Hover Tween.
/// </summary>
[System.Serializable]
public struct Hover : IComponentData
{
    // Animation Baseline
    [ReadOnly] public float Delay; // Animation Delay.
    [ReadOnly] public float Duration; // Animation Duration.
    [ReadOnly] public bool LoopAnimation; // Is animation looping?
    [ReadOnly] public AnimationCurveEnum AnimationCurve; // Animation curve used for this tween.

    public float ElapsedTime; // Animation Elapsed Time.

    // Hover
    [ReadOnly] public float3 TopValue;
    [ReadOnly] public float3 BottomValue;
}

The second step will be to create a proxy component that we could attach to the GameObject. Following naming

using Unity.Entities;
using UnityEngine;

/// <summary>
/// Proxy for Hover component.
/// </summary>
[DisallowMultipleComponent]
public class HoverProxy : ComponentDataProxy<Hover> { }

When this step is done, we should be able to attach our ECS component onto GameObject.

Inspector view after attaching HoverProxy

You can notice that Game Object Entity was also attached, this component is required when it comes to using GameObjects in ECS.

But in our case, it won’t be needed as we are going to convert our GameObject into Entity.

The last step is to make our Hover system working. To do that we need to add Translation proxy to the GameObject and we are done!

Fully configured ECS object.
Now we can see ECS object in action!

Great! We made it!

What do you think about it? Are you using ECS already? Let me know in the comment section below!

If you want to get notified on future content, sign up for the newsletter!

As always, the whole project is available at my public repository. ๐Ÿ”—

And I hope to see you next time! ๐Ÿค“

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments