Question by
Johan32123 · Nov 24, 2020 at 10:12 PM ·
button2d game
2D toggle on/off button without UI?
I want to make a toggle button based off some code that I stole- I mean borrowed. The code allows you to have a button without using UI as well as allows you to change the look of the sprite when you click it. Here is the code: using UnityEngine; using UnityEngine.Events;
public class Button : MonoBehaviour {
public Sprite btnUp;
public Sprite btnDown;
public UnityEvent buttonClick;
void Awake()
{
if (buttonClick == null) { buttonClick = new UnityEvent(); }
}
void OnMouseDown()
{
GetComponent<SpriteRenderer>().sprite = btnDown;
}
void OnMouseUp()
{
print("Click!");
buttonClick.Invoke();
GetComponent<SpriteRenderer>().sprite = btnUp;
}
}
So yeah I'm tryna make that script into a toggle script that changes the look of the object depending on what toggle it is. I'm fairly new to coding and help with a simple answer would be really appreciated!
Comment