| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | | using static UnityEngine.UI.GridLayoutGroup; |
| | 6 | |
|
| | 7 | | public interface IGridContainerComponentView |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Fill the model and updates the grid with this data. |
| | 11 | | /// </summary> |
| | 12 | | /// <param name="model">Data to configure the grid.</param> |
| | 13 | | void Configure(GridContainerComponentModel model); |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Set the type of constraint of the grid. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="newConstraint">Type of constraint.</param> |
| | 19 | | void SetConstraint(Constraint newConstraint); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Set the number of columns/rows of the grid depending on the type of constraint set. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="newConstraintCount">Number of columns or rows.</param> |
| | 25 | | void SetConstraintCount(int newConstraintCount); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Set the item size adaptation to the container. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="adaptItemSizeToContainer">True for activate the size adaptation.</param> |
| | 31 | | void SetItemSizeToContainerAdaptation(bool adaptItemSizeToContainer); |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Set the size of each child item. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="newItemSize">Size of each child.</param> |
| | 37 | | void SetItemSize(Vector2 newItemSize); |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Set the space between child items. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="newSpace">Space between children.</param> |
| | 43 | | void SetSpaceBetweenItems(Vector2 newSpace); |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Set the minimum width for the items when constraint is set to flexible. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="minWidthForFlexibleItems">Min item width.</param> |
| | 49 | | void SetMinWidthForFlexibleItems(float minWidthForFlexibleItems); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Set the items of the grid. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="items">List of UI components.</param> |
| | 55 | | /// <param name="instantiateNewCopyOfItems">Indicates if the items provided will be instantiated as a new copy or no |
| | 56 | | void SetItems(List<BaseComponentView> items, bool instantiateNewCopyOfItems = true); |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Get an item of the grid. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="index">Index of the list of items.</param> |
| | 62 | | /// <returns>A specific UI component.</returns> |
| | 63 | | BaseComponentView GetItem(int index); |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Get all the items of the grid. |
| | 67 | | /// </summary> |
| | 68 | | /// <returns>The list of items.</returns> |
| | 69 | | List<BaseComponentView> GetAllItems(); |
| | 70 | | } |
| | 71 | |
|
| | 72 | | public class GridContainerComponentView : BaseComponentView, IGridContainerComponentView |
| | 73 | | { |
| | 74 | | [Header("Prefab References")] |
| | 75 | | [SerializeField] internal GridLayoutGroup gridLayoutGroup; |
| | 76 | | [SerializeField] internal RectTransform externalParentToAdaptSize; |
| | 77 | |
|
| | 78 | | [Header("Configuration")] |
| | 79 | | [SerializeField] internal GridContainerComponentModel model; |
| | 80 | |
|
| 86 | 81 | | internal List<BaseComponentView> instantiatedItems = new List<BaseComponentView>(); |
| | 82 | | internal bool destroyOnlyUnnecesaryItems = false; |
| | 83 | |
|
| 8 | 84 | | public override void PostInitialization() { Configure(model); } |
| | 85 | |
|
| | 86 | | public void Configure(GridContainerComponentModel model) |
| | 87 | | { |
| 5 | 88 | | this.model = model; |
| 5 | 89 | | RefreshControl(); |
| 5 | 90 | | } |
| | 91 | |
|
| | 92 | | public override void RefreshControl() |
| | 93 | | { |
| 6 | 94 | | if (model == null) |
| 0 | 95 | | return; |
| | 96 | |
|
| 6 | 97 | | SetConstraint(model.constraint); |
| 6 | 98 | | SetItems(model.items); |
| 6 | 99 | | SetConstraintCount(model.constranitCount); |
| 6 | 100 | | SetSpaceBetweenItems(model.spaceBetweenItems); |
| 6 | 101 | | } |
| | 102 | |
|
| | 103 | | public override void PostScreenSizeChanged() |
| | 104 | | { |
| 1 | 105 | | base.PostScreenSizeChanged(); |
| | 106 | |
|
| 1 | 107 | | ResizeGridContainer(); |
| 1 | 108 | | SetItemSize(model.itemSize); |
| 1 | 109 | | } |
| | 110 | |
|
| | 111 | | public override void Dispose() |
| | 112 | | { |
| 23 | 113 | | base.Dispose(); |
| | 114 | |
|
| 23 | 115 | | DestroyInstantiatedItems(true); |
| 23 | 116 | | } |
| | 117 | |
|
| | 118 | | public void SetConstraint(Constraint newConstraint) |
| | 119 | | { |
| 12 | 120 | | model.constraint = newConstraint; |
| | 121 | |
|
| 12 | 122 | | if (gridLayoutGroup == null) |
| 0 | 123 | | return; |
| | 124 | |
|
| 12 | 125 | | gridLayoutGroup.constraint = newConstraint; |
| 12 | 126 | | } |
| | 127 | |
|
| | 128 | | public void SetConstraintCount(int newConstraintCount) |
| | 129 | | { |
| 7 | 130 | | model.constranitCount = newConstraintCount; |
| | 131 | |
|
| 7 | 132 | | if (gridLayoutGroup == null) |
| 0 | 133 | | return; |
| | 134 | |
|
| 7 | 135 | | gridLayoutGroup.constraintCount = newConstraintCount; |
| 7 | 136 | | } |
| | 137 | |
|
| | 138 | | public void SetItemSizeToContainerAdaptation(bool adaptItemSizeToContainer) |
| | 139 | | { |
| 0 | 140 | | model.adaptItemSizeToContainer = adaptItemSizeToContainer; |
| 0 | 141 | | SetItemSize(model.itemSize); |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | public void SetItemSize(Vector2 newItemSize) |
| | 145 | | { |
| 13 | 146 | | Vector2 newSizeToApply = newItemSize; |
| 13 | 147 | | if (model.adaptItemSizeToContainer) |
| | 148 | | { |
| 4 | 149 | | if (model.constraint == Constraint.FixedColumnCount) |
| | 150 | | { |
| 1 | 151 | | float width = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.width : ((RectTransform |
| 1 | 152 | | float extraSpaceToRemove = (model.spaceBetweenItems.x * (model.constranitCount - 1)) / model.constranitC |
| 1 | 153 | | newSizeToApply = new Vector2( |
| | 154 | | (width / model.constranitCount) - extraSpaceToRemove, |
| | 155 | | model.itemSize.y); |
| 1 | 156 | | } |
| 3 | 157 | | else if (model.constraint == Constraint.FixedRowCount) |
| | 158 | | { |
| 0 | 159 | | float height = ((RectTransform)transform).rect.height; |
| 0 | 160 | | float extraSpaceToRemove = (model.spaceBetweenItems.y / (model.constranitCount / 2f)); |
| 0 | 161 | | newSizeToApply = new Vector2( |
| | 162 | | model.itemSize.x, |
| | 163 | | (height / model.constranitCount) - extraSpaceToRemove); |
| 0 | 164 | | } |
| 3 | 165 | | else if (model.constraint == Constraint.Flexible) |
| | 166 | | { |
| 3 | 167 | | float width = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.width : ((RectTransform |
| 3 | 168 | | int numberOfPossibleItems = (int)(width / model.minWidthForFlexibleItems); |
| | 169 | |
|
| 3 | 170 | | SetConstraint(Constraint.FixedColumnCount); |
| | 171 | |
|
| 3 | 172 | | if (numberOfPossibleItems > 0) |
| | 173 | | { |
| 0 | 174 | | for (int numColumnsToTry = 1; numColumnsToTry <= numberOfPossibleItems; numColumnsToTry++) |
| | 175 | | { |
| 0 | 176 | | SetConstraintCount(numColumnsToTry); |
| 0 | 177 | | SetItemSize(model.itemSize); |
| | 178 | |
|
| 0 | 179 | | if (model.itemSize.x < model.minWidthForFlexibleItems) |
| | 180 | | { |
| 0 | 181 | | SetConstraintCount(numColumnsToTry - 1); |
| 0 | 182 | | SetItemSize(model.itemSize); |
| 0 | 183 | | break; |
| | 184 | | } |
| | 185 | |
|
| 0 | 186 | | newSizeToApply = model.itemSize; |
| | 187 | | } |
| 0 | 188 | | } |
| | 189 | | else |
| | 190 | | { |
| 3 | 191 | | newSizeToApply = new Vector2(model.minWidthForFlexibleItems, newSizeToApply.y); |
| | 192 | | } |
| | 193 | |
|
| 3 | 194 | | SetConstraint(Constraint.Flexible); |
| | 195 | | } |
| | 196 | | } |
| | 197 | |
|
| 13 | 198 | | model.itemSize = newSizeToApply; |
| | 199 | |
|
| 13 | 200 | | if (gridLayoutGroup == null) |
| 0 | 201 | | return; |
| | 202 | |
|
| 13 | 203 | | gridLayoutGroup.cellSize = newSizeToApply; |
| | 204 | |
|
| 13 | 205 | | ResizeGridContainer(); |
| 13 | 206 | | } |
| | 207 | |
|
| | 208 | | public void SetSpaceBetweenItems(Vector2 newSpace) |
| | 209 | | { |
| 7 | 210 | | model.spaceBetweenItems = newSpace; |
| | 211 | |
|
| 7 | 212 | | if (gridLayoutGroup == null) |
| 0 | 213 | | return; |
| | 214 | |
|
| 7 | 215 | | gridLayoutGroup.spacing = newSpace; |
| 7 | 216 | | } |
| | 217 | |
|
| | 218 | | public void SetMinWidthForFlexibleItems(float minWidthForFlexibleItems) |
| | 219 | | { |
| 0 | 220 | | model.minWidthForFlexibleItems = minWidthForFlexibleItems; |
| 0 | 221 | | SetItemSize(model.itemSize); |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | public void SetItems(List<BaseComponentView> items, bool instantiateNewCopyOfItems = true) |
| | 225 | | { |
| 10 | 226 | | model.items = items; |
| | 227 | |
|
| 10 | 228 | | DestroyInstantiatedItems(!destroyOnlyUnnecesaryItems); |
| | 229 | |
|
| 62 | 230 | | for (int i = 0; i < items.Count; i++) |
| | 231 | | { |
| 21 | 232 | | CreateItem(items[i], $"Item{i}", instantiateNewCopyOfItems && !destroyOnlyUnnecesaryItems); |
| | 233 | | } |
| | 234 | |
|
| 10 | 235 | | ResizeGridContainer(); |
| | 236 | |
|
| 10 | 237 | | if (!instantiateNewCopyOfItems) |
| 0 | 238 | | destroyOnlyUnnecesaryItems = true; |
| | 239 | |
|
| 10 | 240 | | SetItemSize(model.itemSize); |
| 10 | 241 | | } |
| | 242 | |
|
| | 243 | | public BaseComponentView GetItem(int index) |
| | 244 | | { |
| 2 | 245 | | if (index >= instantiatedItems.Count) |
| 0 | 246 | | return null; |
| | 247 | |
|
| 2 | 248 | | return instantiatedItems[index]; |
| | 249 | | } |
| | 250 | |
|
| 0 | 251 | | public List<BaseComponentView> GetAllItems() { return instantiatedItems; } |
| | 252 | |
|
| | 253 | | internal void CreateItem(BaseComponentView newItem, string name, bool instantiateNewCopyOfItem = true) |
| | 254 | | { |
| 21 | 255 | | if (Application.isPlaying) |
| | 256 | | { |
| 21 | 257 | | InstantiateItem(newItem, name, instantiateNewCopyOfItem); |
| 21 | 258 | | } |
| | 259 | | else |
| | 260 | | { |
| 0 | 261 | | if (isActiveAndEnabled) |
| 0 | 262 | | StartCoroutine(InstantiateItemOnEditor(newItem, name)); |
| | 263 | | } |
| 0 | 264 | | } |
| | 265 | |
|
| | 266 | | internal void InstantiateItem(BaseComponentView newItem, string name, bool instantiateNewCopyOfItem = true) |
| | 267 | | { |
| 21 | 268 | | if (newItem == null) |
| 0 | 269 | | return; |
| | 270 | |
|
| | 271 | | BaseComponentView newGO; |
| 21 | 272 | | if (instantiateNewCopyOfItem) |
| | 273 | | { |
| 21 | 274 | | newGO = Instantiate(newItem, transform); |
| 21 | 275 | | } |
| | 276 | | else |
| | 277 | | { |
| 0 | 278 | | newGO = newItem; |
| 0 | 279 | | newGO.transform.SetParent(transform); |
| 0 | 280 | | newGO.transform.localPosition = Vector3.zero; |
| 0 | 281 | | newGO.transform.localScale = Vector3.one; |
| | 282 | | } |
| | 283 | |
|
| 21 | 284 | | newGO.name = name; |
| 21 | 285 | | instantiatedItems.Add(newGO); |
| 21 | 286 | | } |
| | 287 | |
|
| | 288 | | internal IEnumerator InstantiateItemOnEditor(BaseComponentView newItem, string name) |
| | 289 | | { |
| 0 | 290 | | yield return null; |
| 0 | 291 | | InstantiateItem(newItem, name); |
| 0 | 292 | | } |
| | 293 | |
|
| | 294 | | internal void DestroyInstantiatedItems(bool forzeToDestroyAll) |
| | 295 | | { |
| 34 | 296 | | if (forzeToDestroyAll) |
| | 297 | | { |
| 158 | 298 | | foreach (Transform child in transform) |
| | 299 | | { |
| 45 | 300 | | if (Application.isPlaying) |
| | 301 | | { |
| 45 | 302 | | Destroy(child.gameObject); |
| 45 | 303 | | } |
| | 304 | | else |
| | 305 | | { |
| 0 | 306 | | if (isActiveAndEnabled) |
| 0 | 307 | | StartCoroutine(DestroyGameObjectOnEditor(child.gameObject)); |
| | 308 | | } |
| | 309 | | } |
| | 310 | | } |
| | 311 | | else |
| | 312 | | { |
| 0 | 313 | | foreach (BaseComponentView child in instantiatedItems) |
| | 314 | | { |
| 0 | 315 | | if (!model.items.Contains(child)) |
| | 316 | | { |
| 0 | 317 | | Destroy(child.gameObject); |
| | 318 | | } |
| | 319 | | } |
| | 320 | | } |
| | 321 | |
|
| 34 | 322 | | instantiatedItems.Clear(); |
| 34 | 323 | | } |
| | 324 | |
|
| | 325 | | internal IEnumerator DestroyGameObjectOnEditor(GameObject go) |
| | 326 | | { |
| 0 | 327 | | yield return null; |
| 0 | 328 | | DestroyImmediate(go); |
| 0 | 329 | | } |
| | 330 | |
|
| | 331 | | internal void ResizeGridContainer() |
| | 332 | | { |
| 24 | 333 | | if (model.items.Count == 0) |
| | 334 | | { |
| 10 | 335 | | ((RectTransform)transform).sizeDelta = Vector2.zero; |
| 10 | 336 | | return; |
| | 337 | | } |
| | 338 | |
|
| 14 | 339 | | if (model.constraint == Constraint.FixedColumnCount) |
| | 340 | | { |
| 8 | 341 | | int numRows = (int)Mathf.Ceil((float)model.items.Count / model.constranitCount); |
| 8 | 342 | | ((RectTransform)transform).sizeDelta = new Vector2( |
| | 343 | | model.adaptItemSizeToContainer ? ((RectTransform)transform).sizeDelta.x : (model.constranitCount * model |
| | 344 | | (numRows * model.itemSize.y) + (model.spaceBetweenItems.y * (numRows - 1))); |
| 8 | 345 | | } |
| 6 | 346 | | else if (model.constraint == Constraint.FixedRowCount) |
| | 347 | | { |
| 0 | 348 | | int numCols = (int)Mathf.Ceil((float)model.items.Count / model.constranitCount); |
| 0 | 349 | | ((RectTransform)transform).sizeDelta = new Vector2( |
| | 350 | | (numCols * model.itemSize.x) + (model.spaceBetweenItems.x * (numCols - 1)), |
| | 351 | | model.adaptItemSizeToContainer ? ((RectTransform)transform).sizeDelta.y : (model.constranitCount * model |
| | 352 | | } |
| 6 | 353 | | } |
| | 354 | | } |