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.
 
 
 

36 lines
1.4 KiB

  1. using System;
  2. using UnityEngine;
  3. namespace CIG3.ExtensionMethods
  4. {
  5. public static class Extensions
  6. {
  7. public static void AttachChildTransform(this GameObject parent, GameObject child, SpriteAlignment alignment = SpriteAlignment.Custom)
  8. {
  9. child.transform.parent = parent.transform;
  10. Vector3 localPosition = new Vector3(0f, 0f, -0.0001f);
  11. SpriteRenderer component = parent.GetComponent<SpriteRenderer>();
  12. if (component != null)
  13. {
  14. if (alignment == SpriteAlignment.BottomRight || alignment == SpriteAlignment.RightCenter || alignment == SpriteAlignment.TopRight)
  15. {
  16. localPosition.x = 0.5f * (float)component.sprite.texture.width;
  17. }
  18. if (alignment == SpriteAlignment.BottomLeft || alignment == SpriteAlignment.LeftCenter || alignment == SpriteAlignment.TopLeft)
  19. {
  20. localPosition.x = -0.5f * (float)component.sprite.texture.width;
  21. }
  22. if (alignment == SpriteAlignment.TopRight || alignment == SpriteAlignment.TopCenter || alignment == SpriteAlignment.TopLeft)
  23. {
  24. localPosition.y = (float)component.sprite.texture.height;
  25. }
  26. if (alignment == SpriteAlignment.LeftCenter || alignment == SpriteAlignment.Center || alignment == SpriteAlignment.RightCenter)
  27. {
  28. localPosition.y = 0.5f * (float)component.sprite.texture.height;
  29. }
  30. }
  31. child.transform.localPosition = localPosition;
  32. }
  33. }
  34. }