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

发布时间:2024年01月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<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;
        }
    }
}

文章来源:https://blog.csdn.net/m0_53930254/article/details/135653144
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。