Unity DOTS纯ECS实现虚拟摇杆Joystick控制角色移动

本文介绍了如何在Unity中利用DOTS的ECS框架和ProjectTiny包实现虚拟摇杆Joystick控制角色移动。由于Unity最新Entities包不支持纯ECS编码,故采用ProjectTiny(版本0.17)来实现这一功能。文章提供了关键代码示例,说明了Authoring脚本的使用以及在Unity编辑器和打包后的运行情况。
摘要由CSDN通过智能技术生成

上篇已经实现了ECS框架下的IBeginDragHandlerIDragHandlerIEndDragHandler这几个拖动事件,使得可以任意给ECS框架下的UI(2D entity)响应拖动事件。本篇分享下在前篇实现的功能的基础上再实现一个常用的摇杆控制角色移动的功能。

需要注意的一点,目前Unity最新的Entities包是无法纯ECS写代码的,还有很多GameObject和组件无法转成Entity和IComponentData,所以要体验纯ECS写代码,得下载那个很久没更新过的ProjectTiny包,它的Entities版本还停留在0.17阶段。

代码不多,直接上代码:

using Unity.Entities;
using Unity.Mathematics;
using Unity.Tiny.UI;
using wangtal.EventSystem;
using TinyUI;

namespace wangtal.Joystick {
   
    public struct Joystick : IComponentData {
   
        public readonly struct Settings : IComponentData {
   
            // 可拖动的范围,可以大于背景的圆形
            public readonly float Radius;

            public Settings(float radius) {
   
                Radius = radius;
            }
        }

        public Entity Entity {
   get; private set;}

        public Entity Knob {
   get; private set;}

        public Entity Highlight {
   get; private set;}

        // 拖动距离
        public float Distance;

        public float2 Direction;

        public Joystick(Entity joystickEntity, Entity knob, Entity highlight) {
   
            Entity = joystickEntity;
            Knob = knob;
            Highlight = highlight;
            Distance = 0;
            Direction = float2.zero;
        }
    }

    public abstract class JoystickSystem : SystemBase, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
   
    	// 上上篇实现的ECS下的UI分辨率适配系统-用来实现分辨率变化后,UI大小跟着变
        private CanvasScalerSystem canvasScalerSystem;

        protected override void OnCreate()
        {
   
            base.OnCreate();
            RequireSingletonForUpdate
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity ECS(Entity Component System)框架是一种高性能的游戏开发框架,其思想是将游戏对象拆分为实体(Entity)和组件(Component),并使用系统(System)对这些实体和组件进行处理。使用ECS框架可以有效地提高游戏的性能和可维护性。 要初始化NPC移动的数据,需要先创建一个包含移动组件(如Transform、Rigidbody等)的实体,然后添加一个移动系统(MoveSystem)。移动系统可以根据一定的规则(如AI算法、寻路等)来更新实体的移动组件,从而实现NPC的移动。 以下是一个简单的示例代码: ```csharp using Unity.Entities; using Unity.Mathematics; using Unity.Transforms; public class NPCMoveSystem : ComponentSystem { protected override void OnUpdate() { Entities.ForEach((ref Translation translation, ref Rotation rotation, ref NPCMoveComponent moveComponent) => { // 根据AI算法计算NPC的移动方向和速度 float3 direction = CalculateDirection(moveComponent.TargetPosition, translation.Value); float speed = CalculateSpeed(moveComponent.TargetPosition, translation.Value); // 更新实体的移动组件 translation.Value += direction * speed * Time.deltaTime; rotation.Value = quaternion.LookRotation(direction, math.up()); }); } private float3 CalculateDirection(float3 targetPosition, float3 currentPosition) { // 计算NPC需要移动的方向 return math.normalize(targetPosition - currentPosition); } private float CalculateSpeed(float3 targetPosition, float3 currentPosition) { // 计算NPC的移动速度 return 5f; } } public struct NPCMoveComponent : IComponentData { public float3 TargetPosition; } ``` 在这个示例中,我们创建了一个NPCMoveSystem类,继承自ComponentSystem,并重写了OnUpdate方法。在OnUpdate方法中,我们使用Entities.ForEach遍历所有具有NPCMoveComponent组件的实体,并根据AI算法计算NPC的移动方向和速度,然后更新实体的移动组件(Translation和Rotation),从而实现NPC的移动。 我们还定义了一个NPCMoveComponent结构体,用于存储NPC的目标位置(TargetPosition)。在创建NPC实体时,需要为其添加NPCMoveComponent组件,并设置TargetPosition的值,以指定NPC的目标位置。 这只是一个简单的示例,实际上NPC的移动可能需要更加复杂的逻辑和算法,但使用ECS框架可以使代码更加清晰、高效和易于维护。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值