Last active
May 21, 2024 08:57
-
-
Save brandonpollack23/833af6c777cd79905b6313a8acc78bae to your computer and use it in GitHub Desktop.
A unity editor script that adds a tool in the "tools" menu to generate code artifacts to access animator parameters instead of needing to know strings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO; | |
| using UnityEditor; | |
| using UnityEditor.Animations; | |
| public class AnimatorParameterGenerator | |
| { | |
| [MenuItem("Tools/Generate Animator Parameters")] | |
| public static void Generate() | |
| { | |
| string[] guids = AssetDatabase.FindAssets("t:AnimatorController"); | |
| foreach (string guid in guids) | |
| { | |
| string path = AssetDatabase.GUIDToAssetPath(guid); | |
| AnimatorController animatorController = AssetDatabase.LoadAssetAtPath<AnimatorController>(path); | |
| if (animatorController != null) | |
| { | |
| string savePath = System.IO.Directory.GetParent(path).FullName; | |
| GenerateParameterClass(animatorController, savePath); | |
| } | |
| } | |
| AssetDatabase.Refresh(); | |
| } | |
| private static void GenerateParameterClass(AnimatorController animatorController, string savePath) | |
| { | |
| string className = animatorController.name + "Parameters"; | |
| string filePath = Path.Combine(savePath, className + ".cs"); | |
| using (StreamWriter writer = new StreamWriter(filePath)) | |
| { | |
| writer.WriteLine("// Autogenerated by AnimatorParameterGenerator.cs"); | |
| writer.WriteLine("// DO NOT EDIT"); | |
| writer.WriteLine("namespace AnimatorParametersGenerated {"); | |
| writer.WriteLine("public static class " + className); | |
| writer.WriteLine("{"); | |
| foreach (var parameter in animatorController.parameters) | |
| { | |
| writer.WriteLine($" public const string {parameter.name} = \"{parameter.name}\";"); | |
| } | |
| writer.WriteLine("}"); | |
| writer.WriteLine("}"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment