No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IKUtility.cs 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine.Scripting.APIUpdating;
  2. namespace UnityEngine.U2D.IK
  3. {
  4. /// <summary>
  5. /// General utilities for 2D IK.
  6. /// </summary>
  7. [MovedFrom("UnityEngine.Experimental.U2D.IK")]
  8. public class IKUtility
  9. {
  10. /// <summary>
  11. /// Check if a transform is a descendent of another transform.
  12. /// </summary>
  13. /// <param name="transform">Transforms to check.</param>
  14. /// <param name="ancestor">Transform's ancestor.</param>
  15. /// <returns>Returns true if the transform is a descendent. False otherwise.</returns>
  16. public static bool IsDescendentOf(Transform transform, Transform ancestor)
  17. {
  18. Debug.Assert(transform != null, "Transform is null");
  19. var currentParent = transform.parent;
  20. while (currentParent)
  21. {
  22. if (currentParent == ancestor)
  23. return true;
  24. currentParent = currentParent.parent;
  25. }
  26. return false;
  27. }
  28. /// <summary>
  29. /// Gets the depth of the transform's hierarchy.
  30. /// </summary>
  31. /// <param name="transform">Transform to check.</param>
  32. /// <returns>Integer value for hierarchy depth.</returns>
  33. public static int GetAncestorCount(Transform transform)
  34. {
  35. Debug.Assert(transform != null, "Transform is null");
  36. var ancestorCount = 0;
  37. while (transform.parent)
  38. {
  39. ++ancestorCount;
  40. transform = transform.parent;
  41. }
  42. return ancestorCount;
  43. }
  44. /// <summary>
  45. /// Gets the maximum chain count for a IKChain2D.
  46. /// </summary>
  47. /// <param name="chain">IKChain2D to query.</param>
  48. /// <returns>Integer value for the maximum chain count.</returns>
  49. public static int GetMaxChainCount(IKChain2D chain)
  50. {
  51. var maxChainCount = 0;
  52. if (chain.effector)
  53. maxChainCount = GetAncestorCount(chain.effector) + 1;
  54. return maxChainCount;
  55. }
  56. }
  57. }