简体   繁体   中英

How can I check if a flag is either false/true and then to decide if to enable false/true a button in OnGUI?

The script is EditorWindow type.

The variable wasUndo is global type bool. I want to use this flag to help me to decide when to enable false/true the button "Undo last".

The problem is that it's inside OnGUI and it's doing all the time the loop. I want that if the json file exist and the file size is large then 0 and if one or more of the objects in the selections List is null then enable true the button and then when clicking the button once set the button enabled false again.

The way it is now when I delete an object in the Hierarchy which is part of the selections List the button become enable true but then when I click the button he become enable false again and never turn enable true.

private void OnGUI
{

    //The saving part:

    var selections = Selection.objects.OfType<GameObject>().ToList();
    if (selections.Count > 0)
    {
        for (var i = selections.Count - 1; i >= 0; --i)
        {
            var selected = selections[i];
            transformSelection.Add(selected.transform);
        }

        TransformSaver.SaveTransform(transformSelection.ToArray());

        tempTransformSelection = transformSelection;
        transformSelection = new List<Transform>();
    }

    //The loading part:

    var file = @"d:\json\json.txt";
    FileInfo fi = new FileInfo(file);

    for (int i = 0; i < tempTransformSelection.Count(); i++)
    {
        if (File.Exists(@"d:\json\json.txt") 
            && fi.Length > 0 
            && tempTransformSelection[i] == null 
            && wasUndo == false)
        {
            GUI.enabled = true;
            wasUndo = false;
        }
        else
        {
            GUI.enabled = false;
        }
    }

    if (GUILayout.Button("Undo last"))
    {
        TransformSaver.LoadTransform();
        wasUndo = true;
    }
}

You can make the drawing of the Button depend on wasUndo . But actually you don't need a global variable for that. I just used showButton .. if you want you can stick to your wasUndo anyway just using it the same way

private void OnGUI
{
    var file = @"d:\json\json.txt";
    FileInfo fi = new FileInfo(file);

    // By default asume you don't want to show the button
    // it will than only be enabled if the later conditions match
    bool showButton = false;

    // you can check this already here not in the loop
    // if no file -> nothing to do
    if(!File.Exists(@"d:\json\json.txt") || fi.Length <= 0) return;


    // This is only reached if the file exists and is not empty

    // check for null transforms
    for (int i = 0; i < tempTransformSelection.Count(); i++)
    {
        // if not null do nothing
        if(tempTransformSelection[i] != null) continue;

        // otherwise enable the button and leave the loop
        showButton = true;
        break;
    }

    // if not true then button won't be shown
    if(!showButton ) return;

    if (GUILayout.Button("Undo last"))
    {
        TransformSaver.LoadTransform();
        showButton = false;
    }
}

Update

If you want to just disable the button instead of making it disappear completely you can just wrap it in a EditorGUI.BeginDisabledGroup(bool disabled)

So instead

if(!showButton) return;

...

You would do

EditorGUI.BeginDisabledGroup(!showButton);

if(GUILayout.Button("..."){ //... }

EditorGUI.EndDisabledGroup();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM