SCBaseLayoutGroup.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity
  5. {
  6. public abstract class SCBaseLayoutGroup : MonoBehaviour
  7. {
  8. public Action<SCBaseLayoutGroup> OnUpdatedCollection { get; set; }
  9. [HideInInspector]
  10. [SerializeField]
  11. private List<GroupObj> objList = new List<GroupObj>();
  12. protected List<GroupObj> ObjList
  13. {
  14. get { return objList; }
  15. }
  16. [SerializeField]
  17. private bool isIgnoreInactiveObj = true;//Ignore the deactivation object
  18. public bool IsIgnoreInactiveObj
  19. {
  20. get { return isIgnoreInactiveObj; }
  21. set { isIgnoreInactiveObj = value; }
  22. }
  23. [SerializeField]
  24. private ListSortType groupSortType = ListSortType.Childindex;
  25. public ListSortType GroupSortType
  26. {
  27. get { return groupSortType; }
  28. set { groupSortType = value; }
  29. }
  30. private Transform child;
  31. protected void InitGroup()
  32. {
  33. var tempNodes = new List<GroupObj>();
  34. for (int i = 0; i < ObjList.Count; i++)
  35. {
  36. if (ObjList[i].Transform == null || (IsIgnoreInactiveObj && !ObjList[i].Transform.gameObject.activeSelf) || !(ObjList[i].Transform.parent.gameObject == gameObject) || ObjList[i].Transform.parent == null)
  37. {
  38. tempNodes.Add(ObjList[i]);
  39. }
  40. }
  41. for (int i = 0; i < tempNodes.Count; i++)
  42. {
  43. ObjList.Remove(tempNodes[i]);
  44. }
  45. tempNodes.Clear();
  46. }
  47. protected void SortGroup()
  48. {
  49. switch (GroupSortType)
  50. {
  51. case ListSortType.Childindex:
  52. ObjList.Sort((c1, c2) => (c1.Transform.GetSiblingIndex().CompareTo(c2.Transform.GetSiblingIndex())));
  53. break;
  54. case ListSortType.ChildIndexReverse:
  55. ObjList.Sort((c1, c2) => (c1.Transform.GetSiblingIndex().CompareTo(c2.Transform.GetSiblingIndex())));
  56. ObjList.Reverse();
  57. break;
  58. case ListSortType.ChildAlphabet:
  59. ObjList.Sort((c1, c2) => (string.CompareOrdinal(c1.Name, c2.Name)));
  60. break;
  61. case ListSortType.ChildAlphabetReverse:
  62. ObjList.Sort((c1, c2) => (c1.Transform.GetSiblingIndex().CompareTo(c2.Transform.GetSiblingIndex())));
  63. ObjList.Reverse();
  64. break;
  65. }
  66. }
  67. protected bool IsContainsObj(Transform obj)
  68. {
  69. if (obj == null)
  70. {
  71. return false;
  72. }
  73. for (int i = 0; i < ObjList.Count; i++)
  74. {
  75. if (ObjList[i].Transform == obj)
  76. {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. protected abstract void LayoutChildren();
  83. public virtual void RefreshInfo()
  84. {
  85. InitGroup();
  86. for (int i = 0; i < transform.childCount; i++)
  87. {
  88. child = transform.GetChild(i);
  89. #if UNITY_EDITOR
  90. UnityEditor.Undo.RecordObject(child, "ObjectCollection modify transform");
  91. #endif
  92. if (!IsContainsObj(child) && (child.gameObject.activeSelf || !IsIgnoreInactiveObj))
  93. {
  94. ObjList.Add(new GroupObj { Name = child.name, Transform = child });
  95. }
  96. }
  97. SortGroup();
  98. LayoutChildren();
  99. if (OnUpdatedCollection != null)
  100. {
  101. OnUpdatedCollection.Invoke(this);
  102. }
  103. }
  104. }
  105. }