此示例演示了一个简单的ECS系统,它可以旋转一对立方体。

它显示了什么?

此示例演示了ECS中数据和功能的分离。
数据存储在组件中,如下RadiansPerSecond属性存储在RotationSpeed_ForEach组件中, 该结构体数据组件有[Serializable]修饰符,表示实例化属性。

using System;

using Unity.Entities;

// Serializable attribute is for editor support.

[Serializable]

public struct RotationSpeed_ForEach : IComponentData

{
     public float RadiansPerSecond;

}

而功能则写入系统,如下RotationSpeedSystem_ForEach 使用存储在 RotationSpeed_ForEach 组件中的data更新对象的旋转。

using Unity.Entities;

using Unity.Mathematics;

using Unity.Transforms;

using UnityEngine;

// This system updates all entities in the scene with both a RotationSpeed_ForEach and Rotation component.

public class RotationSpeedSystem_ForEach : ComponentSystem

{
     protected override void OnUpdate()
     {
         // Entities.ForEach processes each set of ComponentData on the main thread. This is not the recommended
         // method for best performance. However, we start with it here to demonstrate the clearer separation
         // between ComponentSystem Update (logic) and ComponentData (data).
         // There is no update logic on the individual ComponentData.
         Entities.ForEach((ref RotationSpeed_ForEach rotationSpeed, ref Rotation rotation) =>
         {
             var deltaTime = Time.deltaTime;
             rotation.Value = math.mul(math.normalize(rotation.Value),
                 quaternion.AxisAngle(math.up(), rotationSpeed.RadiansPerSecond * deltaTime));
         });
     }

}

ComponentSystems和Entities.ForEach

RotationSpeedSystem_ForEach是一个ComponentSystem,它使用Entities.ForEach委托来遍历实体。
此示例仅创建单个实体,但如果向场景添加了更多实体,则RotationSpeedSystem_ForEach会更新所有实体 - 只要它们具有RotationSpeed_ForEach组件(并且在将GameObject的Transform转换为ECS组件时添加了旋转组件)。
请注意,使用Entities.ForEach的ComponentSystems在主线程上运行。
要利用多个内核,可以使用JobComponentSystem(如下一个HelloCube示例所示)。

从GameObject转换为Entity

ConvertToEntity MonoBehaviour在Awake时将GameObject及其子节点转换为实体和ECS组件。
目前,ConvertToEntity可以转换的内置Unity MonoBehaviours集包括Transform和MeshRenderer。
您可以使用实体调试器(菜单:窗口> 分析> 实体调试器)来检查转换创建的ECS实体和组件。
您可以在自己的MonoBehaviours上实现IConvertGameObjectEntity接口,以提供ConvertToEntity用于将存储在MonoBehavi中的数据转换为ECS组件的转换函数。
在此示例中, RotationSpeedAuthoring_ForEach MonoBehaviour使用IConvertGameObjectEntity在转换时将RotationSpeed_ForEach组件添加到实体。

using System;

using Unity.Entities;

using Unity.Mathematics;

using UnityEngine;

[RequiresEntityConversion]

public class RotationSpeedAuthoring_ForEach : MonoBehaviour, IConvertGameObjectToEntity

{
     public float DegreesPerSecond;

// The MonoBehaviour data is converted to ComponentData on the entity.
     // We are specifically transforming from a good editor representation of the data (Represented in degrees)
     // To a good runtime representation (Represented in radians)
     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
     {
         var data = new RotationSpeed_ForEach { RadiansPerSecond = math.radians(DegreesPerSecond) };
         dstManager.AddComponentData(entity, data);
     }

}


HelloCube_1b_ForEachWithEntityChanges

此示例演示了一个简单的ECS系统,该系统使用查询来选择要更新的正确实体集。然后它在ForEach lambda函数内部修改这些实体。

该示例定义了两个标记组件,名为MovingCube_ForEachWithEntityChanges和MoveUp_ForEachWithEntityChanges。

using System;
using Unity.Entities;

namespace Samples.HelloCube_1b
{
    // Serializable attribute is for editor support.
    [Serializable]
    public struct MoveUp_ForEachWithEntityChanges : IComponentData
    {
        // MoveUp is a "tag" component and contains no data. Tag components can be used to mark entities that a system should process.
    }
}

using System;
using Unity.Entities;

namespace Samples.HelloCube_1b
{
    // Serializable attribute is for editor support.
    [Serializable]
    public struct MovingCube_ForEachWithEntityChanges : IComponentData
    {
        // MovingCube_ForEachWithEntityChanges is a "tag" component and contains no data.
        // Tag components can be used to mark entities that a system should process.
    }
}

系统中的一个查询选择同时具有MoveUp_ForEachWithEntityChanges组件和Translation组件的所有实体。与此查询关联的ForEach lambda函数向上移动每个选定的实体,直到达到某个高度时,该函数将删除MoveUp_ForEachWithEntityChanges组件,以便下次系统更新时,将不会选择该实体,因此它不会向上移动任何更远的位置。

第二个查询选择具有Translation组件但没有MoveUp_ForEachWithEntityChanges组件的所有实体。与第二个查询关联的ForEach函数将实体向下移动到其起始位置,并添加新的MoveUp_ForEachWithEntityChanges组件。由于实体再次具有MoveUp_ForEachWithEntityChanges组件,因此下次系统更新时,实体将被第一个ForEach函数向上移动并被第二个查询跳过。

MovingCube_ForEachWithEntityChanges是一个标记组件,用于确保系统仅适用于为此示例标记的组件。示例中的两个查询都需要MovingCube_ForEachWithEntityChanges组件。

它显示了什么?

此示例演示了一个简单的ECS系统,该系统使用查询来选择要向上移动的一组实体。当它们达到一定高度时,系统会删除一个组件并使用另一个查询在较低的高度重新生成它们。它还演示了使用“标签”组件来提供选择具有待处理标记组件的特定entite组的方法。最后,此示例演示了如何在ForEach lambda函数内修改实体。

组件系统和实体.ForEach

MovementSystem_ForEachWithEntityChanges是一个ComponentSystem,它使用Entities.ForEach lambda函数迭代实体。此示例使用WithAll和WithNone约束来选择要处理的特定实体集。

using Unity.Entities;

using Unity.Mathematics;

using Unity.Transforms;

using UnityEngine;

namespace Samples.HelloCube_1b

{
     // This system updates all entities in the scene with Translation components.
     // It treats entities differently depending on whether or not they also have a MoveUp component.
     public class MovementSystem_ForEachWithEntityChanges : ComponentSystem
     {
         protected override void OnUpdate()
         {
             // If a MoveUp component is present, then the system updates the Translation component to move the entity upwards.
             // Once the entity reaches a predetermined height, the function removes the MoveUp component.
             Entities.WithAllReadOnly<MovingCube_ForEachWithEntityChanges, MoveUp_ForEachWithEntityChanges>().ForEach(
                 (Entity id, ref Translation translation) =>
                 {
                     var deltaTime = Time.deltaTime;
                     translation = new Translation()
                     {
                         Value = new float3(translation.Value.x, translation.Value.y + deltaTime, translation.Value.z)
                     };

if (translation.Value.y > 10.0f)
                         EntityManager.RemoveComponent<MoveUp_ForEachWithEntityChanges>(id);
                 }
             );

// If an entity does not have a MoveUp component (but does have a Translation component),
             // then the system moves the entity down to its starting point and adds a MoveUp component.
             Entities.WithAllReadOnly<MovingCube_ForEachWithEntityChanges>().WithNone<MoveUp_ForEachWithEntityChanges>().ForEach(
                 (Entity id, ref Translation translation) =>
                 {
                     translation = new Translation()
                     {
                         Value = new float3(translation.Value.x, -10.0f, translation.Value.z)
                     };

EntityManager.AddComponentData(id, new MoveUp_ForEachWithEntityChanges());
                 }
             );
         }
     }

}

注意:使用Entities.ForEach的组件系统在主线程上运行。要利用多个内核,可以使用JobComponentSystem(如其他HelloCube示例所示)。这也允许更改ForEach lambda函数内的实体。

HelloCube:ForEach的更多相关文章

  1. Java:foreach实现原理

    第一部分: For-each Loop Purpose The basic for loop was extended in Java5 to make iteration over arrays a ...

  2. 编写高质量代码改善C#程序的157个建议——建议18:foreach不能代替for

    建议18:foreach不能代替for 上一个建议中提到了foreach的两个优点:语法更简单,默认调用Dispose方法,所有我们强烈建议在实际的代码编写中更多的使用foreach.但是,该建议也有 ...

  3. (JavaScript基础向)日常小发现:forEach等函数的第二个参数的用法

    forEach函数用得平时用得比较多,但是从来没想到forEach函数还有第二个参数. 这里是菜鸟教程对forEach函数的详细说明:forEach的详细说明. 如上图,forEach函数有第二个参数 ...

  4. C#:foreach语句,yield语句

    原文:C#:foreach语句,yield语句 1. foreach语句 C#编译器会把foreach语句转换为IEnumerable接口的方法和属性. foreach (Person p in pe ...

  5. 如何让类数组也使用数组的方法比如:forEach()

    思路: 让类数组绑定数组的方法<div>1</div><div>2</div>方法一: let div = document.getElementsBy ...

  6. Qt:foreach

    0.说明 Qt提供一个关键字foreach(实际上是<QtGlobal>中定义的一个宏)用于方便地访问容器中的所有数据项. foreach关键字用于遍历容器中的所有数据项 注意 forea ...

  7. php学习笔记:foreach循环访问关联数组里的值

    foreach循环可以将数组里的所有值都访问到,下面我们展示下,用foreach循环访问关联数组里的值. 例如: $fruit=array('apple'=>"苹果",'ba ...

  8. 第46条:for-each循环优先于传统的for循环

    for-each循环通过完全隐藏迭代器或者索引变量,避免混乱和出错的可能,适用于集合和数组和任何实现Iterable接口的对象. 使用传统for循环,容易出错: enum Face { ONE, TW ...

  9. 空数组在以下三种遍历中均不可更改:forEach、map和for...in

    首先,我们要知道对于forEach.map和for...in三种遍历,在不是空数组的情况下,要想实现更改原数组的方法,代码如下: var list = [1,2,3,4]; var list1 = [ ...

随机推荐

  1. 2019牛客暑期多校训练营(第七场)D Number——实系数多项式因式分解定理

    前置知识 代数基本定理 定理:每个次数 ≥ 1 复系数多项式在复数域中至少有一个跟. 由此推出,n次复系数多项式方程在复数域内有且只有n个根(重根按重数计算).(只要不断把多项式除以(x-xa),即可 ...

  2. Sql Server中REPLACE函数的使用

    在sql的使用过程当中,我们偶尔会遇到这样一种情况,就是需要改变数据的存储形式,比如数据库某一张表(Info)当中有一个字段Educational(学历),以前存储的是Json数组,现在由于需求的改变 ...

  3. js.map文件意义(转)

    什么是source map文件 source map文件是js文件压缩后,文件的变量名替换对应.变量所在位置等元信息数据文件,一般这种文件和min.js主文件放在同一个目录下. 比如压缩后原变量是ma ...

  4. 关系型数据库与NoSQL的对比

    SQL(结构化的查询语言)数据库是过去四十年间存储数据的主要方式.20世纪90年代末随着Web应用和MySQL.PostgreSQL和SQLite等开源数据库的兴起,用户爆炸式的增长. NoSQL数据 ...

  5. wait系列

    转自 http://blog.csdn.net/todd911/article/details/15028511 1.wait函数和waitpid函数 当一个进程正常或异常终止时,内核就向其父进程发送 ...

  6. Spring注解驱动——组件注册系列

    1.@Configuration 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被Annot ...

  7. 以字符串为例,谈谈Python到底要学到什么程度

    古语云:慈不掌兵,义不为商:离商业越近,离人性越远:我们在自学数据科学时,一定会辅助一些书籍或者视频来学习,怎么学习,选择哪些资料来学习?这时,我们都要理解好第一句话,理解不好,浪费钱是次要,重要的是 ...

  8. 下载 nasm for win64

    下载nasm https://www.nasm.us/pub/nasm/releasebuilds/?C=M;O=D 以管理员身份运行安装.

  9. POJ 3342 Party at Hali-Bula ——(树型DP)

    一开始用pii保存dp类型,写的很长,还是WA了= =.. 然后参考了一下别人的博客,重新写了一发(似乎是岐哥的博客233). 代码如下: #include <stdio.h> #incl ...

  10. eclipse将web项目部署到tomcat

    在 eclipse 中,选择 Window--->Preferences--->Server--->Runtime Environments,选择 Add 按钮 在弹出的对话框中,选 ...