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

Question About OnGui and GUI.Button

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

  1. skxatbj


    Jun 24, 2018
    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;
    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?