Some Helpful Unity Scripts from Morty

By: Josh

Morty the Matador grew into a larger project than we had initially anticipated, and as a developer, it helps to write a few tools and scripts that can make development smoother. Ever since I started using Unity's Editor API, I've always been looking for scripts to write that would speed up my workflow, not just for Morty, but for any project. Here are a few scripts that I think any Unity developer should always import into a new project.

1) The Level Selector

I found this script a little while ago, and I've been using it ever since. It's called ScriptWriter by Benjamin Ma, and it's from his website, which unfortunately seems to be down. Basically, what it does is it writes another script, that will add to a "Levels" menu in your editor with a list of all of the scenes that have been added to your build settings. This makes it easy to switch between scenes without having to look for the scene you want to load in the Project view.

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;

/**
 * This script was created by Benjamin Ma, and is licensed under:
 *
 * CREATIVE COMMON - Attribution 4.0 International
 *
 * Attribution — You must give appropriate credit, provide a link to the license,
 * and indicate if changes were made. You may do so in any reasonable manner,
 * but not in any way that suggests the licensor endorses you or your use.
 *
 *
 * Share — copy and redistribute the material in any medium or format
 * Adapt — remix, transform, and build upon the material
 * for any purpose, even commercially.
 * The licensor cannot revoke these freedoms as long as you follow the license terms.
 *
 *
 * No additional restrictions — You may not apply legal terms or technological measures
 * that legally restrict others from doing anything the license permits.
 */
public class ScriptWriter : Editor
{
    public static void WriteMapOpener()
    {
        string s =  "using UnityEditor;" + " " +
            "using UnityEngine;" + " " +
                "using System.Collections.Generic;" + " " +
                " " +
                "public class MapOpener : Editor" + " " +
                "{" + " ";
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            if (scene.enabled)
            {
                s += "    [MenuItem("Levels/" + PathToSceneName(scene.path) + "")]" + " " +
                    "    static void Open" + PathToSceneName(scene.path) + "()" + " " +
                        "    {" + " " +
                        "        OpenIf("" + scene.path + "");" + " " +
                        "    }" + " ";
            }
        }
        
        s +=    "                                                                   " + " " +
            "    private static void OpenIf(string level)                       " + " " +
                "    {                                                              " + " " +
                "        if (EditorApplication.SaveCurrentSceneIfUserWantsTo())     " + " " +
                "        {                                                          " + " " +
                "            EditorApplication.OpenScene(level);                    " + " " +
                "        }                                                          " + " " +
                "    }                                                              " + " " +
                "}                                                                  ";
        
        
        
        
        File.WriteAllText(Application.dataPath + "/Editor/MapOpener.cs", s);
        AssetDatabase.Refresh();
    }
    
    [MenuItem("Levels/Update Scene List", false, 0)]
    static void UpdateList()               
    {                                      
        ScriptWriter.WriteMapOpener();     
    }
    
    static string PathToSceneName(string path)
    {
        string path2 = path.Replace('-', '_');
        string[] folders = path2.Split('/');
        string s = folders[folders.Length - 1].Split('.')[0];
        string[] spacesRemoved = s.Split(' ');
        s = "";
        foreach (string str in spacesRemoved)
            s += str;
        return s;
    }
}


2) The Enabler/Disabler

I think this is the most useful helper script I've written so far. What this script does is very simple, it just enables all the selected objects or disables all the selected objects with a press of Ctrl+Q. This hotkey makes it so much quicker to prepare a scene for testing when you have to enable or disable several objects. I know that doesn't sound like it wouldn't make much of a difference, but it's saved me a lot of time.

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

public class UnityUtils
{
    [MenuItem("UnityUtils/ToggleObject %q")]
    public static void ToggleObjectActive() {
        GameObject[] selectedObjects = Selection.gameObjects;
        if (selectedObjects != null && selectedObjects.Length > 0) {
            bool allStatus = selectedObjects[0].activeSelf;
            for (int i = 0; i < selectedObjects.Length; i++) {
                if (selectedObjects[i].activeSelf != allStatus) {
                    allStatus = false;
                }
            }
            
            allStatus = !allStatus;
            Undo.RecordObjects(selectedObjects, "SetActive");
            for (int i = 0; i < selectedObjects.Length; i++) {
                selectedObjects[i].SetActive(allStatus);
                EditorUtility.SetDirty(selectedObjects[i]);
            }
        }
    }
}


3) The Screenshotter

This script helps for Q&A as well as for preparing for a game launch. What it does is it adds a menu that allows you to take a screenshot of the game scene, either at the current resolution or at a blown up resolution. This is particularly helpful when creating screenshots for a target resolution even if you don't own a device with that resolution. Just set the correct aspect ratio in the Unity Editor, then take a blown up screenshot, then scale it down to the right resolution with your preferred image editing tool.

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

public class Screenshotter : MonoBehaviour {
    
    [MenuItem ("Screenshot/High Res Screenshot")]
    static void DoSomething() {
        Application.CaptureScreenshot("HDScreenshot_" + UnityEngine.Random.Range(10000, 99999) + ".png", 4);
    }

    [MenuItem ("Screenshot/Random Screenshot")]
    static void RandomScreenshot() {
        Application.CaptureScreenshot("Screenshot_" + UnityEngine.Random.Range(10000, 99999) + ".png");
    }
}
#endif


That's all for now. Let me know in the comments if you guys have any ideas for an interesting or helpful utility that could be useful and if I find the time, I might just write one up.

Stay tuned for more interesting stuff and more news on Morty the Matador!




DISCUSSIONS