Beispiel #1
0
        public static byte[] Serialize(this BulkBatch batch)
        {
            var size = Marshal.SizeOf(batch);
            var arr  = new byte[size];
            var ptr  = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(batch, ptr, true);
            Marshal.Copy(ptr, arr, 0, size);
            Marshal.FreeHGlobal(ptr);

            return(arr);
        }
Beispiel #2
0
        public static BulkBatch Deserialize(byte[] data)
        {
            var batch = new BulkBatch();
            var size  = Marshal.SizeOf(batch);
            var ptr   = Marshal.AllocHGlobal(size);

            Marshal.Copy(data, 0, ptr, size);

            batch = (BulkBatch)Marshal.PtrToStructure(ptr, batch.GetType());
            Marshal.FreeHGlobal(ptr);

            return(batch);
        }
Beispiel #3
0
        public void CreateBatches(List <UnitBase> units)
        {
            if (m_stopProcess)
            {
                return;
            }

            foreach (var stateChange in m_stateChanges)
            {
                units[stateChange].gameObject.SetActive(false);
            }

            m_stateChanges = new Queue <int>();

            if (Time.time > m_timeTillNextFlush)
            {
                var  count     = 0;
                byte currentId = 1;

                var batch = new BulkBatch
                {
                    Id       = currentId,
                    Position = new float[BatchSize],
                    Rotation = new float[BatchSize],
                };

                foreach (var unit in units)
                {
                    var packedPos = 0f;
                    var packedRot = 0f;

                    //set data only for active objects
                    if (unit.gameObject.activeInHierarchy)
                    {
                        var position = unit.transform.position;
                        var rotation = unit.transform.rotation.eulerAngles;
                        packedPos = Compression.PackVector3(position, m_minPosition, m_maxPosition);
                        packedRot = Compression.PackVector3(rotation, m_minAngle, m_maxAngle);
                    }

                    batch.Position[count] = packedPos;
                    batch.Rotation[count] = packedRot;

                    if (count == BatchSize - 1 || count == units.Count - 1)
                    {
                        var arr = batch.Serialize();
                        m_outgoingData.Enqueue(arr);
                        m_queuedBatches++;

                        currentId++;
                        count = -1;

                        batch = new BulkBatch
                        {
                            Id       = currentId,
                            Position = new float[BatchSize],
                            Rotation = new float[BatchSize],
                        };
                    }

                    count++;
                }

                m_batchesInRow       = units.Count / BatchSize;
                m_batchProcressRate  = Mathf.FloorToInt(m_maxSendRate * m_utilizeSendRatePercentage);
                m_batchProcressRate -= m_batchesInRow;
                m_timeTillNextFlush  = (1f / (Mathf.Max(m_batchProcressRate, 0))) + Time.time;
            }
        }