Using Sprite as Button

Hello everyone…

I’m new to unity, and i want to ask about sprite. Can we use sprite as touch button? If we can, how can we do that?

Thanks for your help, and sorry for my bad english.

Like Simon stated, just read the documentation, it’ll help you a lot!

Here you go, although you would need to make some modifications for touch input.

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;

public class ButtonCustom : MonoBehaviour{

	public RectTransform rt;
	public MaskableGraphic buttonIcon;
	public Color buttonNColor = Color.grey;
	public Color buttonHColor = Color.white;


	public bool isWorldSpace = false;
	public UnityEvent onClicked;
	private Rect rectangle;

	void Start(){
		rt = gameObject.GetComponent<RectTransform> ();
		if (buttonIcon == null)
			buttonIcon = gameObject.GetComponent<MaskableGraphic> ();
		if(!rt)rt = gameObject.GetComponent<RectTransform> ();
		buttonIcon.color = buttonNColor;
	}
	void Update(){
		if (!isWorldSpace) {
			rectangle = rt.rect;
			Vector3 mPos = Input.mousePosition;
			if (mPos.x > rt.position.x - (rectangle.width / 2) && mPos.x < rt.position.x + (rectangle.width / 2)) {
				if (mPos.y > rt.position.y - (rectangle.height / 2) && mPos.y < rt.position.y + (rectangle.height / 2)) {
					handleHit ();
				} else
					buttonIcon.color = buttonNColor;
			} else
				buttonIcon.color = buttonNColor;
		} else if (hit) {
			handleHit ();
			hit = false;
		} else buttonIcon.color = buttonNColor;
	}

	void handleHit(){
		buttonIcon.color = buttonHColor;
		if (Input.GetMouseButtonUp (0)) {
			Action ();
		}
	}

	void Action(){
		onClicked.Invoke();
	}
	private bool hit = false;
	void beingHit(){
		hit = true;
	}
}