Unity ECS 基础教学(三)

Unity ECS 基础教学(三)

OK 老规矩,直接上图片上代码:

组件搭载:
注意预制体、脚本搭载。

在这里插入图片描述

Entity 组件属性

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Collections;

//[GenerateAuthoringComponent]特性:将该物体转化为实体 且有了这个特性,才能挂载到物体上。否则因为没有继承Mono,不能搭载。
[GenerateAuthoringComponent]
public struct  CreateEntityComponentData_ZH : IComponentData
{
    [Header("生成Entity 数量")]
    public int _CreateNum;
    [Header("Entity 预制体")]
    public Entity _EntityPrefab01;
    [Header("Entity 预制体")]
    public Entity _EntityPrefab02;
    [Header("预制体序号")]
    public int _EntityInt;

}

GameObject 预制体 转换 Entity 预制体

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;

/// <summary>
/// 类型转换
/// </summary>

public class PrefabToEntity_ZH : MonoBehaviour,IConvertGameObjectToEntity,IDeclareReferencedPrefabs
{

    [Header("GameObject 预制体")]
    public GameObject _GameObjectPrefab;
    [Header("GameObject 预制体")]
    public GameObject _GameObjectTempPrefab;
    // 预制体 Entity
    public static Entity _EntityPrefab;
    public static Entity _EntityTempPrefab;

    /// <summary>
    ///  类型转换
    /// </summary>
    /// <param 实体="entity"></param>
    /// <param Entity管理="dstManager"></param>
    /// <param 转换系统="conversionSystem"></param>
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        BlobAssetStore _BlobAssetStore = new BlobAssetStore();
        GameObjectConversionSettings _Settines = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, _BlobAssetStore);
        //转换
        _EntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(_GameObjectPrefab, _Settines);


        //GameObject 物体转换成 Entity物体
        Entity _EntityTemp = conversionSystem.GetPrimaryEntity(_GameObjectTempPrefab);
        //Entity物体 转换成 EntityPrefab 物体
        _EntityTempPrefab = _EntityTemp;
    }

    /// <summary>
    /// 类型转换
    /// </summary>
    /// <param 引用预制体数组="referencedPrefabs"></param>
    public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
    {
        referencedPrefabs.Add(_GameObjectTempPrefab);
    }
}

Entity 实体 不同的生成方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Jobs;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
using Random = UnityEngine.Random;

/// <summary>
/// ECS 架构方法
/// </summary>

public class CreateEntityComponentSystem_ZH : ComponentSystem
{
    /// <summary>
    /// 类似于 Start 方法
    /// </summary>
    protected override void OnStartRunning()
    {
        //-------------------- Entity 实体创建 方法01  --------------------  

        //数据转换
        CreateEntityComponentData_ZH _CreateEntityComponentData02 = GetSingleton<CreateEntityComponentData_ZH>();

        for (int i = 0; i < _CreateEntityComponentData02._CreateNum; i++)
        {
            //物体生成
            Entity _MyEntity = EntityManager.Instantiate(_CreateEntityComponentData02._EntityPrefab01);

            //位置赋值
            float3 _Postion = new float3(Random.Range(-3, 3), Random.Range(-3, 3), Random.Range(-3, 3));
            EntityManager.AddComponentData(_MyEntity, new Translation { Value = _Postion });
        }
    }

    /// <summary>
    /// 类似于 Update 方法
    /// </summary>
    protected override void OnUpdate()
    {
        CreateEntity();
    }

    /// <summary>
    /// Entity 实体创建
    /// </summary>
    public void CreateEntity()
    {
        //-------------------- Entity 实体创建 方法02  --------------------  

        //一次性生成多个 Entity 物体
        Entities.ForEach((ref NewEntityData_ZH _NewEntityData) =>
        {
            //游戏运行时间小于 10秒
            if (Time.ElapsedTime < 10.0f)
            {
                //创建 Entity
                Entity _MyEntity = EntityManager.Instantiate(_NewEntityData._MyEntityPrefab);
                //位置赋值
                float3 _Postion01 = new float3(Random.Range(-3, 3), Random.Range(-3, 3), Random.Range(-3, 3));
                EntityManager.AddComponentData(_MyEntity, new Translation { Value = _Postion01 });
            }

        });

        //-------------------- Entity 实体创建 方法03  --------------------  

        //循环遍历  生成
        Entities.ForEach((ref CreateEntityComponentData_ZH _CreateEntityComponentData) =>
        {
            if (_CreateEntityComponentData._CreateNum > 0)
            {
                for (int i = 0; i < 100; i++)
                {
                    //序号生成
                    switch (_CreateEntityComponentData._EntityInt)
                    {
                        case 1:
                            //物体生成
                            Entity _MyEntity01 = EntityManager.Instantiate(_CreateEntityComponentData._EntityPrefab01);
                            //数量减少

                            //位置赋值
                            float3 _Postion01 = new float3(Random.Range(-3, 3), Random.Range(-3, 3), Random.Range(-3, 3));
                            EntityManager.AddComponentData(_MyEntity01, new Translation { Value = _Postion01 });
                            break;

                        case 2:
                            //物体生成
                            Entity _MyEntity02 = EntityManager.Instantiate(_CreateEntityComponentData._EntityPrefab02);
                            //位置赋值
                            float3 _Postion02 = new float3(Random.Range(-3, 3), Random.Range(-3, 3), Random.Range(-3, 3));
                            EntityManager.AddComponentData(_MyEntity02, new Translation { Value = _Postion02 });
                            break;

                        case 3:
                            //物体生成
                            Entity _MyEntity03 = EntityManager.Instantiate(PrefabToEntity_ZH._EntityPrefab);
                            //位置赋值
                            float3 _Postion03 = new float3(Random.Range(-3, 3), Random.Range(-3, 3), Random.Range(-3, 3));
                            EntityManager.AddComponentData(_MyEntity03, new Translation { Value = _Postion03 });
                            break;

                        case 4:
                            //物体生成
                            Entity _MyEntity04 = EntityManager.Instantiate(PrefabToEntity_ZH._EntityTempPrefab);
                            //位置赋值
                            float3 _Postion04 = new float3(Random.Range(-3, 3), Random.Range(-3, 3), Random.Range(-3, 3));
                            EntityManager.AddComponentData(_MyEntity04, new Translation { Value = _Postion04 });
                            break;

                        default:
                            break;
                    }
                }
                //数量减少
                _CreateEntityComponentData._CreateNum--;
            }
        });
    }
}

最终效果:
至于这里为什么会生成 309 个 Entity 实体就自己去观察一下吧。

在这里插入图片描述

暂时先这样吧,如果有时间的话就会更新,如果实在看不明白就留言,看到我会回复的。

路长远兮,与君共勉。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Maddie_Mo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值