SCBaseLayoutGroup.cs 3.3 KB

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