SRFTransformExtensions.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace SRF
  6. {
  7. public static class SRFTransformExtensions
  8. {
  9. public static IEnumerable<Transform> GetChildren(this Transform t)
  10. {
  11. for (int i = 0; i < t.childCount; i++)
  12. {
  13. yield return t.GetChild(i);
  14. }
  15. yield break;
  16. }
  17. public static void ResetLocal(this Transform t)
  18. {
  19. t.localPosition = Vector3.zero;
  20. t.localRotation = Quaternion.identity;
  21. t.localScale = Vector3.one;
  22. }
  23. public static GameObject CreateChild(this Transform t, string name)
  24. {
  25. GameObject gameObject = new GameObject(name);
  26. gameObject.transform.parent = t;
  27. gameObject.transform.ResetLocal();
  28. gameObject.gameObject.layer = t.gameObject.layer;
  29. return gameObject;
  30. }
  31. public static void SetParentMaintainLocals(this Transform t, Transform parent)
  32. {
  33. t.SetParent(parent, false);
  34. }
  35. public static void SetLocals(this Transform t, Transform from)
  36. {
  37. t.localPosition = from.localPosition;
  38. t.localRotation = from.localRotation;
  39. t.localScale = from.localScale;
  40. }
  41. public static void Match(this Transform t, Transform from)
  42. {
  43. t.position = from.position;
  44. t.rotation = from.rotation;
  45. }
  46. public static void DestroyChildren(this Transform t)
  47. {
  48. IEnumerator enumerator = t.GetEnumerator();
  49. try
  50. {
  51. while (enumerator.MoveNext())
  52. {
  53. object obj = enumerator.Current;
  54. UnityEngine.Object.Destroy(((Transform)obj).gameObject);
  55. }
  56. }
  57. finally
  58. {
  59. IDisposable disposable;
  60. if ((disposable = (enumerator as IDisposable)) != null)
  61. {
  62. disposable.Dispose();
  63. }
  64. }
  65. }
  66. }
  67. }