using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.Linq; public class BlendShapeExample : MonoBehaviour { private int blendShapeCount; private SkinnedMeshRenderer skinnedMeshRenderer; private Mesh skinnedMesh; private Dictionary shapekeyIndex = new Dictionary(); private Dictionary shapekeyIndexR = new Dictionary(); private Dictionary> correctiveIndex = new Dictionary>(); void Awake() { this.skinnedMeshRenderer = GetComponent(); this.skinnedMesh = GetComponent().sharedMesh; } void Start() { this.blendShapeCount = skinnedMesh.blendShapeCount; for (int i = 0; i < this.blendShapeCount; i++) { string s = this.skinnedMesh.GetBlendShapeName(i); this.shapekeyIndex.Add(i, s); this.shapekeyIndexR.Add(s, i); } for (int i = 0; i < this.blendShapeCount; i++) { string s = this.skinnedMesh.GetBlendShapeName(i); if (s.Contains('_')) { var ctrls = new List(); foreach (var sub in s.Split('_', StringSplitOptions.RemoveEmptyEntries)) { ctrls.Add(shapekeyIndexR[sub]); } this.correctiveIndex.Add(i, ctrls); } } } void Update() { foreach (var item in this.correctiveIndex) { float weight = 1.0f; foreach (var index in item.Value) { weight *= this.skinnedMeshRenderer.GetBlendShapeWeight(index) * 0.01f; } this.skinnedMeshRenderer.SetBlendShapeWeight(item.Key, weight * 100); } } }