In my current project I had the need to perform some action when a build happens or the play button was pressed, so I tinkered around a bit and came up with this (C#):
/* Put in Assets/Editor */
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class OnBuildAction
{
static bool stopIsPlaying;
static bool stopIsCompiling;
static OnBuildAction()
{
EditorApplication.update += trigger;
}
static void trigger()
{
if (!stopIsCompiling EditorApplication.isCompiling)
{
Debug.Log("Performing on build operations");
stopIsCompiling = true;
onBuild();
}
if (!stopIsPlaying EditorApplication.isPlayingOrWillChangePlaymode)
{
Debug.Log("Performing on build operations");
stopIsPlaying = true;
onBuild();
}
stopIsCompiling = EditorApplication.isCompiling;
stopIsPlaying = EditorApplication.isPlayingOrWillChangePlaymode;
}
static void onBuild()
{
/* what you need to do on build */
}
}
onBuild will be called exactly once per time you press play, hit build or hit build and run.