WPF Tools, Unity and Asset Preprocessors

Scriptable Objects are cool. Managing large data in them can be a pain and Unity Editor Window coding is also less than ideal and, in my opinion, pretty slow when it comes to iteration time.

WPF, however, is a nice and quick way to get tools made.

In our next Beehive Game (see what I did there), we have a large scriptable object that manages when enemies spawn in the level and what time they spawn. It is large so without custom tools managing it in Eidtor can be a bit tricky.

I knocked up a data structure inside of Unity and linked it to a WPF project where I built this tool:

This lets me add ships spawning at points and certain times. When I hit save I save it into and editor folder within unity called “Raw” as a .gamdat file.

I then have a custom asset pre processor script when a .gamdat is added to the project.

using UnityEngine;
using UnityEditor;
using System.IO;

public class GameDataAssetPostProcessor : AssetPostprocessor
{
    void OnPreprocessAsset()
    {
        if (assetPath.EndsWith(".gamdat"))
        {
            SpawnDataSO data = AssetDatabase.LoadAssetAtPath<SpawnDataSO>("Assets/Resources/GameData.Asset");
            string path = Path.GetFullPath(assetPath);
            data.SpawnData = JsonUtility.FromJson<SpawnData>(File.ReadAllText(path));
            EditorUtility.SetDirty(data);
            AssetDatabase.SaveAssets();
        }
    }
}

The above loads up a scriptable object that contains the shared data, parses the JSON into it and writes it back in and as this JSON file is stored in an editor folder it does not include the raw data.

Cool right?

You could also use the above technique for iteration outside of Unity.

If you are trying to avoid every designer on your team needing a Unity license (as that gets expensive real quickly, especially if you have a big team) you can have a custom build that loads the JSON from Streaming Assets.

You run the build and WPF tool in tandem and save it to the games StreamingAssets file and have a button to reload the data within your custom build.

Once you are happy with a data set, this can then be imported to the main project for your release build, either manually, via an in editor option that downloads it from an online source or as part of your build process in Jenkins, TeamCity, etc