| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | | using static UnityEngine.UI.GridLayoutGroup; |
| | 7 | |
|
| | 8 | | public interface IGridContainerComponentView |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Number of items per row that fit with the current grid configuration. |
| | 12 | | /// </summary> |
| | 13 | | int currentItemsPerRow { get; } |
| | 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 item size and resize grid container with respect to the current model. |
| | 41 | | /// </summary> |
| | 42 | | void SetItemSizeForModel(); |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Set the space between child items. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="newSpace">Space between children.</param> |
| | 48 | | void SetSpaceBetweenItems(Vector2 newSpace); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Set the minimum width for the items when constraint is set to flexible. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="minWidthForFlexibleItems">Min item width.</param> |
| | 54 | | void SetMinWidthForFlexibleItems(float minWidthForFlexibleItems); |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Creates the items of the grid from the prefab. All previously existing items will be removed. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="prefab">Prefab to create items</param> |
| | 60 | | /// <param name="amountOfItems">Amounts of items to be created</param> |
| | 61 | | void SetItems(BaseComponentView prefab, int amountOfItems); |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Set the items of the grid. All previously existing items will be removed. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="items">List of UI components.</param> |
| | 67 | | void SetItems(List<BaseComponentView> items); |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Adds a new item in the grid. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="item">An UI component.</param> |
| | 73 | | void AddItem(BaseComponentView item); |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Adds a new item in the grid and resize the grid. |
| | 77 | | /// </summary> |
| | 78 | | /// <param name="item">An UI component.</param> |
| | 79 | | void AddItemWithResize(BaseComponentView item); |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Remove an item from the grid. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="item">An UI component</param> |
| | 85 | | void RemoveItem(BaseComponentView item); |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Get all the items of the grid. |
| | 89 | | /// </summary> |
| | 90 | | /// <returns>List of items.</returns> |
| | 91 | | List<BaseComponentView> GetItems(); |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Extract all items out of the grid. |
| | 95 | | /// </summary> |
| | 96 | | /// <returns>The list of extracted items.</returns> |
| | 97 | | List<BaseComponentView> ExtractItems(); |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Remove all existing items from the grid. |
| | 101 | | /// </summary> |
| | 102 | | void RemoveItems(); |
| | 103 | | } |
| | 104 | |
|
| | 105 | | public class GridContainerComponentView : BaseComponentView, IGridContainerComponentView, IComponentModelConfig<GridCont |
| | 106 | | { |
| | 107 | | [Header("Prefab References")] |
| | 108 | | [SerializeField] internal GridLayoutGroup gridLayoutGroup; |
| | 109 | | [SerializeField] internal RectTransform externalParentToAdaptSize; |
| | 110 | |
|
| | 111 | | [Header("Configuration")] |
| | 112 | | [SerializeField] internal GridContainerComponentModel model; |
| | 113 | |
|
| 1134 | 114 | | internal List<BaseComponentView> instantiatedItems = new List<BaseComponentView>(); |
| | 115 | |
|
| 0 | 116 | | public int currentItemsPerRow { get; internal set; } |
| | 117 | |
|
| | 118 | | public override void Awake() |
| | 119 | | { |
| 898 | 120 | | base.Awake(); |
| | 121 | |
|
| 898 | 122 | | RegisterCurrentInstantiatedItems(); |
| 898 | 123 | | } |
| | 124 | |
|
| | 125 | | public void Configure(GridContainerComponentModel newModel) |
| | 126 | | { |
| 7 | 127 | | model = newModel; |
| 7 | 128 | | RefreshControl(); |
| 7 | 129 | | } |
| | 130 | |
|
| | 131 | | public override void RefreshControl() |
| | 132 | | { |
| 9 | 133 | | if (model == null) |
| 0 | 134 | | return; |
| | 135 | |
|
| | 136 | | //We only do this if we are from editor, this is done because the editor doesn't call awake method |
| 9 | 137 | | if (!Application.isPlaying) |
| 0 | 138 | | RegisterCurrentInstantiatedItems(); |
| | 139 | |
|
| 9 | 140 | | SetConstraint(model.constraint); |
| 9 | 141 | | SetItemSize(model.itemSize); |
| 9 | 142 | | SetConstraintCount(model.constraintCount); |
| 9 | 143 | | SetSpaceBetweenItems(model.spaceBetweenItems); |
| 9 | 144 | | } |
| | 145 | |
|
| | 146 | | public override void OnScreenSizeChanged() |
| | 147 | | { |
| 55 | 148 | | base.OnScreenSizeChanged(); |
| | 149 | |
|
| 55 | 150 | | SetItemSize(model.itemSize); |
| 55 | 151 | | SetConstraintCount(model.constraintCount); |
| 55 | 152 | | } |
| | 153 | |
|
| | 154 | | public override void Dispose() |
| | 155 | | { |
| 2149 | 156 | | base.Dispose(); |
| | 157 | |
|
| 2149 | 158 | | RemoveItems(); |
| 2149 | 159 | | } |
| | 160 | |
|
| | 161 | | public void SetConstraint(Constraint newConstraint) |
| | 162 | | { |
| 14 | 163 | | model.constraint = newConstraint; |
| | 164 | |
|
| 14 | 165 | | if (gridLayoutGroup == null) |
| 0 | 166 | | return; |
| | 167 | |
|
| 14 | 168 | | gridLayoutGroup.constraint = newConstraint; |
| 14 | 169 | | } |
| | 170 | |
|
| | 171 | | public void SetConstraintCount(int newConstraintCount) |
| | 172 | | { |
| 963 | 173 | | model.constraintCount = newConstraintCount; |
| | 174 | |
|
| 963 | 175 | | if (gridLayoutGroup == null) |
| 0 | 176 | | return; |
| | 177 | |
|
| 963 | 178 | | gridLayoutGroup.constraintCount = newConstraintCount; |
| 963 | 179 | | } |
| | 180 | |
|
| | 181 | | public void SetItemSizeToContainerAdaptation(bool adaptItemSizeToContainer) |
| | 182 | | { |
| 1 | 183 | | model.adaptHorizontallyItemSizeToContainer = adaptItemSizeToContainer; |
| 1 | 184 | | SetItemSize(model.itemSize); |
| 1 | 185 | | } |
| | 186 | |
|
| | 187 | | public void SetItemSize(Vector2 newItemSize) |
| | 188 | | { |
| 8938 | 189 | | Vector2 newSizeToApply = newItemSize; |
| | 190 | |
|
| 8938 | 191 | | if (instantiatedItems.Count > 0) |
| | 192 | | { |
| 1845 | 193 | | if (model.adaptVerticallyItemSizeToContainer && model.adaptHorizontallyItemSizeToContainer) |
| | 194 | | { |
| 15 | 195 | | CalculateAutoSize(out newSizeToApply); |
| 15 | 196 | | } |
| 1830 | 197 | | else if (model.adaptVerticallyItemSizeToContainer) |
| | 198 | | { |
| | 199 | | //TODO: We need to implement this functionality. Nobody is using it |
| | 200 | | // Please implement it if needed |
| 0 | 201 | | throw new Exception("Not implemented yet! Please implement the functionality"); |
| | 202 | | } |
| 1830 | 203 | | else if (model.adaptHorizontallyItemSizeToContainer) |
| | 204 | | { |
| 796 | 205 | | switch (model.constraint) |
| | 206 | | { |
| | 207 | | case Constraint.FixedColumnCount: |
| 792 | 208 | | CalculateHorizontalSizeForFixedColumnConstraint(out newSizeToApply); |
| 792 | 209 | | break; |
| | 210 | | case Constraint.FixedRowCount: |
| 2 | 211 | | CalculateHorizontalSizeForFixedRowConstraint(out newSizeToApply); |
| 2 | 212 | | break; |
| | 213 | | case Constraint.Flexible: |
| 2 | 214 | | CalculateHorizontalSizeForFlexibleConstraint(out newSizeToApply, newItemSize); |
| 2 | 215 | | break; |
| | 216 | | } |
| | 217 | | } |
| | 218 | | else |
| | 219 | | { |
| 1034 | 220 | | switch (model.constraint) |
| | 221 | | { |
| | 222 | | case Constraint.FixedColumnCount: |
| | 223 | | case Constraint.Flexible: |
| 1026 | 224 | | currentItemsPerRow = model.constraintCount; |
| 1026 | 225 | | break; |
| | 226 | | case Constraint.FixedRowCount: |
| 8 | 227 | | currentItemsPerRow = (int)Mathf.Ceil((float)instantiatedItems.Count / model.constraintCount); |
| | 228 | | break; |
| | 229 | | } |
| | 230 | | } |
| | 231 | | } |
| | 232 | |
|
| 8938 | 233 | | model.itemSize = newSizeToApply; |
| | 234 | |
|
| 8938 | 235 | | if (gridLayoutGroup == null) |
| 0 | 236 | | return; |
| | 237 | |
|
| 8938 | 238 | | gridLayoutGroup.cellSize = newSizeToApply; |
| | 239 | |
|
| 8938 | 240 | | ResizeGridContainer(); |
| 8938 | 241 | | } |
| | 242 | |
|
| | 243 | | private void CalculateAutoSize(out Vector2 newSizeToApply) |
| | 244 | | { |
| 15 | 245 | | float height = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.height : ((RectTransform)trans |
| 15 | 246 | | float width = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.width : ((RectTransform)transfo |
| | 247 | |
|
| 15 | 248 | | int amountsOfHorizontalItemsPerRow = instantiatedItems.Count / model.constraintCount; |
| 15 | 249 | | int amountsOfVerticalItemsPerColumn = instantiatedItems.Count / amountsOfHorizontalItemsPerRow; |
| | 250 | |
|
| 15 | 251 | | float extraSpaceToRemoveX = model.spaceBetweenItems.x * (amountsOfHorizontalItemsPerRow - 1); |
| 15 | 252 | | float extraSpaceToRemoveY = model.spaceBetweenItems.y * (amountsOfVerticalItemsPerColumn - 1); |
| | 253 | |
|
| 15 | 254 | | float itemWidth = model.recommendedWidthForFlexibleItems; |
| 15 | 255 | | float itemHeight = model.recommendedHeightForFlexibleItems; |
| | 256 | |
|
| 15 | 257 | | if (itemWidth * amountsOfHorizontalItemsPerRow + extraSpaceToRemoveX >= width) |
| 0 | 258 | | itemWidth = (width - extraSpaceToRemoveX) / amountsOfHorizontalItemsPerRow; |
| | 259 | |
|
| 15 | 260 | | if (itemWidth < model.minWidthForFlexibleItems) |
| 0 | 261 | | itemWidth = model.minWidthForFlexibleItems; |
| | 262 | |
|
| 15 | 263 | | if (itemHeight * amountsOfVerticalItemsPerColumn + extraSpaceToRemoveY >= height) |
| 0 | 264 | | itemHeight = (height - extraSpaceToRemoveY) / amountsOfVerticalItemsPerColumn; |
| | 265 | |
|
| 15 | 266 | | if (itemHeight < model.minHeightForFlexibleItems) |
| 0 | 267 | | itemHeight = model.minHeightForFlexibleItems; |
| | 268 | |
|
| 15 | 269 | | if (model.sameHeightAndWidhtFlexibleItem) |
| | 270 | | { |
| 15 | 271 | | float minValue = Mathf.Min(itemHeight, itemWidth); |
| 15 | 272 | | itemHeight = minValue; |
| 15 | 273 | | itemWidth = minValue; |
| | 274 | | } |
| | 275 | |
|
| 15 | 276 | | newSizeToApply = new Vector2( |
| | 277 | | itemWidth, |
| | 278 | | itemHeight); |
| | 279 | |
|
| 15 | 280 | | currentItemsPerRow = model.constraintCount; |
| 15 | 281 | | } |
| | 282 | |
|
| | 283 | | private void CalculateHorizontalSizeForFixedColumnConstraint(out Vector2 newSizeToApply) |
| | 284 | | { |
| 792 | 285 | | float width = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.width : ((RectTransform)transfo |
| 792 | 286 | | float extraSpaceToRemove = (model.spaceBetweenItems.x * (model.constraintCount - 1)) / model.constraintCount; |
| | 287 | |
|
| 792 | 288 | | newSizeToApply = new Vector2( |
| | 289 | | (width / model.constraintCount) - extraSpaceToRemove, |
| | 290 | | model.itemSize.y); |
| | 291 | |
|
| 792 | 292 | | currentItemsPerRow = model.constraintCount; |
| 792 | 293 | | } |
| | 294 | |
|
| | 295 | | private void CalculateHorizontalSizeForFixedRowConstraint(out Vector2 newSizeToApply) |
| | 296 | | { |
| 2 | 297 | | float height = ((RectTransform)transform).rect.height; |
| 2 | 298 | | float extraSpaceToRemove = (model.spaceBetweenItems.y / (model.constraintCount / 2f)); |
| | 299 | |
|
| 2 | 300 | | newSizeToApply = new Vector2( |
| | 301 | | model.itemSize.x, |
| | 302 | | (height / model.constraintCount) - extraSpaceToRemove); |
| | 303 | |
|
| 2 | 304 | | currentItemsPerRow = (int)Mathf.Ceil((float)instantiatedItems.Count / model.constraintCount); |
| 2 | 305 | | } |
| | 306 | |
|
| | 307 | | private void CalculateHorizontalSizeForFlexibleConstraint(out Vector2 newSizeToApply, Vector2 newItemSize) |
| | 308 | | { |
| 2 | 309 | | newSizeToApply = newItemSize; |
| | 310 | |
|
| 2 | 311 | | float width = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.width : ((RectTransform)transfo |
| 2 | 312 | | int numberOfPossibleItems = (int)(width / model.minWidthForFlexibleItems); |
| | 313 | |
|
| 2 | 314 | | SetConstraint(Constraint.FixedColumnCount); |
| | 315 | |
|
| 2 | 316 | | if (numberOfPossibleItems > 0) |
| | 317 | | { |
| 0 | 318 | | for (int numColumnsToTry = 1; numColumnsToTry <= numberOfPossibleItems; numColumnsToTry++) |
| | 319 | | { |
| 0 | 320 | | SetConstraintCount(numColumnsToTry); |
| 0 | 321 | | SetItemSize(model.itemSize); |
| 0 | 322 | | currentItemsPerRow = numColumnsToTry; |
| | 323 | |
|
| 0 | 324 | | if (model.itemSize.x < model.minWidthForFlexibleItems) |
| | 325 | | { |
| 0 | 326 | | SetConstraintCount(numColumnsToTry - 1); |
| 0 | 327 | | SetItemSize(model.itemSize); |
| 0 | 328 | | currentItemsPerRow = numColumnsToTry - 1; |
| 0 | 329 | | break; |
| | 330 | | } |
| | 331 | |
|
| 0 | 332 | | newSizeToApply = model.itemSize; |
| | 333 | | } |
| 0 | 334 | | } |
| | 335 | | else |
| | 336 | | { |
| 2 | 337 | | newSizeToApply = new Vector2(model.minWidthForFlexibleItems, newSizeToApply.y); |
| | 338 | | } |
| | 339 | |
|
| 2 | 340 | | SetConstraint(Constraint.Flexible); |
| 2 | 341 | | } |
| | 342 | |
|
| | 343 | | public void SetSpaceBetweenItems(Vector2 newSpace) |
| | 344 | | { |
| 2282 | 345 | | model.spaceBetweenItems = newSpace; |
| | 346 | |
|
| 2282 | 347 | | if (gridLayoutGroup == null) |
| 0 | 348 | | return; |
| | 349 | |
|
| 2282 | 350 | | gridLayoutGroup.spacing = newSpace; |
| 2282 | 351 | | } |
| | 352 | |
|
| | 353 | | public void SetMinWidthForFlexibleItems(float minWidthForFlexibleItems) |
| | 354 | | { |
| 1 | 355 | | model.minWidthForFlexibleItems = minWidthForFlexibleItems; |
| 1 | 356 | | SetItemSize(model.itemSize); |
| 1 | 357 | | } |
| | 358 | |
|
| | 359 | | public void SetItems(BaseComponentView prefab, int amountOfItems) |
| | 360 | | { |
| 2 | 361 | | RemoveItems(); |
| | 362 | |
|
| 20 | 363 | | for (int i = 0; i < amountOfItems; i++) |
| | 364 | | { |
| 8 | 365 | | BaseComponentView instanciatedItem = Instantiate(prefab); |
| 8 | 366 | | CreateItem(instanciatedItem, $"Item{i}"); |
| | 367 | | } |
| | 368 | |
|
| 2 | 369 | | SetItemSize(model.itemSize); |
| 2 | 370 | | } |
| | 371 | |
|
| | 372 | | public void SetItems(List<BaseComponentView> items) |
| | 373 | | { |
| 12 | 374 | | RemoveItems(); |
| | 375 | |
|
| 62 | 376 | | for (int i = 0; i < items.Count; i++) |
| | 377 | | { |
| 19 | 378 | | CreateItem(items[i], $"Item{i}"); |
| | 379 | | } |
| | 380 | |
|
| 12 | 381 | | SetItemSize(model.itemSize); |
| 12 | 382 | | } |
| | 383 | |
|
| | 384 | | public void AddItemWithResize(BaseComponentView item) |
| | 385 | | { |
| 1407 | 386 | | CreateItem(item, $"Item{instantiatedItems.Count}"); |
| 1407 | 387 | | SetItemSize(model.itemSize); |
| 1407 | 388 | | } |
| | 389 | |
|
| | 390 | | public void SetItemSizeForModel() => |
| 4 | 391 | | SetItemSize(model.itemSize); |
| | 392 | |
|
| | 393 | | public void AddItem(BaseComponentView item) => |
| 56 | 394 | | CreateItem(item, $"Item{instantiatedItems.Count}"); |
| | 395 | |
|
| | 396 | | public void RemoveItem(BaseComponentView item) |
| | 397 | | { |
| 8 | 398 | | BaseComponentView itemToRemove = instantiatedItems.FirstOrDefault(x => x == item); |
| 4 | 399 | | if (itemToRemove != null) |
| | 400 | | { |
| 4 | 401 | | Destroy(itemToRemove.gameObject); |
| 4 | 402 | | instantiatedItems.Remove(item); |
| | 403 | | } |
| | 404 | |
|
| 4 | 405 | | SetItemSize(model.itemSize); |
| 4 | 406 | | } |
| | 407 | |
|
| 0 | 408 | | public List<BaseComponentView> GetItems() { return instantiatedItems; } |
| | 409 | |
|
| | 410 | | public List<BaseComponentView> ExtractItems() |
| | 411 | | { |
| 3386 | 412 | | List<BaseComponentView> extractedItems = new List<BaseComponentView>(); |
| 11680 | 413 | | foreach (BaseComponentView item in instantiatedItems) |
| | 414 | | { |
| 2454 | 415 | | if (item != null) |
| 2449 | 416 | | item.transform.SetParent(null); |
| | 417 | |
|
| 2454 | 418 | | extractedItems.Add(item); |
| | 419 | | } |
| | 420 | |
|
| 3386 | 421 | | instantiatedItems.Clear(); |
| | 422 | |
|
| 3386 | 423 | | SetItemSize(model.itemSize); |
| | 424 | |
|
| 3386 | 425 | | return extractedItems; |
| | 426 | | } |
| | 427 | |
|
| | 428 | | public void RemoveItems() |
| | 429 | | { |
| 3153 | 430 | | List<BaseComponentView> itemsToDestroy = ExtractItems(); |
| 9234 | 431 | | foreach (BaseComponentView itemToDestroy in itemsToDestroy) |
| | 432 | | { |
| 1464 | 433 | | if (itemToDestroy != null) |
| 1459 | 434 | | DestroyImmediate(itemToDestroy.gameObject); |
| | 435 | | } |
| 3153 | 436 | | itemsToDestroy.Clear(); |
| | 437 | |
|
| 3153 | 438 | | instantiatedItems.Clear(); |
| | 439 | |
|
| 3153 | 440 | | SetItemSize(model.itemSize); |
| 3153 | 441 | | } |
| | 442 | |
|
| | 443 | | internal void CreateItem(BaseComponentView newItem, string name) |
| | 444 | | { |
| 1491 | 445 | | if (newItem == null) |
| 0 | 446 | | return; |
| | 447 | |
|
| 1491 | 448 | | newItem.transform.SetParent(transform); |
| 1491 | 449 | | newItem.transform.localPosition = Vector3.zero; |
| 1491 | 450 | | newItem.transform.localScale = Vector3.one; |
| 1491 | 451 | | newItem.name = name; |
| | 452 | |
|
| 1491 | 453 | | instantiatedItems.Add(newItem); |
| 1491 | 454 | | } |
| | 455 | |
|
| | 456 | | private void ResizeGridContainer() |
| | 457 | | { |
| 8938 | 458 | | int currentNumberOfItems = transform.childCount; |
| | 459 | |
|
| 8938 | 460 | | if (currentNumberOfItems == 0) |
| | 461 | | { |
| 6666 | 462 | | ((RectTransform)transform).sizeDelta = Vector2.zero; |
| 6666 | 463 | | return; |
| | 464 | | } |
| | 465 | |
|
| 2272 | 466 | | if (model.constraint == Constraint.FixedColumnCount) |
| | 467 | | { |
| 1303 | 468 | | int numRows = (int)Mathf.Ceil((float)currentNumberOfItems / model.constraintCount); |
| 1303 | 469 | | ((RectTransform)transform).sizeDelta = new Vector2( |
| | 470 | | model.adaptHorizontallyItemSizeToContainer ? ((RectTransform)transform).sizeDelta.x : (model.constraintC |
| | 471 | | (numRows * model.itemSize.y) + (model.spaceBetweenItems.y * (numRows - 1))); |
| 1303 | 472 | | } |
| 969 | 473 | | else if (model.constraint == Constraint.FixedRowCount) |
| | 474 | | { |
| 35 | 475 | | int numCols = (int)Mathf.Ceil((float)currentNumberOfItems / model.constraintCount); |
| 35 | 476 | | ((RectTransform)transform).sizeDelta = new Vector2( |
| | 477 | | (numCols * model.itemSize.x) + (model.spaceBetweenItems.x * (numCols - 1)), |
| | 478 | | model.adaptHorizontallyItemSizeToContainer ? ((RectTransform)transform).sizeDelta.y : (model.constraintC |
| | 479 | | } |
| | 480 | |
|
| 2272 | 481 | | SetSpaceBetweenItems(model.spaceBetweenItems); |
| 2272 | 482 | | } |
| | 483 | |
|
| | 484 | | private void RegisterCurrentInstantiatedItems() |
| | 485 | | { |
| 898 | 486 | | instantiatedItems.Clear(); |
| | 487 | |
|
| 4204 | 488 | | foreach (Transform child in transform) |
| | 489 | | { |
| 1204 | 490 | | BaseComponentView existingItem = child.GetComponent<BaseComponentView>(); |
| 1204 | 491 | | if (existingItem != null) |
| 1204 | 492 | | instantiatedItems.Add(existingItem); |
| | 493 | | else |
| 0 | 494 | | DestroyImmediate(child.gameObject); |
| | 495 | | } |
| | 496 | |
|
| 898 | 497 | | SetItemSize(model.itemSize); |
| 898 | 498 | | SetConstraintCount(model.constraintCount); |
| 898 | 499 | | } |
| | 500 | | } |