Use SystemState.state.Instantiate
to instantiate prefabs but the parent relationship was broken. The only different was the Transform.scale
add photos…
And if i change the scale
to (1, 1, 1)
, the relationship will be right.
And the spawnersystem code is
using Unity.Entities;
using Unity.Transforms;
using Unity.Burst;
using TinyRTS;
using Unity.Mathematics;
using UnityEngine;
using TinyRTS.Utils;
namespace TinyRTS
{
[BurstCompile]
public partial struct SpawnerSystem : ISystem
{
// private static bool init_flag = false;
public void OnCreate(ref SystemState state)
{
//foreach (RefRW<Spawner> spawner in SystemAPI.Query<RefRW<Spawner>>())
//{
// InitProcessSpawner(ref state, spawner);
//}
}
public void OnDestroy(ref SystemState state) { }
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
if (Input.GetMouseButtonDown(0))
{
//Debug.Log("Input.GetMouseButtonDown(0)");
}
// Queries for all Spawner components. Uses RefRW because this system wants
// to read from and write to the component. If the system only needed read-only
// access, it would use RefRO instead.
foreach (RefRW<Spawner> spawner in SystemAPI.Query<RefRW<Spawner>>())
{
InitProcessSpawner(ref state, spawner);
// ProcessSpawner(ref state, spawner);
}
}
private void InitProcessSpawner(ref SystemState state, RefRW<Spawner> spawner)
{
if (spawner.ValueRO.NextSpawnTime < SystemAPI.Time.ElapsedTime){
int num = 4;
int range = 25;
for (int i = 0; i < num; i++)
{
float x = (i - num / 2) * (range / num);
for (int j = 0; j < num; j++)
{
float z = (j - num / 2) * (range / num);
Entity newEntity = state.EntityManager.Instantiate(spawner.ValueRW.Prefab);
float3 pos = new float3(x, 0.0f, z);
// state.EntityManager.SetComponentData(newEntity, LocalTransform.FromPosition(pos));
state.EntityManager.SetComponentData(newEntity, LocalTransform.FromPosition(pos));
UnitBasicAttribute unitBasicAttribute = new UnitBasicAttribute
{
MaxVelocity = 1.1f,
MoveTargetMapPos = TinyRTSUtils.WorldPos2MapPos(pos),
Selected = false
};
state.EntityManager.SetComponentData<UnitBasicAttribute>(newEntity, unitBasicAttribute);
}
}
spawner.ValueRW.NextSpawnTime = (float)SystemAPI.Time.ElapsedTime + spawner.ValueRO.SpawnRate;
}
}
}
}