【Unity3D】ECS入门学习(八)块组件 ArchetypeChunk

全局唯一组件,修改数值后不会产生新的组件,而是全部实体的块组件内容都同步变化。

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
public struct MyChunkComponentData : IComponentData
{
    public int num;
}
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
public class ChunkTest : MonoBehaviour
{
    private void Start()
    {
        EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
        //创建Chunk类型
        EntityArchetype type = entityManager.CreateArchetype(ComponentType.ChunkComponent(typeof(MyChunkComponentData)));
        //创建Chunk实体1
        Entity chunkEntity = entityManager.CreateEntity(type);
        //创建Chunk实体2
        Entity chunkEntity2 = entityManager.CreateEntity(type);
        //获取实体1的Chunk类对象
        ArchetypeChunk chunk = entityManager.GetChunk(chunkEntity);
        //修改Chunk值
        entityManager.SetChunkComponentData(chunk, new MyChunkComponentData() { num = 10 });
    }
}

修改其中一个Chunk对象的值其他实体的Chunk组件也会同步修改。

相关推荐
别动我齐刘海8 小时前
C++ 工程化学习
java·linux·前端·c++·学习·机器学习·机器人
gjf05_0512 小时前
人该怎样活着呢?版本75.8
学习
li星野13 小时前
【学习记录】PCB与PCBA核心知识全解析:从设计到制造的关键细节
学习·制造
流浪打工人13 小时前
Unity3d TimeLine中audio track的使用
unity
卿卿的产品经理日记14 小时前
【AI产品经理实战】Day 35|从“沉迷工具“到回归 AI 底层:Skill 收官、系统复盘与下一个 13 天
人工智能·学习·产品经理
一条泥憨鱼14 小时前
【从0开始学习计算机网络】| ARP 协议:地址解析,ARP 欺骗,链路层寻址
网络·后端·学习·计算机网络
HY小宝F15 小时前
树莓派智能摄像头系统 30 天重学计划之 Day 04 · HTTP / API:控制链入口 —— 学习总结
网络协议·学习·http
悟乙己16 小时前
AFAC2026 赛题四金融长文本 Agent竞赛学习笔记:赛题解析(一)
笔记·学习·金融
淡海水16 小时前
02-07-原理篇-GC暂停时间的本质
开发语言·unity·c#·gc·暂停时间
霍霍的袁16 小时前
【C++】哈希表实现指南:从原理到开放定址法、链地址法的实现
c++·学习·哈希算法·散列表·visual studio