Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of Delegate for multiple buttons

    • 0
    • 6
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 563
    Comment on it


    /* Use of  Delegate for multiple buttons. Different functions are assigned to delegate that perform the required functionality. The basic color change and animation that are common to all buttons are given here. */

    // Apply this Script on every button in your game.

    using UnityEngine;
    using System.Collections;
    
    public class GeneralButton : MonoBehaviour
    {
        /*initialize this delegate "OnButtonHandler()" with different button functions in which ever class  needed. Example-  playButton.onButtonHandler = OnPlay; -> OnPlay() */
    
    
        public delegate void OnButtonHandler();
    
        public OnButtonHandler onButtonHandler;
        public BoxCollider2D collider;    // button collider
    
        public SpriteRenderer buttonBackSprite;
        public Color selectedColor = Color.blue;
    
        private static GameObject selectedObject = null;  //null game object
    
       
    
       void Start ()
        {
                  //initialize any here animation if needed
        }
        
        
        void Update ()     
        {
    
                 OnTouch();
        }
    
        public void OnTouch()
        {
            
            Ray ray;
            RaycastHit2D[] hit;   //returns information about the object detected by the ray cast in 2D physics.
            
            Vector3 pos = Input.mousePosition;
                
            ray = camera.ScreenPointToRay(pos);
            
            hit = Physics2D.RaycastAll(ray.origin, Vector2.zero);  //origin and direction of the ray
            
    
            // On Button Click
    
    
            if (Input.GetMouseButtonDown(0))
            {
                if (hit == null) return;  // no ray detected
    
                foreach (RaycastHit2D elem in hit)
                {
                    if (elem != null && elem.collider == collider)
                    {
                        this.buttonBackSprite.color = selectedColor;  // color of the button changed
                        selectedObject = this.gameObject;
                       
                        break;
                    }
                }
                return;
            }
    
            else if (Input.GetMouseButtonUp(0) && selectedObject == this.gameObject)
            {
                this.buttonBackSprite.color = new Color(1.0f, 1.0f, 1.0f);
    
                if (hit == null) return;
    
                foreach (RaycastHit2D elem in hit)
                {
                    if (elem != null && elem.collider == collider)
                    {
                        if (onButtonHandler != null)
                        {
                            onButtonHandler();  // calling required function through delegate
                        }
                        break;
                    }
                }
                selectedObject = null;
            }       
        }
    
      }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: