Website may be up and down over next few months. I'm currently doing a complete overhaul of everything. Going back to simple individual .htm pages, new overall site theme, sanitizing and cleaning up html of all pages and blog posts, attempting to implement a new tooling and publishing system etc etc.

The code below allows you to set and automatically restore the GUI.enabled state when used with a using block similar to GUILayout.HorizontialScope.

/// <summary>
/// Provides  a class for setting and restoring GUI.enabled.
/// </summary>
public class GuiEnabled : IDisposable
{
    /// <summary>
    /// Gets or sets a value indicating the state that is to be restored.
    /// </summary>  
    public bool StateToBeRestored { get; set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="GuiEnabled"/> class.
    /// </summary>
    /// <param name="stateToBeRestored">Value indicating the state that is to be restored.</param>
    public GuiEnabled(bool stateToBeRestored)                                            
    {
        this.StateToBeRestored = stateToBeRestored;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="GuiEnabled"/> class.
    /// </summary>
    /// <param name="stateToBeRestored">If set to <c>true</c> the GUI.enabled state will be restored to this value.</param>
    /// <param name="setState">Immediatley sets the value of GUI.enabled to this state.</param>
    public GuiEnabled(bool stateToBeRestored, bool setState) : this(stateToBeRestored)
    {
        GUI.enabled = setState;
    }

    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    public void Dispose()
    {
        GUI.enabled = this.StateToBeRestored;
    }
}

Here is an simple use case

GUILayout.Button("Enabled button");
using (var enabled = new GuiEnabled(GUI.enabled, false))
{
    GUILayout.Button("Disabled button");
    //if (someCondition)
    //{   
        // setting to false means after we exit the using block GUI.enabled will be set to false. 
    //    enabled.StateToBeRestored = false; 
    //}
}
GUILayout.Button("Enabled button");

Add comment



biuquote
  • Comment
  • Preview
Loading






Created by: X

Just another personal website in this crazy online world

Name of author Dean Lunz (aka Created by: X)
Computer programming nerd, and tech geek.
About Me -- Resume