rrazgriz / RATS

Harmony-based Unity Editor QOL
https://vpm.razgriz.one
MIT License
83 stars 7 forks source link

Feature: Blendtree Asset Exporting & Creation #7

Open ZenithVal opened 1 year ago

ZenithVal commented 1 year ago

Blendtrees made in the animator cannot be utilized outside the source animator unless copy pasted. That's painful because changes will not propagate. An option to export the selected blendtree as a standalone asset would be very nice.

Asset Blendtrees available in the right click create menu might also be appreciated. image

Some code @rurre assembled for specifically that a bit ago:

//Original from VRCAvatars3Tools by gatosyocora
//Simplified for just Blend Trees by Pumkin

using System.IO;
using UnityEditor;
using UnityEditor.Animations;

namespace CreateBlendTree
{
    public class CreateBlendTree : Editor
    {
        [MenuItem("Assets/Create/BlendTree", priority = 411)]
        private static void CreateNewBlendTree()
        {
            var folder = Selection.activeObject;
            var folderPath = AssetDatabase.GetAssetPath(folder);
            var assetPath = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(folderPath, "BlendTree.asset"));

            var asset = new BlendTree
            {
                name = Path.GetFileNameWithoutExtension(assetPath)
            };
            AssetDatabase.CreateAsset(asset, assetPath);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            Selection.activeObject = asset;
        }
    }
}
rurre commented 1 year ago

In general, there is no reason for blend trees to not be exportable since you can just save them to assets like any other object. If you open an animator in a text editor and set the m_ObjectHideFlags to 0 on a blend tree you can see it as a sub asset and duplicate it in the editor, so should be easy to do in script.

Also, updated the BlendTree creation code to be more in line with how other assets get created.

// CreateBlendTree.cs by Pumkin
// https://github.com/rurre

#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEditor.Animations;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;

namespace CreateBlendTree
{
    public class CreateBlendTree : Editor
    {
        [MenuItem("Assets/Create/BlendTree", priority = 411)]
        static void CreateNewBlendTree()
        {
            ProjectWindowUtil.StartNameEditingIfProjectWindowExists(
               0,
               CreateInstance<DoCreateBlendTree>(),
               "New BlendTree.asset",
               EditorGUIUtility.IconContent("BlendTree Icon").image as Texture2D,
               null);
        }

        class DoCreateBlendTree : EndNameEditAction
        {
            public override void Action(int instanceId, string pathName, string resourceFile)
            {
                BlendTree blendTree = new BlendTree { name = Path.GetFileNameWithoutExtension(pathName) };
                AssetDatabase.CreateAsset(blendTree, pathName);
                Selection.activeObject = blendTree;
            }
        }
    }
}
#endif