0
\$\begingroup\$

How do I close the GUI after the user enters a value? Is there any way to stop execution of the entire app (from inside this script) until a value is entered?

using UnityEngine;
using System.Collections;
using UnityEngine;
using UnityEditor;

public class Popup : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    string record="";

    void OnGUI() {
        //Participant Number/Record:
        GUILayout.Label ("enter participant id:");
        record = GUILayout.TextField(record);
        //GUILayout.TextField()

        if (GUILayout.Button("Submit")) {
            OnClickSavePrefab();
            GUIUtility.ExitGUI();
        }
    }

    void OnClickSavePrefab() {
        record = record.Trim();

        if (string.IsNullOrEmpty(record)) {
            EditorUtility.DisplayDialog("Unable to save record", "Please specify a valid participant record.", "Close");
            return;
        }
        // Save your prefab
        Debug.Log ("record:" + record);
    }
}
\$\endgroup\$
  • \$\begingroup\$ We prefer to have only one question per question post. You could edit this question leave only one and ask another question with the second. \$\endgroup\$ – Alexandre Vaillancourt Jan 21 '16 at 22:19
1
\$\begingroup\$

Well, you can use simple flag to do so, as

using UnityEngine;
using System.Collections;
using UnityEngine;
using UnityEditor;

public class Popup : MonoBehaviour {

    bool _shouldDisplayGUI = true; // Make it false when you want to turn off GUI elements.

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    string record="";

    void OnGUI() {
        if (!_shouldDisplayGUI)
            return;

        //Participant Number/Record:
        GUILayout.Label ("enter participant id:");
        record = GUILayout.TextField(record);
        //GUILayout.TextField()

        if (GUILayout.Button("Submit")) {
            OnClickSavePrefab();
            GUIUtility.ExitGUI();
        }
    }

    void OnClickSavePrefab() {
        record = record.Trim();

        if (string.IsNullOrEmpty(record)) {
            EditorUtility.DisplayDialog("Unable to save record", "Please specify a valid participant record.", "Close");
            return;
        }
        // Save your prefab
        Debug.Log ("record:" + record);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.