Hello,
I finally decided to try the new ECS system.
I’m quite new with this technology so be patient with me.
I tried a very simple example to understand how it work :
I got a simple component :
public class Rotator : MonoBehaviour
{
public float Speed;
}
And a simple system :
public class RotatorSystem : ComponentSystem
{
public struct Components : IComponentData
{
public Rotator Rotator;
public Transform Transform;
}
protected override void OnUpdate()
{
EntityQuery entityQuery = GetEntityQuery(ComponentType.ReadWrite<Components>());
NativeArray<Components> components = entityQuery.ToComponentDataArray<Components>(Allocator.Temp);
float deltaTime = Time.DeltaTime;
foreach (Components component in components)
{
component.Transform.Rotate(0.00f, component.Rotator.Speed * deltaTime, 0f);
}
components.Dispose();
}
}
}
The purpose is to rotate 3 cubes on the scene. So very simple…
The problem is that, I start with an error :
ArgumentException: Tutorial_ECS.RotatorSystem+Components contains a field of Tutorial_ECS.Rotator, which is neither primitive nor blittable.
If I run the scene :
ArgumentException: Unknown Type:Tutorial_ECS.RotatorSystem+Components
All ComponentType must be known at compile time. For generic components, each concrete type must be registered with [RegisterGenericComponentType].
I tried to add :
[assembly: RegisterGenericComponentType(typeof(RotatorSystem.Components))]
But It does not seems to work.
Anybody can explain me what i’m doing wrong ?
Update : I use Entities V5.0.1
Sato
1 Like
As the error tells you, the fields of your IComponentData must be primitive or blittable.
You should have an Authoring version and an ECS version for your components, like this:
public class RotatorAuthoring : MonoBehaviour
{
public float Speed;
}
public struct RotatorData : IComponentData
{
public float Speed;
}
And assign the respective values during conversion.
Take a look on the ECS samples on the GitHub, there is no better place to get started: GitHub - Unity-Technologies/EntityComponentSystemSamples
Ok, so i tried something like this :
public class Rotator_Authoring : MonoBehaviour, IConvertGameObjectToEntity
{
public float Speed;
public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
Rotator_Data data = new Rotator_Data
{
Speed = Speed,
};
dstManager.AddComponentData(entity, data);
}
}
public struct Rotator_Data : IComponentData
{
public float Speed;
}
public class RotatorSystem : ComponentSystem
{
EntityQuery _group;
protected override void OnCreate()
{
_group = GetEntityQuery(typeof(Rotation), ComponentType.ReadOnly<Rotator_Authoring>());
}
protected override void OnUpdate()
{
}
}
But how can I access the transform (Rotation) to rotate my cubes ?
I can retrieve the data from Rotator_Data but there is no example with EntityQuery to do it
By the way thank you for the answer and the example 
In the link there is an example of something similar:
https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/ECSSamples/Assets/HelloCube/1.%20ForEach/RotationSpeedSystem_ForEach.cs
You should access the component Rotation (as you already seems to be doing), in ECS the Transform was broke into smaller components (Translation, Rotation and Scale).
Ok, thank you, I think I finally understand the way to use it.
Instead of giving the reference of the entire transform, we use the reference of it rotation.
Thanks for the example and the explaination:)
1 Like