public SortedDictionary <uint, uint> RenameBone(Transform bone, string oldName) { string newName = bone.m_GameObject.instance.m_Name; SortedDictionary <uint, uint> boneHashTranslation = new SortedDictionary <uint, uint>(); for (int i = 0; i < m_TOS.Count; i++) { var pair = m_TOS[i]; int beginPos = pair.Value.IndexOf(oldName); if (beginPos >= 0) { string begin = pair.Value.Substring(0, beginPos); string end = pair.Value.Substring(beginPos + oldName.Length); if ((beginPos == 0 || beginPos > 0 && begin[begin.Length - 1] == '/') && (end.Length == 0 || end[0] == '/')) { string path = begin + newName + end; uint hash = Animator.StringToHash(path); boneHashTranslation.Add(m_TOS[i].Key, hash); var newPair = new KeyValuePair <uint, string>(hash, path); m_TOS[i] = newPair; if (end.Length == 0) { hash = Animator.StringToHash(newName); uint oldHash = Animator.StringToHash(oldName); for (int j = 0; j < m_Avatar.m_SkeletonNameIDArray.Count; j++) { if (m_Avatar.m_SkeletonNameIDArray[j] == oldHash) { m_Avatar.m_SkeletonNameIDArray[j] = hash; break; } } } } } } if (boneHashTranslation.Count == 0) { Report.ReportLog("Warning! Avatar " + m_Name + " has no translation for Transform " + oldName + ". Reinserting."); AddBoneWithChilds(bone); RemoveBone(oldName); Transform parent = bone.Parent; foreach (var sibling in parent) { if (sibling.m_GameObject.instance.m_Name == oldName) { AddBoneWithChilds(sibling); break; } } } else { for (int i = 0; i < m_Avatar.m_AvatarSkeleton.m_ID.Count; i++) { uint newHash; if (boneHashTranslation.TryGetValue(m_Avatar.m_AvatarSkeleton.m_ID[i], out newHash)) { m_Avatar.m_AvatarSkeleton.m_ID[i] = newHash; } } HashSet <KeyValuePair <uint, string> > duplicates = new HashSet <KeyValuePair <uint, string> >(); m_TOS.Sort ( delegate(KeyValuePair <uint, string> x, KeyValuePair <uint, string> y) { if (x.Key > y.Key) { return(1); } else if (x.Key < y.Key) { return(-1); } return(0); } ); for (int i = 0; i < m_TOS.Count - 1; i++) { var pair = m_TOS[i]; if (pair.Key == m_TOS[i + 1].Key) { duplicates.Add(pair); } } if (duplicates.Count > 0) { foreach (var dup in duplicates) { Report.ReportLog("Warning! Removing duplicate of Transform path " + dup.Value + " in Avatar " + m_Name); m_TOS.Remove(dup); RemoveBoneFromAvatar(dup.Key); } } } return(boneHashTranslation); }
public void AddBone(Transform parent, Transform bone) { string bonePath = bone.GetTransformPath(); uint bonePathHash = Animator.StringToHash(bonePath); uint boneHash = Animator.StringToHash(bone.m_GameObject.instance.m_Name); int idx; for (idx = 0; idx < m_TOS.Count; idx++) { var data = m_TOS[idx]; if (data.Key >= bonePathHash) { if (data.Key == bonePathHash) { return; } break; } } m_TOS.Insert(idx, new KeyValuePair <uint, string>(bonePathHash, bonePath)); if (m_Avatar.m_AvatarSkeleton.m_ID.Count > 0) { uint parentHash = BoneHash(parent.GetTransformPath()); for (int i = 0; i < m_Avatar.m_AvatarSkeleton.m_ID.Count; i++) { if (m_Avatar.m_AvatarSkeleton.m_ID[i] == parentHash) { int cIdx = i + BoneIndex(parent, bone); if (cIdx > m_Avatar.m_AvatarSkeleton.m_Node.Count) { m_TOS.RemoveAt(idx); Report.ReportLog("Error in Avatar - cant add bone " + bone.m_GameObject.instance.m_Name); return; } m_Avatar.m_AvatarSkeleton.m_Node.Insert(cIdx, new Node(i, -1)); m_Avatar.m_AvatarSkeleton.m_ID.Insert(cIdx, bonePathHash); m_Avatar.m_SkeletonNameIDArray.Insert(cIdx, boneHash); xform boneXform = file.VersionNumber < AssetCabinet.VERSION_5_4_1 ? new xform(new Vector4(bone.m_LocalPosition, 0), bone.m_LocalRotation, new Vector4(bone.m_LocalScale, 1)) : new xform(bone.m_LocalPosition, bone.m_LocalRotation, bone.m_LocalScale); m_Avatar.m_AvatarSkeletonPose.m_X.Insert(cIdx, boneXform); m_Avatar.m_DefaultPose.m_X.Insert(cIdx, boneXform); for (int j = cIdx + 1; j < m_Avatar.m_AvatarSkeleton.m_ID.Count; j++) { if (m_Avatar.m_AvatarSkeleton.m_Node[j].m_ParentId >= cIdx) { m_Avatar.m_AvatarSkeleton.m_Node[j].m_ParentId++; } } break; } } if (m_TOS.Count != m_Avatar.m_AvatarSkeleton.m_Node.Count) { m_TOS.RemoveAt(idx); Report.ReportLog("Warning! Parent Transform " + parent.m_GameObject.instance.m_Name + " not found in Avatar member m_ID"); } } }