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

2022-08-29 16:48:35 浏览数 (1)

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

代码如下:

代码语言:javascript复制
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组件           
            SkinnedMeshRenderer smr = Selection.activeGameObject.GetComponent<SkinnedMeshRenderer>();
            if (smr == null)
            {
                EditorGUILayout.HelpBox("物体不包含SkinnedMeshRenderer组件", MessageType.Info);
                return;
            }
            //检测Mesh是否为空
            Mesh mesh = smr.sharedMesh;
            if(mesh == null)
            {
                EditorGUILayout.HelpBox("Mesh为空", MessageType.Info);
                return;
            }
            //检测BlendShape数量 是否为0
            int count = mesh.blendShapeCount;
            if (count == 0)
            {
                EditorGUILayout.HelpBox("BlendShape Count: 0", MessageType.Info);
                return;
            }
            //滚动视图
            scroll = EditorGUILayout.BeginScrollView(scroll);
            {
                //遍历所有BlendShape
                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();
        }
    }
}

0 人点赞