Make a field for editing an AnimationCurve.
private void TextureGUI() { data.curve = UGL.CurveField(data.curve); int?toRemove = null; for (int i = 0; i < data.textureNames.Count; i++) { UGL.BeginHorizontal(); data.textureNames[i] = UGL.DelayedTextField(data.textureNames[i], GUILayout.MaxWidth(Screen.width)); GUILayout.FlexibleSpace(); Texture2D oldVal = i < data.textures.Count ? data.textures[i]: null; Texture2D newVal = (Texture2D)UGL.ObjectField("", oldVal, typeof(Texture2D), true, GUILayout.Width(100)); UGL.BeginVertical(); if (GUILayout.Button("Set As Curve", GUILayout.Width(100))) { Texture2D text = new Texture2D(100, 1); for (int j = 0; j < text.width; j++) { float sample = data.curve.Evaluate((float)j / text.width); text.SetPixel(j, 0, new Color(sample, sample, sample)); } text.Apply(); if (text != oldVal) { string path = AssetDatabase.GetAssetPath(data); AssetDatabase.AddObjectToAsset(text, path); newVal = text; } } GUI.color = Color.red; if (GUILayout.Button("-", GUILayout.Width(100))) { toRemove = i; } UGL.EndVertical(); GUI.color = Color.white; if (oldVal != newVal) { if (!data.textures.Contains(oldVal)) { data.textures.Add(newVal); } else { data.textures[data.textures.IndexOf(oldVal)] = newVal; } } UGL.EndHorizontal(); } if (toRemove != null) { data.textureNames.RemoveAt((int)toRemove); } }
private void OnGUI() { bool recording = NetworkProfiler.isRunning; float deltaTime = (float)(EditorApplication.timeSinceStartup - lastSetup); 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)) { ProfilerTick[] ticks = BinarySerializer.Deserialize <ProfilerContainer>(File.ReadAllBytes(path)).ticks; if (ticks.Length >= 2) { curve = AnimationCurve.Constant(ticks[0].EventId, ticks[(ticks.Length - 1)].EventId, 0); showMax = ticks.Length; showMin = ticks.Length - Mathf.Clamp(100, 0, ticks.Length); } else { curve = AnimationCurve.Constant(0, 1, 0); } currentTicks.Clear(); for (int i = 0; i < ticks.Length; i++) { currentTicks.Add(ticks[i]); uint bytes = 0; if (ticks[i].Events.Count > 0) { for (int j = 0; j < ticks[i].Events.Count; j++) { TickEvent tickEvent = ticks[i].Events[j]; bytes += tickEvent.Bytes; } } curve.AddKey(ticks[i].EventId, bytes); } } } if (GUILayout.Button("Export datafile")) { int max = (int)showMax; int min = (int)showMin; int ticksInRange = max - min; ProfilerTick[] ticks = new ProfilerTick[ticksInRange]; for (int i = min; i < max; i++) { ticks[i - min] = currentTicks[i]; } string path = EditorUtility.SaveFilePanel("Save NetworkProfiler data", "", "networkProfilerData", ""); if (!string.IsNullOrEmpty(path)) { File.WriteAllBytes(path, BinarySerializer.Serialize(new ProfilerContainer() { ticks = ticks })); } } EditorGUILayout.EndHorizontal(); float prevHis = captureCount; captureCount = EditorGUILayout.DelayedIntField("History count", captureCount); if (captureCount <= 0) { captureCount = 1; } updateDelay = EditorGUILayout.Slider("Refresh delay", updateDelay, 0.1f, 10f); EditorGUILayout.EndVertical(); if (prevHis != captureCount) { StartRecording(); } //Cache if (NetworkProfiler.isRunning) { if (Time.unscaledTime - lastDrawn > updateDelay) { lastDrawn = Time.unscaledTime; currentTicks.Clear(); if (NetworkProfiler.Ticks.Count >= 2) { 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++) { ProfilerTick tick = NetworkProfiler.Ticks.ElementAt(i); currentTicks.Add(tick); uint bytes = 0; if (tick.Events.Count > 0) { for (int j = 0; j < tick.Events.Count; j++) { TickEvent tickEvent = tick.Events[j]; bytes += tickEvent.Bytes; } } curve.AddKey(tick.EventId, bytes); } } } //Draw Animation curve and slider curve = EditorGUILayout.CurveField(curve); EditorGUILayout.MinMaxSlider(ref showMin, ref showMax, 0, currentTicks.Count); //Verify slider values if (showMin < 0) { showMin = 0; } if (showMax > currentTicks.Count) { showMax = currentTicks.Count; } if (showMin <= 0 && showMax <= 0) { showMin = 0; showMax = currentTicks.Count; } //Draw main board bool hover = false; int nonEmptyTicks = 0; int largestTickCount = 0; int totalTicks = ((int)showMax - (int)showMin); for (int i = (int)showMin; i < (int)showMax; i++) { if (currentTicks[i].Events.Count > 0) { nonEmptyTicks++; //Count non empty ticks } if (currentTicks[i].Events.Count > largestTickCount) { largestTickCount = 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)showMin; i < (int)showMax; i++) { ProfilerTick tick = currentTicks[i]; if (tick.Events.Count == 0 && i != totalTicks - 1) { emptyStreak++; continue; } else if (emptyStreak > 0 || i == totalTicks - 1) { Rect 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(), 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++) { TickEvent tickEvent = tick.Events[j]; Rect dataRect = new Rect(currentX, currentY, widthPerTick, heightPerEvent); if (dataRect.Contains(Event.current.mousePosition)) { hover = true; 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(), wrapStyle); EditorGUI.LabelField(new Rect(dataRect.x, dataRect.y + dataRect.height / 2, dataRect.width, dataRect.height / 2), tickEvent.Bytes + "B", 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(), wrapStyle); EditorGUI.LabelField(new Rect(currentX, 120, widthPerTick, 20), tick.Frame.ToString(), wrapStyle); currentX += widthPerTick; } //Calculate alpha if (hover) { hoverAlpha += deltaTime * 10f; if (hoverAlpha > 1f) { hoverAlpha = 1f; } else if (hoverAlpha < 0f) { hoverAlpha = 0f; } } else { hoverAlpha -= deltaTime * 10f; if (hoverAlpha > 1f) { hoverAlpha = 1f; } else if (hoverAlpha < 0f) { hoverAlpha = 0f; } } //Draw hover thingy if (eventHover != null) { Rect rect = new Rect(Event.current.mousePosition, new Vector2(500, 100)); EditorGUI.DrawRect(rect, GetEditorColorWithAlpha(hoverAlpha)); float heightPerField = (rect.height - 5) / 4; EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + 5, rect.width, rect.height), "EventType: " + eventHover.EventType.ToString(), GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha)); EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 1 + 5, rect.width, rect.height), "Size: " + eventHover.Bytes + "B", GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha)); EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 2 + 5, rect.width, rect.height), "Channel: " + eventHover.ChannelName, GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha)); EditorGUI.LabelField(new Rect(rect.x + 5, rect.y + heightPerField * 3 + 5, rect.width, rect.height), "MessageType: " + eventHover.MessageType, GetStyleWithTextAlpha(EditorStyles.label, hoverAlpha)); } Repaint(); }