问题遇到的现象和发生背景
动态导入模型后 动态创建Avatar骨骼映射
根据某位博主提供的思路动态创建Avatar时出现报错 hips not find
遇到的现象和发生背景,请写出第一个错误信息
使用trilib预设的动态导入模型场景导入了一个有骨骼的模型
然后出现了报错
模型动态加载是没有错的
场景是trilib插件预设的,应该是没有问题的
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
//using System.Collections;
//using System.Collections.Generic;
//using UnityEngine;
public class Skeleton : MonoBehaviour
{
public static SkeletonBone[] CreateSkeleton(GameObject avatarRoot)
{
List<SkeletonBone> skeleton = new List<SkeletonBone>();
Transform[] avatarTransforms = avatarRoot.GetComponentsInChildren<Transform>();
foreach (Transform avatarTransform in avatarTransforms)
{
SkeletonBone bone = new SkeletonBone()
{
name = avatarTransform.name,
position = avatarTransform.localPosition,
rotation = avatarTransform.localRotation,
scale = avatarTransform.localScale
};
skeleton.Add(bone);
}
return skeleton.ToArray();
}
}
//这是动态创建Avatar的skeleton脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dictionary : MonoBehaviour
{
public static GameObject A;
public static Dictionary<string, string> HumanSkeletonMap = new Dictionary<string, string>()
{ {"00", "Hips" },
{"03", "Spine" },
{"06", "Chest"},
{"09", "UpperChest" },
{"13" ,"Shoulder"},
{"16" ,"UpperArm"},
{"18" ,"LowerArm"},
{"20" ,"Hand"},
{"14" ,"Shoulder"},
{"17" ,"UpperArm"},
{"19" ,"LowerArm"},
{"21" ,"Hand"},
{"01" ,"UpperLeg"},
{"04" ,"LowerLeg"},
{"07" ,"Foot"},
{"10" ,"Toes"},
{"02" ,"UpperLeg"},
{"05" ,"LowerLeg"},
{"08" ,"Foot"},
{"11" ,"Toes"},
{"12", "Neck" },
{"15", "Head" },
//{ "eye_EyeJoint_L", "LeftEye" },
//{ "eye_EyeJoint_R", "RightEye" },
//{ "mouth_JawJoint_M", "Jaw" },
//这里的映射关系是根据手动导入模型后 rig -create from this model 后形成的骨骼映射关系填写补充的
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/078618198276150.png "#left")
如图所示
};
public static HumanBone[] CreateHuman(GameObject avatarRoot)
{
List<HumanBone> human = new List<HumanBone>();
Transform[] avatarTransforms = avatarRoot.GetComponentsInChildren<Transform>();
foreach (Transform avatarTransform in avatarTransforms)
{
if (HumanSkeletonMap.TryGetValue(avatarTransform.name, out string humanName))
{
HumanBone bone = new HumanBone
{
boneName = avatarTransform.name,
humanName = humanName,
limit = new HumanLimit()
};
bone.limit.useDefaultValues = true;
//bone.Equals(human);
human.Add(bone);
}
}
return human.ToArray();
}
HumanDescription humanDescription = new HumanDescription()
{
armStretch = 0.05f,
feetSpacing = 0.0f,
hasTranslationDoF = false,
legStretch = 0.05f,
lowerArmTwist = 0.5f,
lowerLegTwist = 0.5f,
upperArmTwist = 0.5f,
upperLegTwist = 0.5f,
skeleton = Skeleton.CreateSkeleton(A),
human = CreateHuman(A),
};
public void createAvatarTest()
{
A = GameObject.Find("output");
//因为导入的模型名称为output,这里就简单用此获得模型
A.AddComponent<Animator>();
Avatar avatar = AvatarBuilder.BuildHumanAvatar(A, humanDescription);
Animator animator =A.GetComponent<Animator>();
animator.runtimeAnimatorController =
Resources.Load<RuntimeAnimatorController>("jump");
animator.avatar = avatar;
}//此方法用于测设动态创建一个avatar并为他添加animator组件,并加载Resources文件夹下的jump controller使得能够动态导入一个有骨骼的模型,再点击button调用这个方法后,模型能够jump起来
}
运行结果及详细报错内容
然后出现报错
hips not find
我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”
//
```c#
```如果觉得便宜了,可以再商量