- Home /
How to change NGUI Imagebutton amount of sprite states
NGUI's UIImageButton appears to only have three states, not including one for disabled, how could I add the disabled state to the image button?
please look that video and tell me what im thinking or doing wrong please.
http://www.youtube.com/watch?v=xb_RcrPohYU
using UnityEngine;
/// <summary>
/// Sample script showing how easy it is to implement a standard button that swaps sprites.
/// </summary>
[ExecuteInEditMode]
[AddComponentMenu("NGUI/UI/Image Button")]
public class UIImageButton : MonoBehaviour
{
public UISprite target;
public string normalSprite;
public string hoverSprite;
public string pressedSprite;
void OnEnable ()
{
if (target != null)
{
target.spriteName = UICamera.IsHighlighted(gameObject) ? hoverSprite : normalSprite;
}
}
void Start ()
{
if (target == null) target = GetComponentInChildren<UISprite>();
}
void OnHover (bool isOver)
{
if (enabled && target != null)
{
target.spriteName = isOver ? hoverSprite : normalSprite;
target.MakePixelPerfect();
}
}
void OnPress (bool pressed)
{
if (enabled && target != null)
{
target.spriteName = pressed ? pressedSprite : normalSprite;
target.MakePixelPerfect();
}
}
}
Your video doesn't work and in any case this should be a question that is contained on this site with an explanation of the problem. Please fix the video link and describe the problem.
The videolink works like charm i dont know whats wrong, i have proof from 2 skype contacts.
Why do you do it ?
$$anonymous$$ust have been my computer. It does work on my phone...
ok, so can you please open the question again? i do videos because its easier for me also i think a video shows things 7845,8 times better than 30 lines of text (with wrong grammar here and there if the writer isnt native english speaker).
or is it forbidden to link videos/external sources?
Hey sorry, it wasn't me that closed this question. I have reinstated it and reopened it for you.
It is not good practice on UA to have a question that is just a video. It's fine to have a video that shows what you mean but the question should reflect actually what you want (so people can search for it).
Answer by whydoidoit · May 12, 2013 at 07:15 AM
So NGUI works by finding a collider and sending messages to it. All of the NGUI logic for button presses etc are in the UICamera script.
Enabled is an interesting state, if you look at UIButton you will see it implements it as "isEnabled" and in fact UIButton just manages it all internally. To be clear, you do not need a UIButton in order to have a button in NGUI!
So if you want to enable some other state you do so in response to the messages you receive in the script, but in the case of Enabled you script it yourself. Perhaps like this:
using UnityEngine;
[ExecuteInEditMode]
[AddComponentMenu("NGUI/UI/Image Button")]
public class UIImageButtonEx : MonoBehaviour
{
public UISprite target;
public string normalSprite;
public string hoverSprite;
public string pressedSprite;
public string disabledSprite;
public string clickedSprite;
public bool isEnabled;
bool _lastEnabled;
void OnEnable ()
{
StartCoroutine(CheckForEnabled());
}
void OnDisable()
{
if(!string.IsNullOrEmpty(disabledSprite))
target.spriteName = disabledSprite;
}
IEnumerator CheckForEnabled()
{
if(target)
target.spriteName = isEnabled ? (UICamera.IsHighlighted(gameObject) ? hoverSprite : normalSprite) : disabledSprite ;
while(true)
{
if(target && _lastEnabled != isEnabled)
{
_lastEnabled = isEnabled;
target.spriteName = isEnabled ? (UICamera.IsHighlighted(gameObject) ? hoverSprite : normalSprite) : disabledSprite ;
}
yield return new WaitForSeconds(0.1f);
}
}
void Awake ()
{
if (target == null) target = GetComponentInChildren<UISprite>();
disabledSprite = !string.IsNullOrEmpty(disabledSprite) ? disabledSprite : normalSprite;
}
void OnClick()
{
if(target && !string.IsNullOrEmpty(clickedSprite))
{
StartCoroutine(ClickSprite());
}
}
IEnumerator ClickSprite()
{
var former = target.spriteName;
target.spriteName = clickedSprite;
yield return new WaitForSeconds(0.6f);
if(target.spriteName == clickedSprite)
target.spriteName = former;
}
void OnHover (bool isOver)
{
if (enabled && target != null)
{
target.spriteName = isOver ? hoverSprite : normalSprite;
target.MakePixelPerfect();
}
}
void OnPress (bool pressed)
{
if (enabled && target != null)
{
target.spriteName = pressed ? pressedSprite : normalSprite;
target.MakePixelPerfect();
}
}
}
I should point out that UICamera will still send the messages to the other scripts which would need to check the isEnabled property of this script to see if they should react. You could edit UICamera to make that not true.
You probably also want to write a custom inspector for it, so that you get the buttons that choose sprites. Basically that ends up being just an:
NGUIEditorTools.SpriteField("Whatever", currentAtlas, currentSprite, functionToSetNewOne);
$$anonymous$$g.
NGUIEditorTools.SpriteField("Hover", mSprite.atlas, mButton.hoverSprite, OnHover);
...
void OnHover (string spriteName)
{
NGUIEditorTools.RegisterUndo("Image Button Change", mButton, mButton.gameObject, mSprite);
mButton.hoverSprite = spriteName;
Repaint();
}