Unity ECS GameObject转化为Entity(一)

一、实现IConvertGameObjectToEntity接口IConvertGameObjectToEntity

using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;

[System.Serializable]
[RequireComponent(typeof(ConvertToEntity))]
[RequiresEntityConversion]
public class CubeDataSource : MonoBehaviour, IConvertGameObjectToEntity
{
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        dstManager.AddComponentData(entity, new Translation() { Value = new float3(1,1,1)});
    }
}

Unity ECS GameObject转化为Entity(一)_第1张图片     Unity ECS GameObject转化为Entity(一)_第2张图片  

 

二、使用 GameObjectConversionUtility.ConvertGameObjectHierarchy

using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;

public class CubeCreate : MonoBehaviour
{
    void Start()
    {
        GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, null);

        Entity cubeEntity = GameObjectConversionUtility.ConvertGameObjectHierarchy(gameObject, settings);

        World.DefaultGameObjectInjectionWorld.EntityManager.AddComponentData(cubeEntity,new Translation { Value = new float3(1,1,1)});

        Destroy(gameObject);
    }
}

Unity ECS GameObject转化为Entity(一)_第3张图片       Unity ECS GameObject转化为Entity(一)_第4张图片         

你可能感兴趣的:(Unity3D)