声明:本文为个人笔记,用于学习研究使用非商用,内容为个人研究及综合整理所得,若有违规,请联系,违规必改。
系统组件、创建预制体、实体数据组件及对其操作;
注:引用命名空间 Unity.Entities
思考:类比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);});}}
}
思考:需要在结构体下编辑,类似Unity自己封装的Vector3
using Unity.Entities;namespace Test1
{public struct PrintComponyData : IComponentData{public float printData;}
}
思考:给实体添加一个"属性"/将属性变为"实体的属性".
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 });}}
}
public class TranslationSystem : ComponentSystem
{protected override void OnUpdate(){Entities.ForEach((ref Translation pTranslationComponentData) =>{pTranslationComponentData.Value = new float3(0, 0.1f, 0.5f);});}
}
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);});}}
}
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);}
}
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;}}
}
保持饥饿,保持愚蠢.
这世界唯一能够相信的就是你付出的努力和你走过的路.