Unity 编辑器开发实战【Editor Window】- BlendShape调试工具

Skin Mesh Renderer组件编辑器本身包含BlendShape的调试滑动条,但是当数量较多想要重置时较为麻烦,下面介绍的工具添加了这些调试滑动条的同时,增加了一键重置的功能:

代码如下:

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();
        }
    }
}

  欢迎关注公众号 “当代野生程序猿”

猜你喜欢

转载自blog.csdn.net/qq_42139931/article/details/123496037