Search Unity

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?