DEV Community

uni928
uni928

Posted on

[Unity] Automatically cancel a build if there are missing gameobjects.

Have you ever had trouble with missing gameobjects?

There are many assets out there that search for missing gameobjects, but surprisingly there is no mechanism to automatically cancel a build when there are missing gameobjects in the scene you are building.

So this time we would like to provide just that mechanism.
No other operations are required other than implementing this system, and it does not need to be run every time, so we hope you will consider implementing it.

A unitypackage version is also be published, so please consider that if you want an easy installation.


https://drive.google.com/file/d/1sNoFsd5nST2H-uCkTlmhwkgEcV_wW9Wa/view?usp=sharing

Download the unitypackage version from the URL above.

You can delete any files except SephirothStopOutputAtMissing.cs.
We recommend deleting files such as HowToUse.pdf, as they are large in size.


Now let's create the mechanism.
Create an Editor folder and create a new SephirothStopOutputAtMissing.cs in it with the following content.

#pragma warning disable 0618

namespace SephirothTools
{
    public class SephirothStopOutputAtMissing : UnityEditor.Build.IPreprocessBuild
    {

        public void OnPreprocessBuild(UnityEditor.BuildTarget target, string path)
        {
            string currentScenePath = UnityEngine.SceneManagement.SceneManager.GetActiveScene().path;
            foreach (var oneScene in UnityEditor.EditorBuildSettings.scenes)
            {
                UnityEditor.SceneManagement.EditorSceneManager.OpenScene(oneScene.path);

                foreach (var oneGameObject in UnityEngine.Object
            .FindObjectsOfType<UnityEngine.GameObject>(true))
                {
                    foreach (var oneComponent in oneGameObject.GetComponents<UnityEngine.Component>())
                    {
                        if (!oneComponent)
                        {
                            throw new UnityEditor.Build.BuildFailedException(oneScene.path + "'s objectName: " + oneGameObject.name + " is missing. Stop the app's output.");
                        }

                        UnityEditor.SerializedObject serializedObject = new UnityEditor.SerializedObject(oneComponent);

                        serializedObject.ApplyModifiedProperties();

                        UnityEditor.SerializedProperty property = serializedObject.GetIterator();

                        while (property.Next(true))
                        {
                            if (property.propertyType == UnityEditor.SerializedPropertyType.ObjectReference && property.objectReferenceValue == null && property.objectReferenceInstanceIDValue != 0)
                            {
                                throw new UnityEditor.Build.BuildFailedException(oneScene.path + "'s objectName: " + oneGameObject.name + " is missing. Stop the app's output.");
                            }
                        }
                    }
                }
            }
            UnityEditor.SceneManagement.EditorSceneManager.OpenScene(currentScenePath);
        }

        public int callbackOrder { get { return 0; } }
    }
}
Enter fullscreen mode Exit fullscreen mode

You can see that when there is a missing gameobject in the scene to be built,
throw new UnityEditor.Build.BuildFailedException
is called.

When BuildFailedException is thrown,
the build is automatically canceled.
I've created it so that you can see which gameobject is missing in the log.


Afterword

The above is the mechanism to automatically cancel the build when there is a missing gameobject.

Once you have it installed, you don't need to do anything else, which is very convenient.

I hope this mechanism will be helpful in your development.
Thank you for reading.

Top comments (0)