- com.unity.entities 1.2.3
I’m new in DOTS and find trouble with the simple example.
I use a SpeedManager
to try interation between MonoBehaviour
and Entities. But I find that only the metrics in runtime inspector
changes(like Local Transform
and Local to World
). However, the Cube
in the Game
didn’t move.
I try modify the value in the Authoring Inspector
of Speed Authoring
. The Cube
finally moves in the game
view. But the Console
output still show that the speed
are setting to 2
, and local transform
are moving towards right. Actually, in the Game
view, the Cube
is moving at a speed of -0.2
.
And this is the ‘Mixed Inspector’ showing.
Class SpeedManager
.
using UnityEngine;
using Unity.Entities;
using Unity.Collections;
using TinyRTS;
using Unity.Transforms;
using UnityEngine.UIElements;
namespace TinyRTS
{
public class SpeedManager : MonoBehaviour
{
public float global_speed;
private void Start()
{
Debug.Log("SpeedManager Start!!");
global_speed = 2;
}
private void Update()
{
var entities = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(typeof(Speed)).ToEntityArray(Allocator.Temp);
Debug.Log("entities.Length: " + entities.Length + " global_speed: " + global_speed);
foreach (var entity in entities)
{
var speed = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<Speed>(entity);
// Debug.Log("speed.value ori: " + speed.value);
speed.value = global_speed;
World.DefaultGameObjectInjectionWorld.EntityManager.SetComponentData(entity, speed);
// speed = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<Speed>(entity);
// Debug.Log("speed.value after: " + speed.value);
var transform = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<LocalTransform>(entity);
Debug.Log("transform.Position" + transform.Position);
}
}
}
}
class `MovingAspect
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Burst;
namespace TinyRTS{
public readonly partial struct MovingAspect : IAspect
{
private readonly Entity entity;
private readonly RefRO<Speed> speed;
private readonly RefRW<LocalTransform> transform;
public void Move(float deltaTime)
{
transform.ValueRW.Position += new float3(1f, 0, 0) * speed.ValueRO.value * deltaTime;
}
}
[BurstCompile]
public partial struct MovingISystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
new MoveJob { deltaTime = SystemAPI.Time.DeltaTime }.ScheduleParallel();
}
}
[BurstCompile]
public partial struct MoveJob : IJobEntity
{
public float deltaTime;
[BurstCompile]
public void Execute(MovingAspect aspect)
{
aspect.Move(deltaTime);
}
}
}
class SpeedAuthoring
using Unity.Entities;
using UnityEngine;
namespace TinyRTS
{
public class SpeedAuthoring : MonoBehaviour
{
public float value;
}
public struct Speed : IComponentData
{
public float value;
}
public class SpeedBaker : Baker<SpeedAuthoring>
{
public override void Bake(SpeedAuthoring authoring)
{
Entity entity = GetEntity(authoring, TransformUsageFlags.Dynamic);
AddComponent(entity, new Speed { value = authoring.value });
}
}
}