Search Unity

how do i make gui text in to a button

Discussion in 'Scripting' started by ryand-unity, Dec 29, 2013.

  1. ryand-unity

    ryand-unity

    Joined:
    Jan 21, 2011
    Posts:
    515
    how do i make gui text in to a button. i do not want to use the GUIbuttons in unity i want to use GUIText as a button that is what i need for my game. can some one give me an example that i can go off of so i know what to do to make the script.
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,452
    Any reason why you don't want a GUI.Button?
     
  3. ryand-unity

    ryand-unity

    Joined:
    Jan 21, 2011
    Posts:
    515
    it is easier then GUI buttons and it has the style I need for my game that's why I want to use the GUI text and I've already got them set up the way I need them I just need a script turned them in the button and I can apply a simple script to make the button Move without having post-processing effects
     
  4. ryand-unity

    ryand-unity

    Joined:
    Jan 21, 2011
    Posts:
    515
    ok i got it iwas on the right track when i started the script and i finished it. so for any one who needs it here you go.

    Code (csharp):
    1. #pragma strict
    2. var normalText : Color;
    3.  var hoverText : Color;
    4.  var pressedText : Color;
    5.  var messagee : GameObject;
    6.  var message = "ButtonPressed";
    7.  
    8.  private var state = 0;
    9.  private var myGUIText : GUIText;
    10.  
    11.  myGUIText = GetComponent(GUIText);
    12.  
    13.  function OnMouseEnter()
    14.  {
    15.     state++;
    16.     if (state == 1)
    17.         myGUIText.color = hoverText;
    18.  }
    19.  
    20.  function OnMouseDown()
    21.  {
    22.     state++;
    23.     if (state == 2)
    24.         myGUIText.color = pressedText;
    25.  }
    26.  
    27.  function OnMouseUp()
    28.  {
    29.     if (state == 2)
    30.     {
    31.         state--;
    32.         if (messagee)
    33.             messagee.SendMessage(message, gameObject);
    34.     }
    35.     else
    36.     {
    37.         state --;
    38.         if (state < 0)
    39.             state = 0;
    40.     }
    41.     myGUIText.color = normalText;
    42.  }
    43.  
    44.  function OnMouseExit()
    45.  {
    46.     if (state > 0)
    47.         state--;
    48.     if (state == 0)
    49.         myGUIText.color = normalText;
    50.  }
     
  5. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    If you want to use GUIText and have the same functions as a button would be here start off with something like this and apply it to the GUIText!
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HelpScript : MonoBehaviour
    5. {
    6.     void OnMouseDown()
    7.     {
    8.         //put lines of code in here
    9.     }
    10. }
     
    Last edited: Dec 30, 2013
  6. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    Also the code I copied is in C# so you will have to name the script HelpScript or it'll give you an error or just change the public class from HelpScript to whatever you like but it has to match the name you define for Javascript here it's basically the same thing.
    Code (csharp):
    1. function OnMouseDown()
    2. {
    3.     //put lines of code here
    4. }
     
  7. LilacGear

    LilacGear

    Joined:
    Dec 4, 2013
    Posts:
    5
    very helpful
     
  8. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Why not just use GUI.Button, set the text to what ever you want, create a custom GUIskin and delete the background image for the button.... and then hey presto you have your text button!