Search Unity

Scripted graphics in the UI - why so difficult ?

Discussion in 'UGUI & TextMesh Pro' started by LeRan, Jan 30, 2017.

  1. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    Hello forum,

    I'm trying to script some simple graphic elements in the UI for some ball-throwing game. Namely, I need :
    - one dynamic line to set the line of fire (straight line, seen from above),
    - one dynamic angular sector to set the height of the shot,
    - eventually, one parabol to emulate the planned trajectory (as seen from the side).

    It seems very difficult to design easy solutions... I've considered so far for the line :
    - creating a gameObject with a LineRenderer component.
    - using the UILineRenderer extension found here (courtesy of firagon).
    For the circle arc the only elegant solution I've found will probably derive from there (courtesy of zge), and uses OnPopulateMesh.

    Another point is that I would like all those graphic elements to display in only one camera view : the screen will be split into several displays, and for example I want the line of fire to display only in the "top-down camera" and no other ones, even if they happen to focus on the same area (from the side for example).

    So, I submit thoses questions to your wisdom :
    - am I mising something easier to script graphics in the UI?
    - is there a way to force any given graphic element to be displayed only by one specific camera view?

    Thanks
     
    Last edited: Jan 30, 2017
  2. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    All right, here is the best solution I found so far : a canvas object, with a Graphic class script attached to it, that draws things when OnPopulateMech is called. The trick is to store useful info into public variables of that script, and to redraw everything anew when I need it by calling SetVerticesDirty on that script.

    Am I doing it the right way ?

    Besides, it seems that a canvas can hold at most only one Graphic component. Does that mean that I have either :
    1) to create several canvases for several geometries (one line for the aim and one circle for the predicted impact for example), or
    2) to live a nightmare trying to draw all those different geometries with one single OnPopulateMesh ?
     
  3. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Each Game Object can have one Graphic component (like Image or Text). So don't attach it to the Canvas itself but to GameObjects beneath Canvas.

    Not sure if you have seen my answer to another thread, but it is also the answer when it comes to custom polygon shapes in the UI:
    https://forum.unity3d.com/threads/runtime-gradient-editor-ui.455031/#post-2952794
     
    LeRan likes this.
  4. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    @Hosnkobf Thank you Sir, that was the answer I was looking for, everything works perfectly now !

    Less important question (since there are workarounds, but I'd still like to know...) : is there a way to clear the mesh of a Graphic component from another script, or is it necessary to call SetVerticesDirty and have something in the OnPopulateMesh function that detects the fact that the mesh must be cleared (see pseudo code example)?

    For example :
    public bool mustClearMesh;
    (...)
    protected virtual void OnPopulateMesh(VertexHelper vh)
    {
    if (mustClearMesh) then
    {
    vh.Clear();
    return;
    }
    (...)
    }
     
    Last edited: Feb 15, 2017
  5. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    not sure what you want to achieve with the clear.
    normally you have to clear all the time before rendering again unless you want to have some funny artifacts.

    If you really want to make the mesh invisible you have many options. You can do it as you described, or you set the alpha value of the color property to zero or you disable the game object (maybe you can also just disable your Component)...
     
    LeRan likes this.
  6. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    Thanks for the heads up - actually I wanted to stop displaying things once it was no longer useful without having to disable the component, by clearing the mesh one way or another. I ended up disabling the component because it was easier than creating a scripted clearing condition.

    I still don't like the idea of having a still-existant mesh roam around even if it's invisible and/or disabled, but that perfectionnism of mine is another issue that requires unrelated medical attention :)
     
  7. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Don't worry: it is not there (in the graphics card memory) when it is disabled.