ECS(一)1.0.0 Pre65 小球实例

ECS(一)环境

从包管理器安装

在这里插入图片描述
安装几个必要的包,如下。
com.unity.entities.graphics
com.unity.entities
com.unity.jobs
com.unity.burst
com.unity.mathematics
com.unity.physics
如果全部安装完上面的有些包会报错的话可以卸载再重新安装试试,我也不懂什么原理,就如有事没事重启电脑一样- -

新建sub场景
在这里插入图片描述

在这里插入图片描述
我这里用Cube做了一个Enemy预制体出来。添加EnemyAuthoring脚本到预制体中。
EnemyAuthoring脚本如下

using Unity.Entities;
enum EnemyState
{
    Patrol,
    Attack,
    Chase,
}
struct EnemyEntity : IComponentData
{
    public float speed;
    public EnemyState enemyState;
    public float attackRange;
}
public class EnemyAuthoring : UnityEngine.MonoBehaviour
{
    public float speed;
}
class EnemyBacker : Baker<EnemyAuthoring>
{
    public override void Bake(EnemyAuthoring authoring)
    {
        Entity entity = GetEntity(authoring, TransformUsageFlags.NonUniformScale | TransformUsageFlags.Dynamic);

        AddComponent(entity, new EnemyEntity
        {
            speed = authoring.speed,
            enemyState = EnemyState.Chase,
            attackRange = 3,
        });
    }

}

再用Sphere新建一个Player脚本,添加PlayerAuthoring脚本,PlayerAuthoring脚本如下

using Unity.Entities;

struct PlayerEntity : IComponentData
{
    public float speed;
}
class PlayerAuthoring : UnityEngine.MonoBehaviour
{
    public float speed;
}
class PlayerBacker : Baker<PlayerAuthoring>
{
    public override void Bake(PlayerAuthoring authoring)
    {
        Entity entity = GetEntity(authoring, TransformUsageFlags.NonUniformScale | TransformUsageFlags.Dynamic);
        AddComponent(entity, new PlayerEntity
        {
            speed = authoring.speed,
        });
    }
}

PlayerMovementSystem脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Physics;
using Unity.Mathematics;
//有InputSystem只支持SystemBase
public partial class PlayerMovementSystem : SystemBase
{
    public InputSystem inputActions;
    protected override void OnCreate()
    {
        inputActions = new InputSystem();
        inputActions.Enable();

    }
    protected override void OnUpdate()
    {
        Vector2 input = inputActions.Move.WASD.ReadValue<Vector2>();
        float2 PlayerPos = new float2(0, 0);
        Entities
        .WithAll<PlayerEntity>()
        .ForEach((ref LocalTransform transform, ref PhysicsVelocity vel, ref PlayerEntity playerEntity) =>
        {
            if (input.x != 0)
            {
                PlayerPos += transform.Right().xz * input.x * playerEntity.speed;
            }
            if (input.y != 0)
            {
                PlayerPos += transform.Forward().xz * input.y * playerEntity.speed;
            }
            vel.Linear.xz = PlayerPos;
        })
        //.Run();
        .WithBurst().ScheduleParallel();
    }
}

InputSystem的设置
在这里插入图片描述

EnemySystem脚本

using Unity.Entities;
using Unity.Transforms;
using Unity.Physics;
using Unity.Mathematics;
using System.Numerics;
using UnityEngine;
using Unity.Burst;


[BurstCompile]
partial struct EnemyMoveJob : IJobEntity
{
    public LocalTransform playerTransform;
    void Execute(ref LocalTransform transform, ref EnemyEntity enemy)
    {

        float3 dis = playerTransform.Position - transform.Position;
        float3 normalDis = math.normalize(dis);
        if (enemy.enemyState == EnemyState.Chase)
        {
            if (math.distance(playerTransform.Position, transform.Position) < enemy.attackRange)
            {
                //enemy.enemyState = EnemyState.Attack;
            }
            else
            {
                transform.Position += normalDis * enemy.speed;
            }
        }
        else if (enemy.enemyState == EnemyState.Attack)
        {
            if (math.distance(playerTransform.Position, transform.Position) > enemy.attackRange)
            {
                //enemy.enemyState = EnemyState.Chase;
            }
        }
    }
}
[BurstCompile]
public partial struct EnemySystem : ISystem
{
    [BurstCompile]
    public void OnCreate(ref SystemState state)
    {
    }
    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        Entity playerEntity = SystemAPI.GetSingletonEntity<PlayerEntity>();
        LocalTransform _playerTransform = SystemAPI.GetComponent<LocalTransform>(playerEntity);
        //这是第一种方法
        var jab = new EnemyMoveJob { playerTransform = _playerTransform };
        jab.ScheduleParallel();
        //也可以使用这种方法去实现追赶
        // foreach (var (enemy, transform, entity) in SystemAPI.Query<RefRW<EnemyEntity>, RefRW<LocalTransform>>().WithEntityAccess())
        // {
        //     float3 dis = _playerTransform.Position - transform.ValueRW.Position;
        //     float3 normalDis = math.normalize(dis);
        //     if (enemy.ValueRW.enemyState == EnemyState.Chase)
        //     {
        //         if (math.distance(_playerTransform.Position, transform.ValueRW.Position) < enemy.ValueRW.attackRange)
        //         {
        //             enemy.ValueRW.enemyState = EnemyState.Attack;
        //             //enemy.ValueRW.targetEntity = playEntity;
        //         }
        //         else
        //         {
        //             transform.ValueRW.Position += normalDis * enemy.ValueRW.speed;
        //         }
        //     }
        //     else if (enemy.ValueRW.enemyState == EnemyState.Attack)
        //     {
        //         if (math.distance(_playerTransform.Position, transform.ValueRW.Position) > enemy.ValueRW.attackRange)
        //         {
        //             enemy.ValueRW.enemyState = EnemyState.Chase;
        //         }
        //     }
        // }
    }

    // private EntityCommandBuffer.ParallelWriter GetEntityCommandBuffer(ref SystemState state)
    // {
    //     var ecbSingleton = SystemAPI.GetSingleton<BeginSimulationEntityCommandBufferSystem.Singleton>();
    //     var ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged);
    //     return ecb.AsParallelWriter();
    // }
}

demo地址
官方demo
官方demo

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值