Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Unitys generic method for buffering components
- internal void AddEntityComponentCommand<T>(EntityCommandBufferChain* chain, int sortKey, ECBCommand op, Entity e, T component) where T : struct, IComponentData
- {
- var ctype = ComponentType.ReadWrite<T>();
- if (ctype.IsZeroSized)
- {
- AddEntityComponentTypeCommand(chain, sortKey, op, e, ctype);
- return;
- }
- // NOTE: This has to be sizeof not TypeManager.SizeInChunk since we use UnsafeUtility.CopyStructureToPtr
- // even on zero size components.
- var typeSize = UnsafeUtility.SizeOf<T>();
- var sizeNeeded = Align(sizeof(EntityComponentCommand) + typeSize, 8);
- ResetCommandBatching(chain);
- var cmd = (EntityComponentCommand*)Reserve(chain, sortKey, sizeNeeded);
- cmd->Header.Header.CommandType = (int)op;
- cmd->Header.Header.TotalSize = sizeNeeded;
- cmd->Header.Header.SortKey = chain->m_LastSortKey;
- cmd->Header.Entity = e;
- cmd->ComponentTypeIndex = ctype.TypeIndex;
- cmd->ComponentSize = typeSize;
- byte* data = (byte*)(cmd + 1);
- UnsafeUtility.CopyStructureToPtr(ref component, data);
- if (RequiresEntityFixUp(data, ctype.TypeIndex))
- {
- if (op == ECBCommand.AddComponent)
- cmd->Header.Header.CommandType = (int)ECBCommand.AddComponentWithEntityFixUp;
- else if (op == ECBCommand.SetComponent)
- cmd->Header.Header.CommandType = (int)ECBCommand.SetComponentWithEntityFixUp;
- }
- }
- My non generic version for boxed structs
- /// <summary>
- /// An internal method which adds an entity component without generics.
- /// </summary>
- /// <param name="ecbd"></param>
- /// <param name="chain"></param>
- /// <param name="sortKey"></param>
- /// <param name="op"></param>
- /// <param name="e"></param>
- /// <param name="component"></param>
- internal static void AddEntityComponentCommand(this EntityCommandBufferData ecbd, EntityCommandBufferChain* chain, int sortKey, ECBCommand op, Entity e, object component){
- var ctype = ComponentType.ReadWrite(component.GetType());
- if (ctype.IsZeroSized) {
- ecbd.AddEntityComponentTypeCommand(chain, sortKey, op, e, ctype);
- return;
- }
- // NOTE: This has to be sizeof not TypeManager.SizeInChunk since we use UnsafeUtility.CopyStructureToPtr
- // even on zero size components.
- var typeSize = UnsafeUtility.SizeOf(component.GetType());
- var sizeNeeded = EntityCommandBufferData.Align(sizeof(EntityComponentCommand) + typeSize, 8);
- ecbd.ResetCommandBatching(chain);
- var cmd = (EntityComponentCommand*)ecbd.Reserve(chain, sortKey, sizeNeeded);
- cmd->Header.Header.CommandType = (int)op;
- cmd->Header.Header.TotalSize = sizeNeeded;
- cmd->Header.Header.SortKey = chain->m_LastSortKey;
- cmd->Header.Entity = e;
- cmd->ComponentTypeIndex = ctype.TypeIndex;
- cmd->ComponentSize = typeSize;
- byte* data = (byte*)(cmd + 1);
- Marshal.StructureToPtr(component, new IntPtr(data), false);
- if (ecbd.RequiresEntityFixUp(data, ctype.TypeIndex))
- {
- if (op == ECBCommand.AddComponent)
- cmd->Header.Header.CommandType = (int)ECBCommand.AddComponentWithEntityFixUp;
- else if (op == ECBCommand.SetComponent)
- cmd->Header.Header.CommandType = (int)ECBCommand.SetComponentWithEntityFixUp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment