Search Unity

Editor window for graphing values at runtime

Discussion in 'Scripting' started by Pi_3-14, Oct 15, 2012.

  1. Pi_3-14

    Pi_3-14

    Joined:
    May 9, 2012
    Posts:
    168
    I've put a basic graphing plug-in on the wiki: http://wiki.unity3d.com/index.php/EditorGraphWindow

    I give an example of how it can be used right at the top of the page:

    Code (csharp):
    1. public class _TestGraph : MonoBehaviour
    2. {
    3.     void Start ()
    4.     {
    5.         Graph.YMin = -2;
    6.         Graph.YMax = +2;
    7.  
    8.         Graph.channel[ 0 ].isActive = true;
    9.         Graph.channel[ 1 ].isActive = true;
    10.     }
    11.  
    12.  
    13.     void Update () {
    14.         Graph.channel[ 0 ].Feed( Mathf.Sin( Time.time ) );
    15.     }
    16.  
    17.     void FixedUpdate( ) {
    18.         Graph.channel[ 1 ].Feed( Mathf.Sin( Time.time ) );
    19.     }
    20.  
    21. }
    Now if I detach the graph window so that it doesn't cover the main unity window, I get a nice tidy sine wave for both readouts

    However, if I either occlude the main window, or dock the graph window (which does the same thing) the Update graph goes all wobbly.

    What is going on behind the scenes? And can the architecture be improved?

    I suspect it is eating performance by forcing the entire IDE GUI to refresh needlessly. Is there anyway I can get the graph window to repaint without this happening?

    I'm trying to track values in a real-time audio generation component, so it's hard to see whether the performance hit of the graph is messing with the performance of the component.
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,005
    Just found your script. My suggestion is, since you have a fix-sized array, use it as ring-buffer. So you have an in and out "pointer" (index). You would insert an element at the "in"-index and increment it afterwards. Keep in mind to wrap the index at the end. If you want to read the buffer, start at "out" and increment the loop variable until it reaches the in-index. You also have to wrap this loop-variable at the end. Finally you have to decide if you want an endless scrolling view or a fixed overwriting. For an overwriting view, just leave the out marker at 0. For an scrolling view everytime you change the in-index you have to check if you reached the out.index. If so increment the out index as well. Again, you have to wrap the out index as well. So in this case the out index will always be one ahead the in-index once the buffer is filled ;)

    A non scrolling view could be optimised so the window isn't cleared and you only draw the new lines except a redraw is necessary.
     
  3. thling90

    thling90

    Joined:
    Sep 10, 2013
    Posts:
    2
    Hi, I am using your graphing plug-in and find it very useful!
    However, I have some difficulties in plotting graph using it.
    I need to plot the graph when a new data comes in..
    Any advice will be appreciated!
    Pls help, thank you :)
     
  4. danieldownes

    danieldownes

    Joined:
    Apr 20, 2013
    Posts:
    9
    Nice editor plugin. I possibly found a problem which explains why you are getting differences with Update and FixedUpdate sines however.

    I noticed that the two waves move independently, and concluded they should ideal be moving at the same rate.

    If you read up on FixedUpdate, it will only be running at about 40fps (with default 'Time' settings), and Update can run 30-60fps+. Each channel has its own array of data, and basically you are adding more data using the Update function than the FixedUpdate, resulting in the shifting of the two graphs.

    To fix this its better to keep the same number of entries in each channel, here's the updated code to achieve this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. public class TestGraph : MonoBehaviour
    7. {
    8.     float uT;
    9.     float fT;
    10.  
    11.     void Start ()
    12.     {
    13.         Graph.YMin = -2;
    14.         Graph.YMax = +2;
    15.      
    16.         Graph.channel[ 0 ].isActive = true;
    17.         Graph.channel[ 1 ].isActive = true;
    18.     }
    19.  
    20.  
    21.     void Update () {
    22.         uT = Mathf.Sin( Time.time );
    23.         Graph.channel[ 0 ].Feed( uT );
    24.         Graph.channel[ 1 ].Feed( fT );
    25.     }
    26.  
    27.     void FixedUpdate( ) {
    28.         fT = Mathf.Sin( Time.time );
    29.         Graph.channel[ 1 ].Feed( fT );
    30.         Graph.channel[ 0 ].Feed( uT );
    31.     }
    32. }
    33.  
    ..And you'll then notice there's no difference in Time.time between the two calls.
     
  5. carolina-chico

    carolina-chico

    Joined:
    Mar 5, 2016
    Posts:
    1
    Hi, I am running your code, but i Have a error
    Assets/EditorGraph.cs(41,26): warning CS0618: `UnityEngine.Material.Material(string)' is obsolete: `Creating materials from shader source string will be removed in the future. Use Shader assets instead.'
    You can help me. I am new in it.
     
  6. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    If you are new than I do not recommend using code that is 2 years old as it probably has issues.
    The "error" you have is actually simply a warning and should not be anything to worry about.
     
  7. legoblaster1234

    legoblaster1234

    Joined:
    Aug 7, 2017
    Posts:
    27
    You can replace the function with this to get rid of the error:
    Code (CSharp):
    1. void CreateLineMaterial() {
    2.         if (!lineMaterial) {
    3.             // Unity has a built-in shader that is useful for drawing
    4.             // simple colored things.
    5.             Shader shader = Shader.Find("Hidden/Internal-Colored");
    6.             lineMaterial = new Material(shader);
    7.             lineMaterial.hideFlags = HideFlags.HideAndDontSave;
    8.             // Turn on alpha blending
    9.             lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    10.             lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    11.             // Turn backface culling off
    12.             lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
    13.             // Turn off depth writes
    14.             lineMaterial.SetInt("_ZWrite", 0);
    15.         }
    16.     }