public static ProfilerContainer FromBytes(byte[] bytes)
            {
                ProfilerContainer container = new ProfilerContainer();
                BitStream         stream    = new BitStream(bytes);
                BitReader         reader    = new BitReader(stream);
                ushort            count     = reader.ReadUInt16Packed();

                container.ticks = new ProfilerTick[count];
                for (int i = 0; i < count; i++)
                {
                    container.ticks[i] = ProfilerTick.FromStream(stream);
                }

                return(container);
            }
Esempio n. 2
0
            public static ProfilerContainer FromBytes(byte[] bytes)
            {
                var container = new ProfilerContainer();
                var buffer    = new NetworkBuffer(bytes);
                var reader    = new NetworkReader(buffer);
                var count     = reader.ReadUInt16Packed();

                container.Ticks = new ProfilerTick[count];

                for (int i = 0; i < count; i++)
                {
                    container.Ticks[i] = ProfilerTick.FromStream(buffer);
                }

                return(container);
            }
Esempio n. 3
0
        private void OnGUI()
        {
            bool  recording = NetworkProfiler.IsRunning;
            float deltaTime = (float)(EditorApplication.timeSinceStartup - m_LastSetup);

            m_LastSetup = EditorApplication.timeSinceStartup;

            //Draw top bar
            EditorGUILayout.BeginVertical();
            EditorGUILayout.BeginHorizontal();

            if (GUILayout.Button(recording ? "Stop" : "Capture"))
            {
                ChangeRecordState();
            }
            if (GUILayout.Button("Clear"))
            {
                ClearDrawing();
            }

            EditorGUILayout.Space();
            EditorGUILayout.Space();
            EditorGUILayout.Space();

            if (GUILayout.Button("Import datafile"))
            {
                string path = EditorUtility.OpenFilePanel("Choose a NetworkProfiler file", "", "");
                if (!string.IsNullOrEmpty(path))
                {
                    var ticks = ProfilerContainer.FromBytes(File.ReadAllBytes(path)).Ticks;
                    if (ticks.Length >= 2)
                    {
                        m_Curve   = AnimationCurve.Constant(ticks[0].EventId, ticks[(ticks.Length - 1)].EventId, 0);
                        m_ShowMax = ticks.Length;
                        m_ShowMin = ticks.Length - Mathf.Clamp(100, 0, ticks.Length);
                    }
                    else
                    {
                        m_Curve = AnimationCurve.Constant(0, 1, 0);
                    }

                    m_CurrentTicks.Clear();
                    for (int i = 0; i < ticks.Length; i++)
                    {
                        m_CurrentTicks.Add(ticks[i]);

                        uint bytes = 0;
                        if (ticks[i].Events.Count > 0)
                        {
                            for (int j = 0; j < ticks[i].Events.Count; j++)
                            {
                                var tickEvent = ticks[i].Events[j];
                                bytes += tickEvent.Bytes;
                            }
                        }

                        m_Curve.AddKey(ticks[i].EventId, bytes);
                    }
                }
            }

            if (GUILayout.Button("Export datafile"))
            {
                int max          = (int)m_ShowMax;
                int min          = (int)m_ShowMin;
                int ticksInRange = max - min;
                var ticks        = new ProfilerTick[ticksInRange];
                for (int i = min; i < max; i++)
                {
                    ticks[i - min] = m_CurrentTicks[i];
                }
                string path = EditorUtility.SaveFilePanel("Save NetworkProfiler data", "", "networkProfilerData", "");
                if (!string.IsNullOrEmpty(path))
                {
                    File.WriteAllBytes(path, new ProfilerContainer {
                        Ticks = ticks
                    }.ToBytes());
                }
            }

            EditorGUILayout.EndHorizontal();
            float prevHis = m_CaptureCount;

            m_CaptureCount = EditorGUILayout.DelayedIntField("History count", m_CaptureCount);
            if (m_CaptureCount <= 0)
            {
                m_CaptureCount = 1;
            }
            m_UpdateDelay = EditorGUILayout.Slider("Refresh delay", m_UpdateDelay, 0.1f, 10f);
            EditorGUILayout.EndVertical();

            if (prevHis != m_CaptureCount)
            {
                StartRecording();
            }

            //Cache
            if (NetworkProfiler.IsRunning)
            {
                if (Time.unscaledTime - m_LastDrawn > m_UpdateDelay)
                {
                    m_LastDrawn = Time.unscaledTime;
                    m_CurrentTicks.Clear();
                    if (NetworkProfiler.Ticks.Count >= 2)
                    {
                        m_Curve = AnimationCurve.Constant(NetworkProfiler.Ticks.ElementAt(0).EventId, NetworkProfiler.Ticks.ElementAt(NetworkProfiler.Ticks.Count - 1).EventId, 0);
                    }

                    for (int i = 0; i < NetworkProfiler.Ticks.Count; i++)
                    {
                        var tick = NetworkProfiler.Ticks.ElementAt(i);
                        m_CurrentTicks.Add(tick);

                        uint bytes = 0;
                        if (tick.Events.Count > 0)
                        {
                            for (int j = 0; j < tick.Events.Count; j++)
                            {
                                var tickEvent = tick.Events[j];
                                bytes += tickEvent.Bytes;
                            }
                        }

                        m_Curve.AddKey(tick.EventId, bytes);
                    }
                }
            }


            //Draw Animation curve and slider
            m_Curve = EditorGUILayout.CurveField(m_Curve);
            EditorGUILayout.MinMaxSlider(ref m_ShowMin, ref m_ShowMax, 0, m_CurrentTicks.Count);
            //Verify slider values
            if (m_ShowMin < 0)
            {
                m_ShowMin = 0;
            }
            if (m_ShowMax > m_CurrentTicks.Count)
            {
                m_ShowMax = m_CurrentTicks.Count;
            }
            if (m_ShowMin <= 0 && m_ShowMax <= 0)
            {
                m_ShowMin = 0;
                m_ShowMax = m_CurrentTicks.Count;
            }

            //Draw main board
            bool hover            = false;
            int  nonEmptyTicks    = 0;
            int  largestTickCount = 0;
            int  totalTicks       = ((int)m_ShowMax - (int)m_ShowMin);

            for (int i = (int)m_ShowMin; i < (int)m_ShowMax; i++)
            {
                if (m_CurrentTicks[i].Events.Count > 0)
                {
                    nonEmptyTicks++;                                     //Count non empty ticks
                }
                if (m_CurrentTicks[i].Events.Count > largestTickCount)
                {
                    largestTickCount = m_CurrentTicks[i].Events.Count;                                                    //Get how many events the tick with most events has
                }
            }

            int emptyTicks = totalTicks - nonEmptyTicks;

            float equalWidth   = position.width / totalTicks;
            float propWidth    = equalWidth * 0.3f;
            float widthPerTick = ((position.width - emptyTicks * propWidth) / nonEmptyTicks);

            float currentX    = 0;
            int   emptyStreak = 0;

            for (int i = (int)m_ShowMin; i < (int)m_ShowMax; i++)
            {
                var tick = m_CurrentTicks[i];
                if (tick.Events.Count == 0 && i != totalTicks - 1)
                {
                    emptyStreak++;
                    continue;
                }

                if (emptyStreak > 0 || i == totalTicks - 1)
                {
                    var dataRect = new Rect(currentX, 140f, propWidth * emptyStreak, position.height - 140f);
                    currentX += propWidth * emptyStreak;
                    if (emptyStreak >= 2)
                    {
                        EditorGUI.LabelField(new Rect(dataRect.x, dataRect.y, dataRect.width, dataRect.height), emptyStreak.ToString(), s_WrapStyle);
                    }
                    emptyStreak = 0;
                }

                if (tick.Events.Count > 0)
                {
                    float heightPerEvent = ((position.height - 140f) - (5f * largestTickCount)) / largestTickCount;

                    float currentY = 140f;
                    for (int j = 0; j < tick.Events.Count; j++)
                    {
                        var tickEvent = tick.Events[j];
                        var dataRect  = new Rect(currentX, currentY, widthPerTick, heightPerEvent);

                        if (dataRect.Contains(Event.current.mousePosition))
                        {
                            hover        = true;
                            m_EventHover = tickEvent;
                        }

                        EditorGUI.DrawRect(dataRect, TickTypeToColor(tickEvent.EventType, true));
                        EditorGUI.LabelField(new Rect(dataRect.x, dataRect.y, dataRect.width, dataRect.height / 2), tickEvent.EventType.ToString(), s_WrapStyle);
                        EditorGUI.LabelField(new Rect(dataRect.x, dataRect.y + dataRect.height / 2, dataRect.width, dataRect.height / 2), tickEvent.Bytes + "B", s_WrapStyle);

                        currentY += heightPerEvent + 5f;
                    }
                }

                EditorGUI.DrawRect(new Rect(currentX, 100, widthPerTick, 40), TickTypeToColor(tick.Type, false));
                EditorGUI.LabelField(new Rect(currentX, 100, widthPerTick, 20), tick.Type.ToString(), s_WrapStyle);
                EditorGUI.LabelField(new Rect(currentX, 120, widthPerTick, 20), tick.Frame.ToString(), s_WrapStyle);
                currentX += widthPerTick;
            }

            //Calculate alpha
            if (hover)
            {
                m_HoverAlpha += deltaTime * 10f;

                if (m_HoverAlpha > 1f)
                {
                    m_HoverAlpha = 1f;
                }
                else if (m_HoverAlpha < 0f)
                {
                    m_HoverAlpha = 0f;
                }
            }
            else
            {
                m_HoverAlpha -= deltaTime * 10f;
                if (m_HoverAlpha > 1f)
                {
                    m_HoverAlpha = 1f;
                }
                else if (m_HoverAlpha < 0f)
                {
                    m_HoverAlpha = 0f;
                }
            }

            //Draw hover thingy
            if (m_EventHover != null)
            {
                var rect = new Rect(Event.current.mousePosition, new Vector2(500, 100));
                EditorGUI.DrawRect(rect, GetEditorColorWithAlpha(m_HoverAlpha));

                float heightPerField = (rect.height - 5) / 4;
                EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + 5, rect.width, rect.height), "EventType: " + m_EventHover.EventType, GetStyleWithTextAlpha(EditorStyles.label, m_HoverAlpha));
                EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 1 + 5, rect.width, rect.height), "Size: " + m_EventHover.Bytes + "B", GetStyleWithTextAlpha(EditorStyles.label, m_HoverAlpha));
                EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 2 + 5, rect.width, rect.height), "Channel: " + m_EventHover.ChannelName, GetStyleWithTextAlpha(EditorStyles.label, m_HoverAlpha));
                EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 3 + 5, rect.width, rect.height), "MessageType: " + m_EventHover.MessageType, GetStyleWithTextAlpha(EditorStyles.label, m_HoverAlpha));
            }

            Repaint();
        }