Skip to content

Instantly share code, notes, and snippets.

@Ryxali
Last active May 2, 2022 09:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryxali/33fa23ec833b2b64aae88e616e0f03bb to your computer and use it in GitHub Desktop.
Save Ryxali/33fa23ec833b2b64aae88e616e0f03bb to your computer and use it in GitHub Desktop.
Enforcing a specific transition configuration for all transitions in a RuntimeAnimationController
using UnityEngine;
using UnityEditor;
using System.Text.RegularExpressions;
using System.IO;
// Drop this in some Editor folder
public static class AnimatorControllerTool {
[MenuItem("CONTEXT/RuntimeAnimatorController/Enforce Transition Settings")]
public static void FixTransitions(MenuCommand command)
{
var path = AssetDatabase.GetAssetPath(command.context);
path = Regex.Replace(path, "^Assets", Application.dataPath);
AssetDatabase.StartAssetEditing();
try
{
// We can't use serialization to read the properties, so we rely on editing the asset text file instead
// This naturally requires asset files to be serialized as text
// You can achieve this by Edit/Project Settings with Asset Serialization Mode set to Force Text
var txt = File.ReadAllText(path);
txt = Regex.Replace(txt, @"(?<param>m_HasExitTime:\s+)\d+", "${param}0");
txt = Regex.Replace(txt, @"(?<param>m_HasFixedDuration:\s+)\d+", "${param}1");
txt = Regex.Replace(txt, @"(?<param>m_TransitionDuration:\s+)[\.\d]+", "${param}0");
txt = Regex.Replace(txt, @"(?<param>m_TransitionOffset:\s+)[\.\d]+", "${param}0");
File.WriteAllText(path, txt);
}
catch(System.Exception e) { Debug.LogException(e); }
AssetDatabase.StopAssetEditing();
AssetDatabase.Refresh();
}
}
@Ryxali
Copy link
Author

Ryxali commented May 31, 2019

BEWARE that this operation can be really destructive as it manually overwrites parts of the asset data in a really hacky way. So backup your asset before attempting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment