Unity组件——ECS常见组件/API理解及使用
创始人
2024-06-02 08:43:47
0

声明:本文为个人笔记,用于学习研究使用非商用,内容为个人研究及综合整理所得,若有违规,请联系,违规必改。


Unity组件——ECS常见组件/API理解及使用


文章目录

  • Unity组件——ECS常见组件/API理解及使用
  • 一、ECS常见组件
  • 二、实现
    • 1.ComponentSystem:系统组件,包含 Update
    • 2.IComponentData:实体数据组件
    • 3.IConvertGameObjectToEntity:实现接口 对该物体进行转换实体操作.
    • 4.ConvertToEntity:挂载物体身上,将物体转换为实体
      • 4.1.Translation:对坐标操控
    • 5.[DisableAutoCreation] :不自动创建系统
    • 6.创建预制体
    • 7.通过EntityManager修改组件的值
  • 三、总结


一、ECS常见组件

系统组件、创建预制体、实体数据组件及对其操作;
注:引用命名空间 Unity.Entities


二、实现

1.ComponentSystem:系统组件,包含 Update

思考:类比Unity开发中脚本继承MonoBehaviour组件

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Unity.Entities;namespace Test1
{public class PrintSystem : ComponentSystem{protected override void OnUpdate(){Entities.ForEach((ref PrintComponyData pPrintComponentData) =>{Debug.Log(pPrintComponentData.printData);});}}
}

2.IComponentData:实体数据组件

思考:需要在结构体下编辑,类似Unity自己封装的Vector3

using Unity.Entities;namespace Test1
{public struct PrintComponyData : IComponentData{public float printData;}
}

3.IConvertGameObjectToEntity:实现接口 对该物体进行转换实体操作.

思考:给实体添加一个"属性"/将属性变为"实体的属性".

using UnityEngine;
using Unity.Entities;namespace Test1
{public class PrintAutoring :MonoBehaviour,  IConvertGameObjectToEntity{public float printData;public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem){dstManager.AddComponentData(entity, new PrintComponyData() { printData = printData });}}
}

4.ConvertToEntity:挂载物体身上,将物体转换为实体

4.1.Translation:对坐标操控

public class TranslationSystem : ComponentSystem
{protected override void OnUpdate(){Entities.ForEach((ref Translation pTranslationComponentData) =>{pTranslationComponentData.Value = new float3(0, 0.1f, 0.5f);});}
}

5.[DisableAutoCreation] :不自动创建系统

using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;namespace Test2
{[DisableAutoCreation]public class TranslationSystem : ComponentSystem{protected override void OnUpdate(){Entities.ForEach((ref Translation pTranslationComponentData) =>{pTranslationComponentData.Value = new float3(0, 0.1f, 0.5f);});}}
}

6.创建预制体

using Unity.Entities;
using UnityEngine;public class ECSPrefabCreator_MZ : MonoBehaviour
{public GameObject cube;private void Start(){//获取当前世界设置cube = Resources.Load("Cube");GameObjectConversionSettings tempSettings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, null);//获取实体预制体Entity tempEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(cube, tempSettings);EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;Entity tempCube = entityManager.Instantiate(tempEntityPrefab);}
}

7.通过EntityManager修改组件的值

using Unity.Entities;
using Unity.Transforms;
using UnityEngine;public class ECSPrefabCreator_MZ : MonoBehaviour
{public GameObject cube;public float interval, sum;private void Start(){//获取当前世界设置cube = Resources.Load("Cube");GameObjectConversionSettings tempSettings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, null);//获取实体预制体Entity tempEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(cube, tempSettings);EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;Translation tempTransla = new Translation();tempTransla.Value = -interval;for (int i = 0; i < sum; i++){for (int j = 0; j < sum; j++){Entity tempCube = entityManager.Instantiate(tempEntityPrefab);tempTransla.Value.x += interval;//通过EntityManager修改组件的值entityManager.SetComponentData(tempCube, tempTransla);}tempTransla.Value.x = -interval;tempTransla.Value.y += interval;}}
}

三、总结

保持饥饿,保持愚蠢.

这世界唯一能够相信的就是你付出的努力和你走过的路.

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...