ECS入门 3.SpawnFromMonoBehaviour

前言

此示例演示如何使用预制件游戏对象生成实体和组件。该场景生成一对旋转立方体的“场”。

它显示了什么?

Unity.Entities 提供游戏对象转换实用程序,用于将游戏对象层次结构转换为其实体表示形式。使用此实用程序,您可以将预制件转换为实体表示形式,并在需要时使用该表示形式生成新实例。
实例化实体预制件时,整个预制件表示形式将作为一个组进行克隆,其方式与基于游戏对象实例化预制件的方式相同。
Spawner_FromMonoBehaviour类在 Start() 方法中将预制件转换为其实体表示形式,然后实例化旋转对象的字段。

项目

场景布置

创建一个预制体
创建一个预制体

场景布置
场景布置

代码编写

创建两个脚本
Spawner_FromMonoBehaviour.cs

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

// ReSharper disable once InconsistentNaming
[AddComponentMenu("DOTS Samples/SpawnFromMonoBehaviour/Spawner")]
public class Spawner_FromMonoBehaviour : MonoBehaviour
{
    
    
    public GameObject Prefab;
    public int CountX = 100;
    public int CountY = 100;

    void Start()
    {
    
    
        // Create entity prefab from the game object hierarchy once
        // 创建实体预制体的游戏对象
        var settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, null);
        var prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab, settings);
        var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

        for (var x = 0; x < CountX; x++)
        {
    
    
            for (var y = 0; y < CountY; y++)
            {
    
    
                // Efficiently instantiate a bunch of entities from the already converted entity prefab
                var instance = entityManager.Instantiate(prefab);

                // Place the instantiated entity in a grid with some noise
                var position = transform.TransformPoint(new float3(x * 1.3F, noise.cnoise(new float2(x, y) * 0.21F) * 2, y * 1.3F));
                entityManager.SetComponentData(instance, new Translation {
    
    Value = position});
            }
        }
    }
}


RotationSpeed_ForEach

using System;
using Unity.Entities;

[GenerateAuthoringComponent]
public struct RotationSpeed_ForEach : IComponentData
{
    
    
    public float RadiansPerSecond;
}

添加并设置脚本

给父物体添加RotationSpeed_ForEach脚本,并设置Radians Per Second的值为2
设置脚本
在场景中添加并设置
设置

总结

// 初始设置
var settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, null);
var prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab, settings);
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;


//生成到场景
var instance = entityManager.Instantiate(prefab);

// Place the instantiated entity in a grid with some noise
var position = transform.TransformPoint(new float3(x * 1.3F, noise.cnoise(new float2(x, y) * 0.21F) * 2, y * 1.3F));
// 设置组件内容
entityManager.SetComponentData(instance, new Translation {
    
    Value = position});

===========================Blob=============================
//首先,声明一个结构。之后,声明一个临时分配的“构建器”。
BlobBuilder builder = new BlobBuilder(Allocator.Temp);
//然后你设计你的结构,从你在结构中使用的数据方案开始。
ref MyStruct myStruct = ref builder.ConstructRoot<MyStruct>();
        
//最后,您设置 BLOB 资产参考并持续分配它。
BlobAssetReference<MyStruct> myBlobAssetReference = builder.CreateBlobAssetReference<MyStruct>(Allocator.Persistent);

//添加组件数据
entityManager.AddComponentData(entity, new MyPointReferece {
    
     Value = myBlobAssetReference });
builder.Dispose();

运行效果

运行效果

猜你喜欢

转载自blog.csdn.net/a71468293a/article/details/127538194
ECS