Tanoda
ByNameHumanoidAvatarMapper.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using TriLibCore.General;
3using TriLibCore.Extensions;
4using UnityEngine;
5using HumanLimit = TriLibCore.General.HumanLimit;
6
7namespace TriLibCore.Mappers
8{
10 [CreateAssetMenu(menuName = "TriLib/Mappers/Humanoid/By Name Humanoid Avatar Mapper", fileName = "ByNameHumanoidAvatarMapper")]
11 public class ByNameHumanoidAvatarMapper : HumanoidAvatarMapper
12 {
16 [Header("Left = Loaded GameObjects Names, Right = Names in BonesMapping.BoneNames")]
17 public StringComparisonMode stringComparisonMode;
18
22 public bool CaseInsensitive = true;
23
27 public List<BoneMapping> BonesMapping;
28
30 public override Dictionary<BoneMapping, Transform> Map(AssetLoaderContext assetLoaderContext)
31 {
32 var mapping = new Dictionary<BoneMapping, Transform>();
33 for (var i = 0; i < BonesMapping.Count; i++)
34 {
35 var boneMapping = BonesMapping[i];
36 if (boneMapping.BoneNames != null)
37 {
38 var found = false;
39 for (var j = 0; j < boneMapping.BoneNames.Length; j++)
40 {
41 var boneName = boneMapping.BoneNames[j];
42 var transform = assetLoaderContext.RootGameObject.transform.FindDeepChild(boneName, stringComparisonMode, CaseInsensitive);
43 if (transform == null)
44 {
45 continue;
46 }
47
48 var model = assetLoaderContext.Models[transform.gameObject];
49 if (!model.IsBone)
50 {
51 continue;
52 }
53
54 mapping.Add(boneMapping, transform);
55 found = true;
56 break;
57 }
58
59 if (!found && !IsBoneOptional(boneMapping.HumanBone))
60 {
61 if (assetLoaderContext.Options.ShowLoadingWarnings)
62 {
63 Debug.LogWarning($"Could not find bone '{boneMapping.HumanBone}'");
64 }
65
66 mapping.Clear();
67 return mapping;
68 }
69 }
70 }
71
72 return mapping;
73 }
74
75 private static bool IsBoneOptional(HumanBodyBones humanBodyBones)
76 {
77 return !HumanTrait.RequiredBone((int)humanBodyBones);
78 }
79
84 public void AddMapping(HumanBodyBones humanBodyBones, HumanLimit humanLimit, params string[] boneNames)
85 {
86 if (BonesMapping == null)
87 {
88 BonesMapping = new List<BoneMapping>();
89 }
90 BonesMapping.Add(new BoneMapping(humanBodyBones, humanLimit, boneNames));
91 }
92 }
93}
TriLibCore.General.HumanLimit HumanLimit
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Represents a Mapper that finds humanoid Avatar bones by name-matching.
bool CaseInsensitive
Is the string comparison case insensitive?
List< BoneMapping > BonesMapping
The human bone to Unity bone relationship list.
StringComparisonMode stringComparisonMode
String comparison mode to use on the mapping.
override Dictionary< BoneMapping, Transform > Map(AssetLoaderContext assetLoaderContext)
void AddMapping(HumanBodyBones humanBodyBones, HumanLimit humanLimit, params string[] boneNames)
Adds a new mapping item, containing the humanoid bone type, the limits, and the list of names to look...