声明:本文为个人笔记,用于学习研究使用非商用,内容为个人研究及综合整理所得,若有违规,请联系,违规必改。
Unity组件——ECS常见组件/API理解及使用
文章目录
一、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<GameObject>("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<GameObject>("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;
}
}
}
三、总结
保持饥饿,保持愚蠢.
这世界唯一能够相信的就是你付出的努力和你走过的路.