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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    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?