【不用找素材】ECS 游戏Demo制作教程(3) 1.17

一、生成墓碑

新建脚本如下:

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
    {
        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)
            }) ;
        }
    }

}

赋个值

【不用找素材】ECS 游戏Demo制作教程(3) 1.17_第1张图片

这边也能看到了

【不用找素材】ECS 游戏Demo制作教程(3) 1.17_第2张图片

再写个新脚本

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;
        private readonly RefRW _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;
        }
    }
}

你可能感兴趣的:(游戏)