Author Topic: How to change a material color with an NGUI sliders?  (Read 3411 times)

PotatoOrgyLt

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 2
    • View Profile
How to change a material color with an NGUI sliders?
« on: April 19, 2014, 02:34:29 PM »
Hello everyone. So I am really new to NGUI and I am trying to figure out how to change the material RGB with 3 ngui sliders (one for red, the second for green, and the third for blue). I have modified the code that I found on unity forums and made the color changing with normal unity gui. Here is the code.

  1. #pragma strict
  2.  
  3.     var Skin : GUISkin;
  4.     var myColorR : Color;
  5.     var myColorG : Color;
  6.     var myColorB : Color;
  7.     var myMaterial : Material;
  8.     var myColorRGB : Color;
  9.    
  10.     function OnGUI () {
  11.         GUI.skin = Skin;
  12.         myColorR = RSlider (Rect (100,100,255,10), myColorR);
  13.         myColorG = GSlider(Rect (100,150,255,10), myColorG);
  14.         myColorB = BSlider (Rect (100,200,255,10), myColorB);
  15.     }
  16.      
  17.     function RSlider (screenRect : Rect, rgb : Color) : Color {
  18.         rgb.r = GUI.HorizontalSlider (screenRect, rgb.r, 0.0, 1.0);
  19.         return rgb;
  20.     }
  21.     function GSlider (screenRect : Rect, rgb : Color) : Color {
  22.         rgb.g = GUI.HorizontalSlider (screenRect, rgb.g, 0.0, 1.0);
  23.         return rgb;
  24.     }
  25.     function BSlider (screenRect : Rect, rgb : Color) : Color {
  26.         rgb.b = GUI.HorizontalSlider (screenRect, rgb.b, 0.0, 1.0);
  27.         return rgb;
  28.     }
  29.      
  30.     function Update () {
  31.         myColorRGB = myColorR + myColorG + myColorB;
  32.         myMaterial.color = myColorRGB;
  33.     }

How do I make this with NGUI sldiers? Thank you for your help and time.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to change a material color with an NGUI sliders?
« Reply #1 on: April 19, 2014, 11:19:30 PM »
Attach this script to some object, then on your sliders drag & drop this object into the On Change notification field and choose the appropriate function -- OnRed, OnGreen or OnBlue.
  1. using UnityEngine;
  2.  
  3. public class MyClass : MonoBehaviour
  4. {
  5.     public void OnRed (Material mat)
  6.     {
  7.         Color c = mat.color;
  8.         c.r = UISlider.current.value;
  9.         mat.color = c;
  10.     }
  11.  
  12.     public void OnGreen (Material mat)
  13.     {
  14.         Color c = mat.color;
  15.         c.g = UISlider.current.value;
  16.         mat.color = c;
  17.     }
  18.  
  19.     public void OnBlue (Material mat)
  20.     {
  21.         Color c = mat.color;
  22.         c.b = UISlider.current.value;
  23.         mat.color = c;
  24.     }
  25. }

PotatoOrgyLt

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: How to change a material color with an NGUI sliders?
« Reply #2 on: April 20, 2014, 04:41:37 AM »
Great! Thanks for the help!