zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

编辑器调试工具开发 实战 Unity window Editor
2023-09-27 14:19:52 时间

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