Inspector Lock Toggle

You know that tiny “Lock” icon at the right top corner of the Inspector tab, right? Then you probably know that it is pretty much impossible to click on it with an average trackpad on an average laptop? Well, I do! So I was very excited when I saw the “EasyShortcutLockInspector” asset in the Asset Store and decided to add the same functionality to the ExtraTools. Here how it is done:

[MenuItem("Extra Tools/Toggle Inspector Lock %l")]
private static void ToggleInspectorLock()
{
    ActiveEditorTracker.sharedTracker.isLocked = !ActiveEditorTracker.sharedTracker.isLocked;
    ActiveEditorTracker.sharedTracker.ForceRebuild();
}

Just copy-paste this method anywhere in your project and use CTRL+L to toggle the lock of the Inspector window! Also, you can download it from the GitHub.

I will be adding some other stuff to the ExtraTools in the future. Let me know if you have something in mind!

A Better GetComponent() Method

At some point, I found myself writing the following code:

public class TestScript : MonoBehaviour
{
    private BoxCollider col_1;
    private BoxCollider col_2;
    private Text message;

    private void CheckComponents()
    {
        col_1 = GetComponent<BoxCollider>();
        if(!col_1)
        {
            Debug.LogWarning(string.Format("{0} does not have a component of type BoxCollider./nAdding one...", name));
            col_1 = gameObject.AddComponent<BoxCollider>();
        }

        col_2 = GetComponent<BoxCollider>();
        if (!col_2)
        {
            Debug.LogWarning(string.Format("{0} does not have a component of type BoxCollider./nAdding one...", name));
            col_2 = gameObject.AddComponent<BoxCollider>();
        }

        message = GetComponent<Text>();
        if (!message)
        {
            Debug.LogWarning(string.Format("{0} does not have a component of type Text./nAdding one...", name));
            message = gameObject.AddComponent<Text>();
        }
    }
}

See how quickly it can get very annoying and repetitive very fast? So, the obvious solution was to write a more functional and generic GetComponent<>() method. Here is what I did:

public static bool GetComponent<T>(this Transform t, out T component, bool needWarning = false, bool ensureComponent = false, bool addedMessage = false) where T : Component
{
    component = t.GetComponent<T>();
    if (!component)
    {
        if (needWarning)
            Debug.LogWarning(string.Format("{0} does not have a component of type {1}!", t.name, typeof(T)));

        if (ensureComponent)
        {
            component = t.gameObject.AddComponent<T>();
            if (addedMessage)
                Debug.Log(string.Format("Added a component of type {0} to {1}", typeof(T), t.name));
        }

    }

    return component;
}

And my ChceckComponents() method was quickly reduced to this:

public class TestScript : MonoBehaviour
{
    private BoxCollider col_1;
    private BoxCollider col_2;
    private Text message;

    private void CheckComponents()
    {
        transform.GetComponent(out col_1, true, true, true);
        transform.GetComponent(out col_2, false, true, false);
        transform.GetComponent(out message);
    }
}

Check out the GitHub repo and let me know if you have any suggestions or ideas for the next post!

Unity Console Clear Shortcut

Annoyed yet?

Are you compulsively spamming that “Clear” button on the console after every message? Well, then I have great news for you! Copy the following script, paste it anywhere you like and you will be able to compulsively spam CTRL+Q!

[MenuItem("Extra Tools/Clear Console %q")] // CTRL + Q
private static void ClearConsole()
{
    Assembly assembly = Assembly.GetAssembly(typeof(SceneView));
    Type type = assembly.GetType("UnityEditor.LogEntries");
    MethodInfo method = type.GetMethod("Clear");
    method.Invoke(new object(), null);
}

Or you can download it from GitHub.

I will be updating the project with some other useful stuff. Stay tuned and let me know if there is anything you would like me to add.

PS: As far as I can tell, there is no command for this in the new shortcut editor in 2019.1