Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

From onGui to new UI

Discussion in 'UGUI & TextMesh Pro' started by jamuk, Mar 12, 2015.

  1. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    I created some buttons in an OnGUI function script that rotated my object. I want to use the new UI instead.
    I've managed to create all my button graphics within Canvas but I'm not really understanding where to add the script to get them to work like my old OnGUI script - I guess what I'm asking is how do I convert my old onGUI script into new UI?
    If someone could please point me in the right direction it would be greatly appreciated!

    (old OnGUI script)

    var btnRotUp : Texture;

    functionOnGUI()
    {
    if (GUI.RepeatButton(Rect(19,31,22,22), btnRotUp)) {
    transform.Rotate(Vector3.right, -turnSpeed * Time.deltaTime, Space.World);
    UserDefinedOrientation = true;
    }
     
    Last edited: Mar 12, 2015
  2. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    so I'm trying this -

    var btnRotUp : UnityEngine.UI.Button;

    if (btnRotUp.GetComponent(Button)) {
    transform.Rotate(Vector3.right, -turnSpeed * Time.deltaTime, Space.World);
    UserDefinedOrientation = true;
    }

    and still no luck, any help appreciated!
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    In the new UI, your script will define functionality (what the button does), but not how it's displayed. (In the old OnGUI, your script defines both functionality and display.)

    Add a script with some functions like this:
    Code (csharp):
    1. var isButtonDown = false;
    2.  
    3. function OnPointerDown()
    4. {
    5.     isButtonDown = true;
    6. }
    7.  
    8. function OnPointerUp()
    9. {
    10.     isButtonDown = false;
    11. }
    12.  
    13. function Update()
    14. {
    15.     if (isButtonDown)
    16.     {
    17.         transform.Rotate(Vector3.right, -turnSpeed * Time.deltaTime, Space.World);
    18.         UserDefinedOrientation = true;
    19.     }
    20. }
    (I typed directly into the editor; may have typos.)

    On the UI button, add an Event Trigger for OnPointerDown. Assign the GameObject containing the script above. From the dropdown menu, select your OnPointerDown function. Repeat for the OnPointerUp event.

    So when the user presses the pointer down on the UI button, it will call your script's OnPointerDown function. When the user releases the button, it will call your script's OnPointerUp. These functions manage a Boolean that's checked in Update(). If you don't want to use Update(), you could run a coroutine instead.

    Tons more info here: http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-events-and-event-triggers
     
    SimonDarksideJ likes this.
  4. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    awesome thanks, ill give it a go.
    Although why Unity couldn't just create a repeat button for the new UI is beyond me!
     
  5. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Many Thanks TonyLi, it worked perfectly!
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Happy to help!
     
  7. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Hi TonyLi, can you tell me a bit more about the coroutine approach.
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    It would look something like this:
    Code (csharp):
    1. function OnPointerDown()
    2. {
    3.     Rotate();
    4. }
    5.  
    6. function OnPointerUp()
    7. {
    8.     StopAllCoroutines();
    9. }
    10.  
    11. function Rotate()
    12. {
    13.     while (true)
    14.     {
    15.         transform.Rotate(Vector3.right, -turnSpeed * Time.deltaTime, Space.World);
    16.         UserDefinedOrientation = true;
    17.         yield return null;
    18.     }
    19. }
    The OnPointerDown function starts the Rotate coroutine. The OnPointerUp function stops it. While the Rotate coroutine is active, it updates and then releases control until the next frame.

    Coroutines: https://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines
     
  9. jamuk

    jamuk

    Joined:
    Jun 5, 2013
    Posts:
    28
    Many Thanks TonyLi.