Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: kaz2057 on October 04, 2012, 10:04:12 AM

Title: Toggle Button
Post by: kaz2057 on October 04, 2012, 10:04:12 AM
Dear support,
I need to create a lot of toggle buttons like checkbox using "hierarchy groups".

I have not found any solution for this problem.

On others thread there is a link to a script: http://www.tasharen.com/?topic=toggle-button but it is void ...
Then ... I have not find anything ...

There is any news about it? Any new solution? How can I fix this?
This is very important for my web application because it is a configurator.
Help me please!
Thank you

Title: Re: Toggle Button
Post by: ArenMook on October 04, 2012, 10:31:10 AM
1. Create a new game object under your panel.

2. Create a sliced sprite for the button's background of how it should look like when it's not toggled.

3. Create another sprite covering the button's background -- how it should look like when it's toggled.

4. Add a UICheckbox script to the game object you created in step 1.

5. For the "checkmark" on the UICheckbox, specify sprite you created in step 3.

You now have a toggle button.
Title: Re: Toggle Button
Post by: kaz2057 on October 04, 2012, 10:36:46 AM
1. Create a new game object under your panel.

2. Create a sliced sprite for the button's background of how it should look like when it's not toggled.

3. Create another sprite covering the button's background -- how it should look like when it's toggled.

4. Add a UICheckbox script to the game object you created in step 1.

5. For the "checkmark" on the UICheckbox, specify sprite you created in step 3.

You now have a toggle button.

Thank you Aren. Quick reply @_@

I try immediatly this solution.
However just another question...
Before than the sprite cover the button's background, can I have also hover button,  ? thanks!!
Title: Re: Toggle Button
Post by: ArenMook on October 04, 2012, 10:38:40 AM
For a hover effect you can add a UIButton script and set it to color the sprite from step 2.
Title: Re: Toggle Button
Post by: kaz2057 on October 08, 2012, 10:23:40 AM
For a hover effect you can add a UIButton script and set it to color the sprite from step 2.

Thanks you for the answer.

However I fix the problem with IUImage Button to have another image button as hover.

Now, however, I have another problem.

If I reclick on an actived button, it return on base state and so I have no button actived ...

Can you help me please?
Title: Re: Toggle Button
Post by: ArenMook on October 08, 2012, 04:36:44 PM
"Option can be none" should be set to false.
Title: Re: Toggle Button
Post by: kaz2057 on October 09, 2012, 03:45:25 AM
"Option can be none" should be set to false.

Thank you. I have tested this option but for some reason not work yesterday . Sorry for the trouble. It work now :D
Title: Re: Toggle Button
Post by: trelobyte on April 26, 2013, 01:55:27 PM
Could you please give an example of toggling between 2 materials of the target game object without disabling it ?
This is what i have so far but i get compile errors:

An object reference is required to access non-static member `CheckBoxMaterialToggle.material1
An object reference is required to access non-static member `CheckBoxMaterialToggle.material2
any help is greatly appreciated.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CheckBoxMaterialToggle : MonoBehaviour {
  5.  
  6.  
  7.         public GameObject target;
  8.         public bool inverse = false;
  9.         public Material material1;
  10.         public Material material2;
  11.        
  12.         static public void ChangeMaterial (GameObject target, bool state)
  13.         {
  14.                 if (state)
  15.                         target.renderer.material = material1;
  16.                  else
  17.                         target.renderer.material = material2;
  18.                
  19.         }
  20.  
  21.         void OnEnable ()
  22.         {
  23.                 UICheckbox chk = GetComponent<UICheckbox>();
  24.                 if (chk != null) OnActivate(chk.isChecked);
  25.         }
  26.  
  27.         void OnActivate (bool isActive)
  28.         {
  29.                 if (target != null) {
  30.                         ChangeMaterial(target, inverse ? !isActive : isActive);
  31.        
  32.                         UIPanel panel = NGUITools.FindInParents<UIPanel> (target);
  33.                         if (panel != null)
  34.                                 panel.Refresh ();
  35.                 }
  36.         }
  37. }
Title: Re: Toggle Button
Post by: ArenMook on April 26, 2013, 08:56:07 PM
Remove the "static" from ChangeMaterial function's definition.
Title: Re: Toggle Button
Post by: trelobyte on April 27, 2013, 02:00:23 AM
Thanks Aren !
I am trying to implement a fading between the two but it does not like it.

i would greatly appreciate your help if you had the time to have a look.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CheckBoxMaterialToggle : MonoBehaviour {
  5.  
  6.  
  7.         public GameObject target;
  8.         public bool inverse = false;
  9.         public Material material1;
  10.         public Material material2;
  11.         public float duration;
  12.         public float lerp = Mathf.PingPong (Time.time, duration) / duration;
  13.        
  14.         public void ChangeMaterial (GameObject target, bool state)
  15.         {
  16.                 if (state)
  17.                         target.renderer.material.Lerp (material1, material2, lerp);
  18.                  else
  19.                         target.renderer.material.Lerp (material2, material, lerp);
  20.                
  21.         }
  22.  
  23.         void OnEnable ()
  24.         {
  25.                 UICheckbox chk = GetComponent<UICheckbox>();
  26.                 if (chk != null) OnActivate(chk.isChecked);
  27.         }
  28.  
  29.         void OnActivate (bool isActive)
  30.         {
  31.                 if (target != null) {
  32.                         ChangeMaterial(target, inverse ? !isActive : isActive);
  33.        
  34.                         UIPanel panel = NGUITools.FindInParents<UIPanel> (target);
  35.                         if (panel != null)
  36.                                 panel.Refresh ();
  37.                 }
  38.         }
  39. }
Title: Re: Toggle Button
Post by: ArenMook on April 27, 2013, 03:45:47 PM
This isn't an NGUI question at this point, but a Unity usability question. Seeing as I've never used renderer.material.Lerp, I can't advise you much here. Try Unity's forums instead.
Title: Re: Toggle Button
Post by: trelobyte on December 17, 2013, 04:44:32 PM
watched the tutorial on converting to ngui 3 but can't convert this one ...any help ?
thanks