title: C# GUI Texture Button Script
body: <p>I've finally found a script that can be used with a GUI Button in an IOS project. I am using Unity3d game engine. I'm a little familiar with JavaScript buttons and animation but am not familiar at all with C#. My problem is not knowing were to write the function that will play a queued animation in the C# button script when the button is touched. Below is copy of the IOS Button Script and then the code I have to play the queued animation.</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public enum Btn
  {
      normal,
      hover,
      armed
  }
  
  [System.Serializable] // Required so it shows up in the inspector 
  public class ButtonTextures
  {
      public Texture normal=null;
      public Texture hover=null;
      public Texture armed=null;
      public ButtonTextures() {}
  
      public Texture this [ButtonState state]
      {
          get
          {
              switch(state)
              {
                  case ButtonState.normal:
                      return normal;
                  case ButtonState.hover:
                      return hover;
                  case ButtonState.armed:
                      return armed;
                  default:
                      return null;
              }
          }
      }
  }
  
  
  [RequireComponent(typeof(GUITexture))]
  [AddComponentMenu ("GUI/Button")]    
  public class GuiButton : MonoBehaviour
  {
      public GameObject messagee;
      public string message = "";
      public string messageDoubleClick = "";
      public ButtonTextures textures;
  
      protected int state = 0;
      protected GUITexture myGUITexture;
  
      private int clickCount = 1;
      private float lastClickTime = 0.0f;
      static private float doubleClickSensitivity = 0.5f;
  
      protected virtual void SetButtonTexture(ButtonState state)
      {
          if (textures[state] != null)
          {
              myGUITexture.texture = textures[state];
          }
      }
  
      public virtual void Reset()
      {
          messagee = gameObject;
          message = "";
          messageDoubleClick = "";
      }
  
      public bool HitTest(Vector2 pos)
      {
          return myGUITexture.HitTest(new Vector3(pos.x, pos.y, 0));
      }
  
      public virtual void Start()
      {
          myGUITexture = GetComponent(typeof(GUITexture)) as GUITexture; 
          SetButtonTexture(ButtonState.normal);
      }
  
      public virtual void OnMouseEnter()
      {
          state++;
          if (state == 1)
          SetButtonTexture(ButtonState.hover);
      }
  
      public virtual void OnMouseDown()
      {
           state++;
           if (state == 2)
              SetButtonTexture(ButtonState.armed);
      }
  
      public virtual void OnMouseUp()
      {
          if (Time.time - lastClickTime &lt;= doubleClickSensitivity)
          {
              ++clickCount;
          }
          else
          {
              clickCount = 1;
          }
  
          if (state == 2)
          {
              state--;
              if (clickCount == 1)
              {
                  if (messagee != null &amp;&amp; message != "")
                  {
                      messagee.SendMessage(message, this);
                  }
              }
              else
              {
                  if (messagee != null &amp;&amp; messageDoubleClick != "")
                  {
                      messagee.SendMessage(messageDoubleClick, this);
                  }
              }
          }
          else
          {
              state --;
              if (state &lt; 0)
                  state = 0;
          }
          SetButtonTexture(ButtonState.normal);
          lastClickTime = Time.time;
      }
  
      public virtual void OnMouseExit()
      {
          if (state &gt; 0)
              state--;
          if (state == 0)
              SetButtonTexture(ButtonState.normal);
      }
  
  #if (UNITY_IPHONE || UNITY_ANDROID)
      void Update()
      {
          int count = Input.touchCount;
          for (int i = 0; i &lt; count; i++)
          {
              Touch touch = Input.GetTouch(i);
              if (HitTest(touch.position))
              {
                  if (touch.phase == TouchPhase.Ended || touch.phase ==      TouchPhase.Canceled)
                  {
                      SetButtonTexture(ButtonState.normal);
                  }
                  else
                  {
                      SetButtonTexture(ButtonState.armed);
                  }
                  if (touch.phase == TouchPhase.Began)
                  {
                      if (touch.tapCount == 1)
                  {
                      if (messagee != null &amp;&amp; message != "")
                      {
                          messagee.SendMessage(message, this);
                      }
                      }
                      else if (touch.tapCount == 2)
                      {
                          if (messagee != null &amp;&amp; messageDoubleClick != "")
                          {
                              messagee.SendMessage(messageDoubleClick, this);
                          }
                      }
                  }
                  break;
              }
          }
      }
  #endif
  }
  </code></pre>
  
  <p>Most of this seems to deal with button states, where my touch button only has one state, which is 'normal'. Should references to 'hover' and armed just be deleted? I also get an error in the console saying; "the type or namespace "Button State" could not be found. Are you missing a using directive or an assembly reference?"  </p>
  
  <p>The code for C# GUI Button play queued animation I want to insert reads like this:</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class example : MonoBehaviour {
      void Update() {
          if (Input.GetButtonDown("Btn"))
              animation.PlayQueued("shoot", QueueMode.PlayNow);
  
      }
  }
  </code></pre>
  
  <p>I suppose of queued animation script snippet; Input.GetButtondown....would change to </p>
  
  <pre><code>void Update() {    
        if(Input.GetTouch("Btn"))
           animation.PlayQueued("shoot", QueueMode.PlayNow);
  } 
  </code></pre>
  
  <p>and be inserted at about line 148 of the GUI Button script. Please help me if you can, I'm
  feeling down. Seriously!  Any help in reformatting this script would be greatly appreciated
  and used as a template as I have two other GUI buttons to setup. It maybe asking a lot or what's a heaven for?  </p>
  
  <p>Respectfully,</p>
  
  <p>Digital D</p>
  
  <p>an analog man, 
  in a digital world        </p>