Author Topic: UIWidget onRender - how to use it?  (Read 4236 times)

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
UIWidget onRender - how to use it?
« on: December 11, 2014, 12:33:23 AM »
Hello all,

Basically I have a bunch of UISprites, all contained within an UIPanel container. The sprites use a special material with an attached shader (Brightness).

I want to be able to control that Brightness parameter through code, and I tried all I could think of, it doesn't work, only manual setting before the build works fine.

I have found that I should use "UIWidget onRender" --> but how shall I use it? Where should I put it? What is the correct syntax to call this? Is there an example for my specific situation? Please help. Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIWidget onRender - how to use it?
« Reply #1 on: December 11, 2014, 09:07:15 AM »
Using onRender is simple. Just assign it on any of those widgets. They will all be drawn using the same draw call assuming you have them using the same one. If you want to control brightness individually, then you will want to make sure that they use different materials instead -- and you will need to assign onRender to each of them.

This function is the equivalent of OnWillRenderObject() that comes with Unity. The only difference is that it's called by NGUI from inside OnWillRenderObject() -- just before the draw call goes to the videocard. You get a material reference to work with. Feed it any values you need, such as your brightness.

For example, here is how I use it in Windward, attached to a UITexture drawing a game map:
  1.         void OnRender (Material mat)
  2.         {
  3.                 if (FOWSystem.instance != null)
  4.                 {
  5.                         mat.SetTexture("_FogTex0", FOWSystem.instance.texture0);
  6.                         mat.SetTexture("_FogTex1", FOWSystem.instance.texture1);
  7.                         mat.SetTexture("_Gradient", gradient);
  8.  
  9.                         if (background != null)
  10.                                 mat.SetTexture("_Background", background);
  11.  
  12.                         mat.SetTextureScale("_FogTex0", new Vector2(mFogScale, mFogScale));
  13.                         mat.SetTextureOffset("_FogTex0", new Vector2(mFogOffset, mFogOffset));
  14.                 }
  15.         }

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: UIWidget onRender - how to use it?
« Reply #2 on: December 11, 2014, 11:15:50 AM »
Thanks for looking into my case! I need all of the sprites in the container to have the same brightness level.

I am clearly missing something here. I have made a script like you suggested and added it to:

1) One of the sprites: no effect
2) Panel=container of the sprites: no effect
3) UIAtlas with the material: no effect

As for the script, it's this simple:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ChangeBrighnessShader : MonoBehaviour {
  5.  
  6.         // Use this for initialization
  7.         void Start () {
  8.        
  9.         }
  10.        
  11.         // Update is called once per frame
  12.         void Update () {
  13.        
  14.         }
  15.        
  16.  
  17.                 void OnRender (Material mat)
  18.  
  19.                 {
  20.  
  21.                                 Debug.Log ("render!");
  22.  
  23.                                 mat.SetFloat ("_Brightness", 20.0f);
  24.  
  25.  
  26.                 }
  27.        
  28.  
  29. }

The shader works for sure. I also tried "Brightness" instead of "_Brightness", also no effect. Anyway the DEBUG message is never displayed, so I assume somehow this onRender stuff never happens?  :-[

By the way, I also tried this
  1.                 Object [] renderers = GameObject.FindObjectsOfType(typeof(Renderer));
  2.                 int i_max = renderers.Length;
  3.                 for (int i = 0; i < i_max; i++)
  4.                 {
  5.                         Material[] materials = ((Renderer)renderers[i]).materials;
  6.                         int j_max = materials.Length;
  7.                         for (int j = 0; j < j_max; j++)
  8.                         {
  9.                                 if (materials[i].shader.name == "Unlit/Transparent Colored (Bright)")
  10.                                 {
  11.                                        
  12.                                         Debug.Log ("shader found!");
  13.                                         materials[i].SetFloat("Brightness", 2.0f);
  14.                                 }
  15.                         }
  16.                 }

This code says "shader found", but the brightness is not adjusted  >:(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIWidget onRender - how to use it?
« Reply #3 on: December 12, 2014, 11:10:19 PM »
Well did you ever set the onRender callback? You made a function for it, but where is the assignment?
  1. GetComponent<UIWidget>().onRender = OnRender;

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: UIWidget onRender - how to use it?
« Reply #4 on: December 13, 2014, 01:48:20 PM »
After fiddling around, I finally got it working! Many thanks.
« Last Edit: December 14, 2014, 07:40:02 PM by Biggix »