Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question About OnGui and GUI.Button

Discussion in 'Immediate Mode GUI (IMGUI)' started by skxatbj, Nov 5, 2022.

  1. skxatbj

    skxatbj

    Joined:
    Jun 24, 2018
    Posts:
    5
    OnGUI is called for rendering and handling GUI events.Your OnGUI implementation might be called several times per frame (one call per event)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour
    5. {
    6.     void OnGUI()
    7.     {
    8.         if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button"))
    9.         {
    10.             print("You clicked the button!");
    11.         }
    12.     }
    13. }
    GUI.Button will be called many times, why the button only be created once?