新建脚本如下:
using Unity.Entities;
using Unity.Mathematics;
namespace ECSdemo
{
public struct GraveyardRandom : IComponentData
{
public Random Value;
}
}
扩充GraveyardMono如下:
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using Random = Unity.Mathematics.Random;
namespace ECSdemo
{
public class GraveyardMono : MonoBehaviour
{
public float2 FieldDimensions;
//float2 结构体表示一个包含两个 float 字段的结构体,记得引用命名空间
public int NumberTombstonesToSpawn;
//想要跟踪的数量
public GameObject TombstonePrefab;
//墓碑预制件
public uint RandomSeed;
}
public class GraveyardBaker : Baker<GraveyardMono>
{
public override void Bake(GraveyardMono authoring)
{
AddComponent(new GraveyardProperties
{
FieldDimensions = authoring.FieldDimensions,
NumberTombstonesToSpawn = authoring.NumberTombstonesToSpawn,
TombstonePrefab = GetEntity(authoring.TombstonePrefab)
//GetEntity:把GameObject变成Entity
});
AddComponent(new GraveyardRandom
{
Value = Random.CreateFromIndex(authoring.RandomSeed)
}) ;
}
}
}
赋个值
这边也能看到了
再写个新脚本
using Unity.Entities;
using Unity.Transforms;
namespace ECSdemo
{
public readonly partial struct GraveyardAspect:IAspect
{
public readonly Entity Entity;
private readonly TransformAspect transformAspect;
private readonly RefRO<GraveyardProperties> _graveyardProperties;
private readonly RefRW<GraveyardRandom> _graveyardRandom;
}
}
添加新脚本
using Unity.Burst;
using Unity.Entities;
namespace ECSdemo
{
[BurstCompile]
[UpdateInGroup(typeof(InitializationSystemGroup))]
public partial struct SpawnTombstoneSystem : ISystem
{
[BurstCompile]
void ISystem.OnCreate(ref SystemState state)
{
}
[BurstCompile]
void ISystem.OnDestroy(ref SystemState state)
{
}
[BurstCompile]
void ISystem.OnUpdate(ref SystemState state)
{
state.Enabled = false;
}
}
}