Unity editor development combat [Editor Window] - BlendShape debugging tool

The Skin Mesh Renderer component editor itself contains BlendShape's debugging sliders, but it is troublesome to reset when there are a large number of them. The tools described below add these debugging sliders and add a one-click reset function:

code show as below:

using UnityEngine;
using UnityEditor;

namespace SK.Framework
{
    /// <summary>
    /// BlendShape调试工具
    /// </summary>
    public class BlendShapesPreviewer : EditorWindow
    {
        //菜单
        [MenuItem("SKFramework/Tools/BlendShapes Previewer")]
        private static void Open()
        {
            GetWindow<BlendShapesPreviewer>("BlendShapes Previewer").Show();
        }

        private Vector2 scroll = Vector2.zero;

        private void OnGUI()
        {
            if (Selection.activeGameObject == null)
            {
                EditorGUILayout.HelpBox("未选中任何物体", MessageType.Info);
                return;
            }
            SkinnedMeshRenderer smr = Selection.activeGameObject.GetComponent<SkinnedMeshRenderer>();
            if (smr == null)
            {
                EditorGUILayout.HelpBox("物体不包含SkinnedMeshRenderer组件", MessageType.Info);
                return;
            }
            Mesh mesh = smr.sharedMesh;
            if(mesh == null)
            {
                EditorGUILayout.HelpBox("Mesh为空", MessageType.Info);
                return;
            }
            int count = mesh.blendShapeCount;
            if (count == 0)
            {
                EditorGUILayout.HelpBox("BlendShape Count: 0", MessageType.Info);
                return;
            }
            scroll = EditorGUILayout.BeginScrollView(scroll);
            {
                for (int i = 0; i < count; i++)
                {
                    //水平布局
                    GUILayout.BeginHorizontal();
                    //BlendShape名称
                    GUILayout.Label(mesh.GetBlendShapeName(i), GUILayout.Width(150f));
                    //滑动条
                    float newValue = EditorGUILayout.Slider(smr.GetBlendShapeWeight(i), 0f, 100f);
                    if (newValue != smr.GetBlendShapeWeight(i))
                    {
                        smr.SetBlendShapeWeight(i, newValue);
                    }
                    GUILayout.EndHorizontal();
                }
            }
            EditorGUILayout.EndScrollView();

            GUILayout.FlexibleSpace();
            //重置按钮 点击时将所有BlendShape值设为0
            if (GUILayout.Button("Reset"))
            {
                for (int i = 0; i < count; i++)
                {
                    smr.SetBlendShapeWeight(i, 0);
                }
            }
        }
        //选择的物体变更时调用重新绘制方法
        private void OnSelectionChange()
        {
            Repaint();
        }
    }
}

  Welcome to the public account "Contemporary Wild Programmer"

Guess you like

Origin blog.csdn.net/qq_42139931/article/details/123496037