Unity 에디터 개발 전투 [에디터 창] - 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();
        }
    }
}

  공개 계정 "Contemporary Wild Programmer"에 오신 것을 환영합니다.

추천

출처blog.csdn.net/qq_42139931/article/details/123496037