Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by GravityL · May 09, 2020 at 11:08 PM · dynamicdotsbuffer

How to use multiple DynamicBuffer in one entity

I need to use multiple DynamicBuffers for my entity but im getting errors because the second one redefines the type of the first

Heres the error:

     InvalidOperationException: Attempted to access ArchetypeChunkBufferType<Block> which has been invalidated by a structural change.

And the code:

 private void Start(){
         heightMap = new float[xSize,zSize];
         heightMap2 = new float[xSize,zSize];
 
         EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
 
         EntityArchetype entityArchetype = entityManager.CreateArchetype(
             typeof(Translation),
             typeof(Chunk),
             typeof(Heightmap),
             typeof(RenderMesh),
             typeof(LocalToWorld),
             typeof(RenderBounds)
         );
 
         Entity entity = entityManager.CreateEntity(entityArchetype);
 
         DynamicBuffer<Block> dynamicBuffer =  entityManager.AddBuffer<Block>(entity);
 
         DynamicBuffer<VertexBuffer> dynamicVertexBuffer = entityManager.AddBuffer<VertexBuffer>(entity);
 
         for (int x = 0; x < xSize; x++)
             for (int y = 0; y < ySize; y++)
                 for (int z = 0; z < zSize; z++)
                 {
 
                     float simplex1 = noise.GetSimplex(x * .8f, z * .8f) * 10;
                     float simplex2 = noise.GetSimplex(x * 3f, z * 3f) * 10 * (noise.GetSimplex(x * .3f, z * .3f) + .5f);
                     float heightMap = simplex1 + simplex2;
                     float dirtHeight = ySize * .5f + heightMap;
 
                     dynamicBuffer.Add(new Block
                     {
                         material = y > dirtHeight ? BlockMaterials.air : BlockMaterials.dirt
                     });
 
                 }
 
         for (int x = 0; x <= xSize; x++)
             for (int y = 0; y <= ySize; y++)
                 for (int z = 0; z <= zSize; z++)
                 {
                     dynamicVertexBuffer.Add(new VertexBuffer { vertex = new Vector3(x, y, z) });
                 }
 
         entityManager.SetComponentData(entity, new ChunkComponent
         {
             xSize = xSize,
             ySize = ySize,
             zSize = zSize
         });
 
         //entityManager.SetComponentData(entity, new NativeArray<Vector3>.CopyFrom(vertex));
 
     }




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Jul 24, 2021 at 08:43 PM

Use of EntityCommandBuffer fixes this issue.

MyScript.cs

 using UnityEngine;
 using Unity.Mathematics;
 using Unity.Transforms;
 using Unity.Entities;
 using Unity.Collections;
 using Unity.Rendering;
 
 public class MyScript : MonoBehaviour
 {
     [SerializeField] int _width = 10, _height = 10, _depth = 10;
     [SerializeField] Mesh _mesh = null;
     [SerializeField] Material _material = null;
     void Start ()
     {
         var ecb = new EntityCommandBuffer( Allocator.Temp );
         EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
         EntityArchetype archetype = entityManager.CreateArchetype(
                 typeof(Translation)
             ,   typeof(Chunk)
             ,   typeof(ChunkComponent)
             ,   typeof(Heightmap)
             ,   typeof(RenderMesh)
             ,   typeof(LocalToWorld)
             ,   typeof(RenderBounds)
             ,   typeof(VertexBuffer)
             ,   typeof(Block)
         );
 
         Entity entity = entityManager.CreateEntity( archetype );
         entityManager.SetSharedComponentData( entity , new RenderMesh{
             mesh        = _mesh ,
             material    = _material ,
         } );
 
         for( int x=0 ; x<_width ; x++ )
         for( int y=0 ; y<_height ; y++ )
         for( int z=0 ; z<_depth ; z++ )
         {
             float simplex1 = noise.snoise(new float2(x*.8f,z*.8f)) * 10;
             float simplex2 = noise.snoise(new float2(x*3f,z*3f)) * 10 * (noise.snoise(new float2(x*.3f,z*.3f)) + .5f);
             float height = simplex1 + simplex2;
             float dirtHeight = _height * .5f + height;
 
             ecb.AppendToBuffer( entity , new Block{
                 material = y>dirtHeight ? (byte) 0 : (byte) 1
             } );
         }
 
         for( int x=0 ; x<=_width ; x++ )
         for( int y=0 ; y<=_height ; y++ )
         for( int z=0 ; z<=_depth ; z++ )
         {
             ecb.AppendToBuffer( entity , new VertexBuffer{
                 vertex = new Vector3(x,y,z)
             } );
         }
 
         ecb.SetComponent( entity , new ChunkComponent{
             xSize = _width,
             ySize = _height,
             zSize = _depth
         } );
 
         ecb.Playback( entityManager );
     }
 
     struct Chunk : IComponentData
     {
 
     }
     struct ChunkComponent : IComponentData
     {
         public int xSize, ySize, zSize;
     }
     struct Heightmap : IComponentData
     {
 
     }
     struct VertexBuffer : IBufferElementData
     {
         public Vector3 vertex;
     }
     struct Block : IBufferElementData
     {
         public byte material;
     }
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

201 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

when using Dynamic Buffers(ECS), How can you add two variables 2 Answers

Creating a dynamic mesh with audio = crash after 30secs ? 0 Answers

Cannot get 3D Text/Text Mesh to wrap or a attached collider used as a button to scale based on word count. 2 Answers

Creating a dynamic alpha based on cursor/object position 0 Answers

How to obtain third person accuracy 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges