由于模型不停更改,需要频繁手动添加uLipSync组件,花时间写了编辑器代码去处理
using uLipSync;
using UnityEditor;
using UnityEditor.Events;
using UnityEngine;
using static uLipSync.uLipSyncBlendShape;/// <summary>
/// 模型添加uLipSync组件
/// </summary>
public class AdduLipSyncComponent : EditorWindow
{private GameObject selectedObject;private AudioSource audioSource;private SkinnedMeshRenderer skinnedMeshRenderer;/// <summary>/// 男的/// </summary>private bool isMale = false;[MenuItem("Tools/模型添加uLipSync组件")]public static void ShowWindow(){GetWindow<AdduLipSyncComponent>("模型添加uLipSync组件");}private void OnGUI(){GUILayout.Label("选择模型:", EditorStyles.boldLabel);selectedObject = EditorGUILayout.ObjectField(selectedObject, typeof(GameObject), true) as GameObject;EditorGUILayout.Space();GUILayout.Label("选择音源:", EditorStyles.boldLabel);audioSource = EditorGUILayout.ObjectField(audioSource, typeof(AudioSource), true) as AudioSource;EditorGUILayout.Space();GUILayout.Label("选择口形动画SkinnedMeshRenderer:", EditorStyles.boldLabel);skinnedMeshRenderer = EditorGUILayout.ObjectField(skinnedMeshRenderer, typeof(SkinnedMeshRenderer), true) as SkinnedMeshRenderer;EditorGUILayout.Space();isMale = EditorGUILayout.Toggle("男的", isMale);EditorGUILayout.Space();if (GUILayout.Button("添加组件") && selectedObject != null && audioSource != null && skinnedMeshRenderer != null){AddComponent();}EditorGUILayout.Space();if (GUILayout.Button("运行")){EditorApplication.isPlaying = true;}}void AddComponent(){//给音频组件添加uLipSyncAudioSourceuLipSyncAudioSource uLipSyncAudioSource = audioSource.gameObject.GetComponent<uLipSyncAudioSource>();if (uLipSyncAudioSource != null){DestroyImmediate(uLipSyncAudioSource);}uLipSyncAudioSource = audioSource.gameObject.AddComponent<uLipSyncAudioSource>();uLipSyncBlendShape uLipSyncBlendShape = selectedObject.GetComponent<uLipSyncBlendShape>();if (uLipSyncBlendShape != null){DestroyImmediate(uLipSyncBlendShape);}uLipSyncBlendShape = selectedObject.AddComponent<uLipSyncBlendShape>();uLipSyncBlendShape.skinnedMeshRenderer = skinnedMeshRenderer;uLipSyncBlendShape.minVolume = -3f;uLipSyncBlendShape.maxVolume = -2f;uLipSyncBlendShape.smoothness = 0.05f;#region AIUEON-BlendShapeInfo A = new BlendShapeInfo();A.phoneme = "A";A.maxWeight = 1f;A.index = 1;uLipSyncBlendShape.blendShapes.Add(A);BlendShapeInfo I = new BlendShapeInfo();I.phoneme = "I";I.maxWeight = 1f;I.index = 3;uLipSyncBlendShape.blendShapes.Add(I);BlendShapeInfo U = new BlendShapeInfo();U.phoneme = "U";U.maxWeight = 1f;U.index = 5;uLipSyncBlendShape.blendShapes.Add(U);BlendShapeInfo E = new BlendShapeInfo();E.phoneme = "E";E.maxWeight = 1f;E.index = 2;uLipSyncBlendShape.blendShapes.Add(E);BlendShapeInfo O = new BlendShapeInfo();O.phoneme = "O";O.maxWeight = 1f;O.index = 4;uLipSyncBlendShape.blendShapes.Add(O);BlendShapeInfo N = new BlendShapeInfo();N.phoneme = "N";N.maxWeight = 1f;uLipSyncBlendShape.blendShapes.Add(N);BlendShapeInfo ms = new BlendShapeInfo();ms.phoneme = "-";ms.maxWeight = 1f;uLipSyncBlendShape.blendShapes.Add(ms);#endregionuLipSync.uLipSync uls = selectedObject.GetComponent<uLipSync.uLipSync>();if (uls != null){DestroyImmediate(uls);}uls = selectedObject.AddComponent<uLipSync.uLipSync>();uls.outputSoundGain = 1f;uls.audioSourceProxy = uLipSyncAudioSource;
#if UNITY_WEBGLuls.autoAudioSyncOnWebGL = true;uls.audioSyncOffsetTime = 0f;
#endifUnityEventTools.AddPersistentListener(uls.onLipSyncUpdate, uLipSyncBlendShape.OnLipSyncUpdate);if (isMale){uls.profile = AssetDatabase.LoadAssetAtPath<Profile>("Assets/uLipSync/Assets/Profiles/uLipSync-Profile-Sample-Male.asset");}else{uls.profile = AssetDatabase.LoadAssetAtPath<Profile>("Assets/uLipSync/Assets/Profiles/uLipSync-Profile-Sample-Female.asset");}}
}