Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Baroto · Apr 05, 2014 at 03:30 PM · buttonspritetouch

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Simon-Larsen · Apr 05, 2014 at 04:00 PM 0
Share

Yes, you can do that. You should read a bit up on the documentation here http://docs.unity3d.com/Documentation/ScriptReference/GUI.Button.html

avatar image Baroto · Apr 07, 2014 at 04:09 AM 0
Share

where we can place our sprite in that script? the script says that it needs texture as image, not sprite.

avatar image jmgek · Jan 08, 2017 at 04:47 AM 0
Share

A little late, and with no help from @ Simon :P

         GameObject gameObject = new GameObject();
         Button button = gameObject.AddComponent<Button>();
         Image image = gameObject.AddComponent<Image>();     
        
         image.sprite = sprite;

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Blayer98 · Apr 05, 2014 at 04:16 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by RubenD056 · Jan 08, 2017 at 09:58 AM

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Button Touch for Android 2 Answers

(Unity 4.6) Button animations 1 Answer

GUI Button, Sprite Swap on Timer (Flashing) 0 Answers

Image Sprite doesn't change with Button Onclick 0 Answers

If you help me, i make you 3d object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges