MagicaAvatarParts.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace MagicaCloth
  7. {
  8. /// <summary>
  9. /// アバターパーツコンポーネント
  10. /// </summary>
  11. [HelpURL("https://magicasoft.jp/avatar-parts/")]
  12. [AddComponentMenu("MagicaCloth/MagicaAvatarParts")]
  13. public class MagicaAvatarParts : BaseComponent, IDataVerify
  14. {
  15. //=============================================================================================
  16. /// <summary>
  17. /// 親アバター
  18. /// </summary>
  19. private MagicaAvatar parentAvatar = null;
  20. /// <summary>
  21. /// このアバターパーツが保持するボーン辞書
  22. /// </summary>
  23. private Dictionary<string, Transform> boneDict = new Dictionary<string, Transform>();
  24. /// <summary>
  25. /// このアバターパーツが保持するMagicaコンポーネントのリスト
  26. /// </summary>
  27. private List<CoreComponent> magicaComponentList = null;
  28. //=========================================================================================
  29. public override ComponentType GetComponentType()
  30. {
  31. return ComponentType.AvatarParts;
  32. }
  33. //=============================================================================================
  34. public MagicaAvatar ParentAvatar
  35. {
  36. get
  37. {
  38. return parentAvatar;
  39. }
  40. set
  41. {
  42. parentAvatar = value;
  43. }
  44. }
  45. public bool HasParent
  46. {
  47. get
  48. {
  49. return parentAvatar != null;
  50. }
  51. }
  52. public int PartsId
  53. {
  54. get
  55. {
  56. return GetInstanceID();
  57. }
  58. }
  59. //=============================================================================================
  60. private void OnDestroy()
  61. {
  62. Dispose();
  63. }
  64. //=============================================================================================
  65. /// <summary>
  66. /// 破棄
  67. /// </summary>
  68. public void Dispose()
  69. {
  70. // 親から削除する
  71. if (parentAvatar != null)
  72. {
  73. parentAvatar.DetachAvatarParts(gameObject);
  74. parentAvatar = null;
  75. }
  76. }
  77. //=============================================================================================
  78. /// <summary>
  79. /// ゲームオブジェクト名が重複するトランスフォームのリストを返す
  80. /// </summary>
  81. /// <returns></returns>
  82. public List<Transform> CheckOverlappingTransform()
  83. {
  84. var boneHash = new HashSet<string>();
  85. var overlapList = new List<Transform>();
  86. var tlist = GetComponentsInChildren<Transform>();
  87. var root = transform;
  88. foreach (var t in tlist)
  89. {
  90. if (t == root)
  91. continue;
  92. if (boneHash.Contains(t.name))
  93. {
  94. overlapList.Add(t);
  95. }
  96. else
  97. {
  98. boneHash.Add(t.name);
  99. }
  100. }
  101. return overlapList;
  102. }
  103. /// <summary>
  104. /// すべてのボーンを辞書に登録して返す
  105. /// この時にボーン名に重複があると着せ替えのときに問題を起こす可能性がある
  106. /// </summary>
  107. public Dictionary<string, Transform> GetBoneDict()
  108. {
  109. if (boneDict.Count > 0)
  110. return boneDict;
  111. boneDict.Clear();
  112. var tlist = GetComponentsInChildren<Transform>();
  113. foreach (var t in tlist)
  114. {
  115. if (boneDict.ContainsKey(t.name))
  116. {
  117. // Duplication name!
  118. Debug.LogWarning(string.Format("{0} [{1}]", Define.GetErrorMessage(Define.Error.OverlappingTransform), t.name));
  119. }
  120. else
  121. {
  122. boneDict.Add(t.name, t);
  123. }
  124. }
  125. //Debug.Log("boneDict:" + boneDict.Count);
  126. return boneDict;
  127. }
  128. /// <summary>
  129. /// このアバターパーツが保持するMagicaコンポーネントのリストを返す
  130. /// </summary>
  131. /// <returns></returns>
  132. public List<CoreComponent> GetMagicaComponentList()
  133. {
  134. if (magicaComponentList != null)
  135. return magicaComponentList;
  136. magicaComponentList = new List<CoreComponent>(GetComponentsInChildren<CoreComponent>());
  137. return magicaComponentList;
  138. }
  139. //=============================================================================================
  140. public int GetVersion()
  141. {
  142. return 1;
  143. }
  144. public void CreateVerifyData()
  145. {
  146. throw new System.NotImplementedException();
  147. }
  148. public Define.Error VerifyData()
  149. {
  150. if (Application.isPlaying)
  151. {
  152. // 実行中
  153. return Define.Error.None;
  154. }
  155. else
  156. {
  157. // エディット中
  158. // 重複トランスフォームチェック
  159. var olist = CheckOverlappingTransform();
  160. if (olist.Count > 0)
  161. return Define.Error.OverlappingTransform;
  162. return Define.Error.None;
  163. }
  164. }
  165. public string GetInformation()
  166. {
  167. StaticStringBuilder.Clear();
  168. if (Application.isPlaying)
  169. {
  170. // 実行中
  171. if (ParentAvatar)
  172. {
  173. StaticStringBuilder.Append("Connection parent avatar:");
  174. StaticStringBuilder.AppendLine();
  175. StaticStringBuilder.Append(" [", ParentAvatar.name, "]");
  176. }
  177. else
  178. {
  179. StaticStringBuilder.Append("No connection.");
  180. }
  181. }
  182. else
  183. {
  184. // エディット中
  185. // 重複トランスフォームチェック
  186. var olist = CheckOverlappingTransform();
  187. if (olist.Count > 0)
  188. {
  189. StaticStringBuilder.Append("There are duplicate game object names.");
  190. foreach (var t in olist)
  191. {
  192. StaticStringBuilder.AppendLine();
  193. StaticStringBuilder.Append("* ", t.name);
  194. }
  195. }
  196. else
  197. {
  198. StaticStringBuilder.Append("No problem.");
  199. }
  200. }
  201. return StaticStringBuilder.ToString();
  202. }
  203. }
  204. }