Author Topic: Combining NGUI and Unity's LineRenderer  (Read 14242 times)

Bradamante3D

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 79
    • View Profile
Combining NGUI and Unity's LineRenderer
« on: July 28, 2013, 03:27:23 PM »
Hi,

for my game I need a map where the player build routes via clicking on star systems. Now, those star systems need to be connected via lines. NGUI doesn't have such feature, which is why I am using Unity's LineRenderer.

My problem is that the lines in my current test case show up in the editor, but not in the game preview. See this screenshot:



In the middle column you can see many GameObject, all parented to a scroll view (which clipping does not work, btw). The three lines you can see (LineDrawer0, 1, 2) all have different Z values that I manually set for testing, with 0 (the selected line) giving me the look I want. The lines show up in the top left preview, but not in the buttom left game.

Do do I get those lines to show up in the game?

Now, you might say, well, set those lines up manully, using only NGUI. Well, I could do that. But it was already a pain setting up those buttons. Setting up the lines would be even more manual work, which I am currently doing via script:

  1. void Start () {
  2.                
  3.                 foreach ( Transform nextTf in MapNeighbors ) {
  4.                        
  5.                         createLine ( tf.position, nextTf.position, 0.025f, new Color ( 0.5f, 0.5f, 0.5f, 1.0f ) );
  6.                 }
  7.                
  8.                 foreach ( Transform lineChild in tf ) {
  9.                         Vector3 oldPos = lineChild.position;
  10.                         oldPos.z = 0;
  11.                         lineChild.position = oldPos;
  12.                         Debug.Log( lineChild.name );
  13.                 //      lineChild.Translate ( new Vector3 ( 0,0, -3 / lineChild.localScale.z ) );
  14.                 }
  15.         }
  16.  
  17. public void createLine (Vector3 start, Vector3 end, float lineSize, Color c) {
  18.                
  19.                 GameObject lineDrawer = new GameObject(string.Format("lineDrawer{0}", lineIndex));
  20.                 lineDrawer.layer = 9;
  21.                 lineDrawer.transform.parent = tf;
  22.                
  23.                 LineRenderer line = (LineRenderer) lineDrawer.AddComponent<LineRenderer>();
  24.                 line.material = mat; // new Material(shader);
  25.                 line.material.color = c;
  26.                 line.useWorldSpace = false;
  27.                 line.SetWidth(lineSize, lineSize);
  28.                 line.SetVertexCount(2);
  29.                 line.SetPosition(0, new Vector3( start.x, start.y, 0) );
  30.                 line.SetPosition(1, new Vector3( end.x, end.y, 0) );
  31.                
  32.                 lineIndex++;
  33.         }
  34.  

And if I did it via NGUI ... how would I go about it? Use a NGUI Sprite for each line? Let's say a 64x64 px line graphic, turned and scaled for each line? That would take forever to setup.

Also not that my map needs some dynamic element, it is not static. To build routes, the player would click planets to build a route. On top of the current thin grey lines a thick, green line would appear. I am not sure how I would do that via NGUI, but I do know how to do it with LineRenderer.

Also note that the clipping does not work. It's a scroll panel right now. I will now go and look at the scroll camera technique.

Thx
#301224014, #301432336, #302399130

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Combining NGUI and Unity's LineRenderer
« Reply #1 on: July 29, 2013, 09:41:02 AM »
Look at Vectrocity plugin.

Bradamante3D

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 79
    • View Profile
Re: Combining NGUI and Unity's LineRenderer
« Reply #2 on: July 31, 2013, 07:59:32 AM »
Well, rendering issues aside ... Why does the scroll view clipping not work? For the clipping to work, do all elements have to be on the same atlas? With all z values at 0, using only the NGUI ordering values to sort the elements? If I correct that, that will likely fix my clipping?

Look at Vectrocity plugin.

OK, I've send them an email. But ... Why should Vectrocity work while the LineRenderer doesn't? Shouldn't NGUI work with the LineRenderer?
#301224014, #301432336, #302399130

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Combining NGUI and Unity's LineRenderer
« Reply #3 on: July 31, 2013, 12:37:57 PM »
Clipping only works on NGUI widgets, and only if they are using a proper shader (Unlit Transparent). Furthermore only one panel can clip widgets. Nested panels break clipping.

Bradamante3D

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 79
    • View Profile
Re: Combining NGUI and Unity's LineRenderer
« Reply #4 on: July 31, 2013, 01:49:39 PM »
EDIT: Alright then, let's say there's no way to combine the NGUI clipping function with Unity's LineRenderer. How is Vectrocity different? I won't start familiarizing myself with Vectrocity without some reassurance that it will work.

Bingo, got at least the clipping to work:





But as you can see, the LineRenderer is only drawn outside the clipping area, not inside ("Scope" should have three lines). Also, I can't get for the life of mine the z values on the LineRenderer nodes to work. Following several repositioning attempts is always something like -300+, -1, +1, +2, +300+ or whatever. They never show up in the clipping area (or mostly not at all).

My hierarchy previously was:

UIPanel / UIDraggable Panel -> UIPanel / UIDragPanelContents -> [my elements]

Deleting the middle UIPanel (leaving only the DragPanelContents) on the middle node did it. Also the z values are now: 0 -> -1 -> 0. That helped too. NOTE: No Panel script needed on the Content node.
« Last Edit: July 31, 2013, 02:57:39 PM by Bradamante3D »
#301224014, #301432336, #302399130

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Combining NGUI and Unity's LineRenderer
« Reply #5 on: August 01, 2013, 06:11:16 PM »
As I recall, with Vectrocity you can draw lines in 3D. That said, I ended up trying it then using my own custom solution to draw the hyperspace routes between stars in Starlink.

Johanna

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Combining NGUI and Unity's LineRenderer
« Reply #6 on: December 06, 2014, 05:12:54 PM »
Well, it has been more than a year since the last reply - but I also have the problem, that my line renderer line can not be seen ingame. Is there any solution to use the line renderer with NGUI now?

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: Combining NGUI and Unity's LineRenderer
« Reply #7 on: December 08, 2014, 01:26:26 AM »
Assuming that the renderers that you are using are in the correct unity layer it should render behind all of the other UI/Panel geometry by default.

If the problem is that the lines aren't being clipped properly by NGUI panels, you only have a couple of options.

  • The best solution would be to create a simple NGUI Sprite based line renderer. Using sliced sprites I don't imagine it being too hard to get something working depending on your requirements.
  • Use a different clipping mechanic such as a stencil buffer. (Now available for free users) However, this will require you to write some shaders to read/write into the buffer.

Johanna

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: Combining NGUI and Unity's LineRenderer
« Reply #8 on: December 09, 2014, 02:43:58 PM »
Quote
Assuming that the renderers that you are using are in the correct unity layer
Do you mean the sorting layers? Or the Layer which can be adjusted near the Tags?
All objects (NGUI as well as the object with the Line Renderer) are on the same Layer.

Quote
it should render behind all of the other UI/Panel geometry by default
Hmm then I think I should not use NGUI sprites for the background graphics.

Thank you for your advices relating to the sprite based line renderer and the stencil buffer. I have no experience in such things. I will first try to change the NGUI background with Unity Background sprites, but when it does not help, I will learn about those things.

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: Combining NGUI and Unity's LineRenderer
« Reply #9 on: December 10, 2014, 05:24:18 AM »
Yeah, I meant as long as the camera isn't culling out the renderer due to the culling layer mask is what to check. Make sure that the camera component includes the same layer as the layer that the line renderer is on.

Another thing to try would be is if you have a very controlled scene you can explicitly set the render queues on the UIPanels. By default transparent geometry is rendered in the 3000 queue. So if you setup a BG panel and set it to < 3000 and a FG NGUI panels > 3000 it should render the line renderer between the two properly. This doesn't help with the NGUIPanel clipping (which you would need to investigate those other solutions for), but it will help if all you need to do render some sprites in front or behind the line renderer.

Hope that helps.

r.pedra

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 20
  • Posts: 131
    • View Profile
Re: Combining NGUI and Unity's LineRenderer
« Reply #10 on: December 10, 2014, 06:32:40 AM »