Skip to content

Instantly share code, notes, and snippets.

@aholkner
Last active March 8, 2019 23:13
Show Gist options
  • Save aholkner/6462828d3e9bb0b67d65e7c7b723c8e8 to your computer and use it in GitHub Desktop.
Save aholkner/6462828d3e9bb0b67d65e7c7b723c8e8 to your computer and use it in GitHub Desktop.
Unity editor script that warns the user when an AnimatorController asset changes on disk.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
// Detect when .controller assets are modified on disk, and give the user a warning so they
// can restart Unity to pick up the changes.
//
// Workaround for bug #1133706 (AnimatorController assets are not reimported if they contain
// attached StateMachineBehaviour).
public class AnimatorControllerChangeDetector : AssetPostprocessor
{
static DateTime lastSaveTime;
static bool IsAnimatorControllerAsset(string assetPath)
{
return assetPath.EndsWith(".controller");
}
public class SaveProcessor : UnityEditor.AssetModificationProcessor
{
static string[] OnWillSaveAssets(string[] paths)
{
lastSaveTime = DateTime.Now;
return paths;
}
}
public class AssetProcessor : AssetPostprocessor
{
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
// Ignore changes due to save. This is obviously a pretty wild hack, but we can't match up
// whether an asset import is due to save or not, as this function may be called multiple
// times for a single asset save.
if ((DateTime.Now - lastSaveTime) < TimeSpan.FromSeconds(1))
return;
bool hasModifications = false;
foreach (string asset in importedAssets)
{
if (IsAnimatorControllerAsset(asset))
{
hasModifications = true;
UnityEngine.Debug.LogWarning($"{asset} has changed on disk; restart Unity");
}
}
if (hasModifications)
EditorUtility.DisplayDialog("Files changed on disk", "AnimatorController assets have changed on disk, and Unity may not have reloaded them. Recommend you restart Unity.", "Hooray");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment