| | 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 | |
|
| 2871 | 114 | | internal List<BaseComponentView> instantiatedItems = new List<BaseComponentView>(); |
| | 115 | |
|
| 1936 | 116 | | public int currentItemsPerRow { get; internal set; } |
| | 117 | |
|
| | 118 | | public override void Awake() |
| | 119 | | { |
| 1394 | 120 | | base.Awake(); |
| | 121 | |
|
| 1394 | 122 | | RegisterCurrentInstantiatedItems(); |
| 1394 | 123 | | } |
| | 124 | |
|
| | 125 | | public override void Show(bool instant = false) |
| | 126 | | { |
| 0 | 127 | | base.Show(instant); |
| | 128 | |
|
| 0 | 129 | | if (instant) |
| 0 | 130 | | gameObject.SetActive(true); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | public override void Hide(bool instant = false) |
| | 134 | | { |
| 0 | 135 | | base.Hide(instant); |
| | 136 | |
|
| 0 | 137 | | if (instant) |
| 0 | 138 | | gameObject.SetActive(false); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | public void Configure(GridContainerComponentModel newModel) |
| | 142 | | { |
| 1 | 143 | | model = newModel; |
| 1 | 144 | | RefreshControl(); |
| 1 | 145 | | } |
| | 146 | |
|
| | 147 | | public override void RefreshControl() |
| | 148 | | { |
| 2 | 149 | | if (model == null) |
| 0 | 150 | | return; |
| | 151 | |
|
| | 152 | | //We only do this if we are from editor, this is done because the editor doesn't call awake method |
| 2 | 153 | | if (!Application.isPlaying) |
| 0 | 154 | | RegisterCurrentInstantiatedItems(); |
| | 155 | |
|
| 2 | 156 | | SetConstraint(model.constraint); |
| 2 | 157 | | SetItemSize(model.itemSize); |
| 2 | 158 | | SetConstraintCount(model.constraintCount); |
| 2 | 159 | | SetSpaceBetweenItems(model.spaceBetweenItems); |
| 2 | 160 | | } |
| | 161 | |
|
| | 162 | | public override void OnScreenSizeChanged() |
| | 163 | | { |
| 21 | 164 | | base.OnScreenSizeChanged(); |
| | 165 | |
|
| 21 | 166 | | SetItemSize(model.itemSize); |
| 21 | 167 | | SetConstraintCount(model.constraintCount); |
| 21 | 168 | | } |
| | 169 | |
|
| | 170 | | public override void Dispose() |
| | 171 | | { |
| 2939 | 172 | | base.Dispose(); |
| | 173 | |
|
| 2939 | 174 | | RemoveItems(); |
| 2939 | 175 | | } |
| | 176 | |
|
| | 177 | | public void SetConstraint(Constraint newConstraint) |
| | 178 | | { |
| 7 | 179 | | model.constraint = newConstraint; |
| | 180 | |
|
| 7 | 181 | | if (gridLayoutGroup == null) |
| 0 | 182 | | return; |
| | 183 | |
|
| 7 | 184 | | gridLayoutGroup.constraint = newConstraint; |
| 7 | 185 | | } |
| | 186 | |
|
| | 187 | | public void SetConstraintCount(int newConstraintCount) |
| | 188 | | { |
| 1418 | 189 | | model.constraintCount = newConstraintCount; |
| | 190 | |
|
| 1418 | 191 | | if (gridLayoutGroup == null) |
| 0 | 192 | | return; |
| | 193 | |
|
| 1418 | 194 | | gridLayoutGroup.constraintCount = newConstraintCount; |
| 1418 | 195 | | } |
| | 196 | |
|
| | 197 | | public void SetItemSizeToContainerAdaptation(bool adaptItemSizeToContainer) |
| | 198 | | { |
| 1 | 199 | | model.adaptHorizontallyItemSizeToContainer = adaptItemSizeToContainer; |
| 1 | 200 | | SetItemSize(model.itemSize); |
| 1 | 201 | | } |
| | 202 | |
|
| | 203 | | public void SetItemSize(Vector2 newItemSize) |
| | 204 | | { |
| 13191 | 205 | | Vector2 newSizeToApply = newItemSize; |
| | 206 | |
|
| 13191 | 207 | | if (instantiatedItems.Count > 0) |
| | 208 | | { |
| 1938 | 209 | | if (model.adaptVerticallyItemSizeToContainer && model.adaptHorizontallyItemSizeToContainer) |
| | 210 | | { |
| 0 | 211 | | CalculateAutoSize(out newSizeToApply); |
| | 212 | | } |
| 1938 | 213 | | else if (model.adaptVerticallyItemSizeToContainer) |
| | 214 | | { |
| | 215 | | //TODO: We need to implement this functionality. Nobody is using it |
| | 216 | | // Please implement it if needed |
| 0 | 217 | | throw new Exception("Not implemented yet! Please implement the functionality"); |
| | 218 | | } |
| 1938 | 219 | | else if (model.adaptHorizontallyItemSizeToContainer) |
| | 220 | | { |
| 1754 | 221 | | switch (model.constraint) |
| | 222 | | { |
| | 223 | | case Constraint.FixedColumnCount: |
| 1750 | 224 | | CalculateHorizontalSizeForFixedColumnConstraint(out newSizeToApply); |
| 1750 | 225 | | break; |
| | 226 | | case Constraint.FixedRowCount: |
| 2 | 227 | | CalculateHorizontalSizeForFixedRowConstraint(out newSizeToApply); |
| 2 | 228 | | break; |
| | 229 | | case Constraint.Flexible: |
| 2 | 230 | | CalculateHorizontalSizeForFlexibleConstraint(out newSizeToApply, newItemSize); |
| 2 | 231 | | break; |
| | 232 | | } |
| | 233 | | } |
| | 234 | | else |
| | 235 | | { |
| 184 | 236 | | switch (model.constraint) |
| | 237 | | { |
| | 238 | | case Constraint.FixedColumnCount: |
| | 239 | | case Constraint.Flexible: |
| 176 | 240 | | currentItemsPerRow = model.constraintCount; |
| 176 | 241 | | break; |
| | 242 | | case Constraint.FixedRowCount: |
| 8 | 243 | | currentItemsPerRow = (int)Mathf.Ceil((float)instantiatedItems.Count / model.constraintCount); |
| | 244 | | break; |
| | 245 | | } |
| | 246 | | } |
| | 247 | | } |
| | 248 | |
|
| 13191 | 249 | | model.itemSize = newSizeToApply; |
| | 250 | |
|
| 13191 | 251 | | if (gridLayoutGroup == null) |
| 0 | 252 | | return; |
| | 253 | |
|
| 13191 | 254 | | gridLayoutGroup.cellSize = newSizeToApply; |
| | 255 | |
|
| 13191 | 256 | | ResizeGridContainer(); |
| 13191 | 257 | | } |
| | 258 | |
|
| | 259 | | private void CalculateAutoSize(out Vector2 newSizeToApply) |
| | 260 | | { |
| 0 | 261 | | float height = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.height : ((RectTransform)trans |
| 0 | 262 | | float width = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.width : ((RectTransform)transfo |
| | 263 | |
|
| 0 | 264 | | int amountsOfHorizontalItemsPerRow = instantiatedItems.Count / model.constraintCount; |
| 0 | 265 | | int amountsOfVerticalItemsPerColumn = instantiatedItems.Count / amountsOfHorizontalItemsPerRow; |
| | 266 | |
|
| 0 | 267 | | float extraSpaceToRemoveX = model.spaceBetweenItems.x * (amountsOfHorizontalItemsPerRow - 1); |
| 0 | 268 | | float extraSpaceToRemoveY = model.spaceBetweenItems.y * (amountsOfVerticalItemsPerColumn - 1); |
| | 269 | |
|
| 0 | 270 | | float itemWidth = model.recommendedWidthForFlexibleItems; |
| 0 | 271 | | float itemHeight = model.recommendedHeightForFlexibleItems; |
| | 272 | |
|
| 0 | 273 | | if (itemWidth * amountsOfHorizontalItemsPerRow + extraSpaceToRemoveX >= width) |
| 0 | 274 | | itemWidth = (width - extraSpaceToRemoveX) / amountsOfHorizontalItemsPerRow; |
| | 275 | |
|
| 0 | 276 | | if (itemWidth < model.minWidthForFlexibleItems) |
| 0 | 277 | | itemWidth = model.minWidthForFlexibleItems; |
| | 278 | |
|
| 0 | 279 | | if (itemHeight * amountsOfVerticalItemsPerColumn + extraSpaceToRemoveY >= height) |
| 0 | 280 | | itemHeight = (height - extraSpaceToRemoveY) / amountsOfVerticalItemsPerColumn; |
| | 281 | |
|
| 0 | 282 | | if (itemHeight < model.minHeightForFlexibleItems) |
| 0 | 283 | | itemHeight = model.minHeightForFlexibleItems; |
| | 284 | |
|
| 0 | 285 | | if (model.sameHeightAndWidhtFlexibleItem) |
| | 286 | | { |
| 0 | 287 | | float minValue = Mathf.Min(itemHeight, itemWidth); |
| 0 | 288 | | itemHeight = minValue; |
| 0 | 289 | | itemWidth = minValue; |
| | 290 | | } |
| | 291 | |
|
| 0 | 292 | | newSizeToApply = new Vector2( |
| | 293 | | itemWidth, |
| | 294 | | itemHeight); |
| | 295 | |
|
| 0 | 296 | | currentItemsPerRow = model.constraintCount; |
| 0 | 297 | | } |
| | 298 | |
|
| | 299 | | private void CalculateHorizontalSizeForFixedColumnConstraint(out Vector2 newSizeToApply) |
| | 300 | | { |
| 1750 | 301 | | float width = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.width : ((RectTransform)transfo |
| 1750 | 302 | | float extraSpaceToRemove = (model.spaceBetweenItems.x * (model.constraintCount - 1)) / model.constraintCount; |
| | 303 | |
|
| 1750 | 304 | | newSizeToApply = new Vector2( |
| | 305 | | (width / model.constraintCount) - extraSpaceToRemove, |
| | 306 | | model.itemSize.y); |
| | 307 | |
|
| 1750 | 308 | | currentItemsPerRow = model.constraintCount; |
| 1750 | 309 | | } |
| | 310 | |
|
| | 311 | | private void CalculateHorizontalSizeForFixedRowConstraint(out Vector2 newSizeToApply) |
| | 312 | | { |
| 2 | 313 | | float height = ((RectTransform)transform).rect.height; |
| 2 | 314 | | float extraSpaceToRemove = (model.spaceBetweenItems.y / (model.constraintCount / 2f)); |
| | 315 | |
|
| 2 | 316 | | newSizeToApply = new Vector2( |
| | 317 | | model.itemSize.x, |
| | 318 | | (height / model.constraintCount) - extraSpaceToRemove); |
| | 319 | |
|
| 2 | 320 | | currentItemsPerRow = (int)Mathf.Ceil((float)instantiatedItems.Count / model.constraintCount); |
| 2 | 321 | | } |
| | 322 | |
|
| | 323 | | private void CalculateHorizontalSizeForFlexibleConstraint(out Vector2 newSizeToApply, Vector2 newItemSize) |
| | 324 | | { |
| 2 | 325 | | newSizeToApply = newItemSize; |
| | 326 | |
|
| 2 | 327 | | float width = externalParentToAdaptSize != null ? externalParentToAdaptSize.rect.width : ((RectTransform)transfo |
| 2 | 328 | | int numberOfPossibleItems = (int)(width / model.minWidthForFlexibleItems); |
| | 329 | |
|
| 2 | 330 | | SetConstraint(Constraint.FixedColumnCount); |
| | 331 | |
|
| 2 | 332 | | if (numberOfPossibleItems > 0) |
| | 333 | | { |
| 0 | 334 | | for (int numColumnsToTry = 1; numColumnsToTry <= numberOfPossibleItems; numColumnsToTry++) |
| | 335 | | { |
| 0 | 336 | | SetConstraintCount(numColumnsToTry); |
| 0 | 337 | | SetItemSize(model.itemSize); |
| 0 | 338 | | currentItemsPerRow = numColumnsToTry; |
| | 339 | |
|
| 0 | 340 | | if (model.itemSize.x < model.minWidthForFlexibleItems) |
| | 341 | | { |
| 0 | 342 | | SetConstraintCount(numColumnsToTry - 1); |
| 0 | 343 | | SetItemSize(model.itemSize); |
| 0 | 344 | | currentItemsPerRow = numColumnsToTry - 1; |
| 0 | 345 | | break; |
| | 346 | | } |
| | 347 | |
|
| 0 | 348 | | newSizeToApply = model.itemSize; |
| | 349 | | } |
| | 350 | | } |
| | 351 | | else |
| | 352 | | { |
| 2 | 353 | | newSizeToApply = new Vector2(model.minWidthForFlexibleItems, newSizeToApply.y); |
| | 354 | | } |
| | 355 | |
|
| 2 | 356 | | SetConstraint(Constraint.Flexible); |
| 2 | 357 | | } |
| | 358 | |
|
| | 359 | | public void SetSpaceBetweenItems(Vector2 newSpace) |
| | 360 | | { |
| 1950 | 361 | | model.spaceBetweenItems = newSpace; |
| | 362 | |
|
| 1950 | 363 | | if (gridLayoutGroup == null) |
| 0 | 364 | | return; |
| | 365 | |
|
| 1950 | 366 | | gridLayoutGroup.spacing = newSpace; |
| 1950 | 367 | | } |
| | 368 | |
|
| | 369 | | public void SetMinWidthForFlexibleItems(float minWidthForFlexibleItems) |
| | 370 | | { |
| 1 | 371 | | model.minWidthForFlexibleItems = minWidthForFlexibleItems; |
| 1 | 372 | | SetItemSize(model.itemSize); |
| 1 | 373 | | } |
| | 374 | |
|
| | 375 | | public void SetItems(BaseComponentView prefab, int amountOfItems) |
| | 376 | | { |
| 0 | 377 | | RemoveItems(); |
| | 378 | |
|
| 0 | 379 | | for (int i = 0; i < amountOfItems; i++) |
| | 380 | | { |
| 0 | 381 | | BaseComponentView instanciatedItem = Instantiate(prefab); |
| 0 | 382 | | CreateItem(instanciatedItem, $"Item{i}"); |
| | 383 | | } |
| | 384 | |
|
| 0 | 385 | | SetItemSize(model.itemSize); |
| 0 | 386 | | } |
| | 387 | |
|
| | 388 | | public void SetItems(List<BaseComponentView> items) |
| | 389 | | { |
| 12 | 390 | | RemoveItems(); |
| | 391 | |
|
| 62 | 392 | | for (int i = 0; i < items.Count; i++) |
| | 393 | | { |
| 19 | 394 | | CreateItem(items[i], $"Item{i}"); |
| | 395 | | } |
| | 396 | |
|
| 12 | 397 | | SetItemSize(model.itemSize); |
| 12 | 398 | | } |
| | 399 | |
|
| | 400 | | public void AddItemWithResize(BaseComponentView item) |
| | 401 | | { |
| 1765 | 402 | | CreateItem(item, $"Item{instantiatedItems.Count}"); |
| 1765 | 403 | | SetItemSize(model.itemSize); |
| 1765 | 404 | | } |
| | 405 | |
|
| | 406 | | public void SetItemSizeForModel() => |
| 4 | 407 | | SetItemSize(model.itemSize); |
| | 408 | |
|
| | 409 | | public void AddItem(BaseComponentView item) => |
| 61 | 410 | | CreateItem(item, $"Item{instantiatedItems.Count}"); |
| | 411 | |
|
| | 412 | | public void RemoveItem(BaseComponentView item) |
| | 413 | | { |
| 8 | 414 | | BaseComponentView itemToRemove = instantiatedItems.FirstOrDefault(x => x == item); |
| 4 | 415 | | if (itemToRemove != null) |
| | 416 | | { |
| 4 | 417 | | Destroy(itemToRemove.gameObject); |
| 4 | 418 | | instantiatedItems.Remove(item); |
| | 419 | | } |
| | 420 | |
|
| 4 | 421 | | SetItemSize(model.itemSize); |
| 4 | 422 | | } |
| | 423 | |
|
| 491 | 424 | | public List<BaseComponentView> GetItems() { return instantiatedItems; } |
| | 425 | |
|
| | 426 | | public List<BaseComponentView> ExtractItems() |
| | 427 | | { |
| 5022 | 428 | | List<BaseComponentView> extractedItems = new List<BaseComponentView>(); |
| 12004 | 429 | | foreach (BaseComponentView item in instantiatedItems) |
| | 430 | | { |
| 980 | 431 | | if (item != null) |
| 974 | 432 | | item.transform.SetParent(null); |
| | 433 | |
|
| 980 | 434 | | extractedItems.Add(item); |
| | 435 | | } |
| | 436 | |
|
| 5022 | 437 | | instantiatedItems.Clear(); |
| | 438 | |
|
| 5022 | 439 | | SetItemSize(model.itemSize); |
| | 440 | |
|
| 5022 | 441 | | return extractedItems; |
| | 442 | | } |
| | 443 | |
|
| | 444 | | public void RemoveItems() |
| | 445 | | { |
| 4959 | 446 | | List<BaseComponentView> itemsToDestroy = ExtractItems(); |
| 11762 | 447 | | foreach (BaseComponentView itemToDestroy in itemsToDestroy) |
| | 448 | | { |
| 922 | 449 | | if (itemToDestroy != null) |
| 916 | 450 | | DestroyImmediate(itemToDestroy.gameObject); |
| | 451 | | } |
| 4959 | 452 | | itemsToDestroy.Clear(); |
| | 453 | |
|
| 4959 | 454 | | instantiatedItems.Clear(); |
| | 455 | |
|
| 4959 | 456 | | SetItemSize(model.itemSize); |
| 4959 | 457 | | } |
| | 458 | |
|
| | 459 | | internal void CreateItem(BaseComponentView newItem, string name) |
| | 460 | | { |
| 1846 | 461 | | if (newItem == null) |
| 0 | 462 | | return; |
| | 463 | |
|
| 1846 | 464 | | newItem.transform.SetParent(transform); |
| 1846 | 465 | | newItem.transform.localPosition = Vector3.zero; |
| 1846 | 466 | | newItem.transform.localScale = Vector3.one; |
| 1846 | 467 | | newItem.name = name; |
| | 468 | |
|
| 1846 | 469 | | instantiatedItems.Add(newItem); |
| 1846 | 470 | | } |
| | 471 | |
|
| | 472 | | private void ResizeGridContainer() |
| | 473 | | { |
| 13191 | 474 | | int currentNumberOfItems = transform.childCount; |
| | 475 | |
|
| 13191 | 476 | | if (currentNumberOfItems == 0) |
| | 477 | | { |
| 11244 | 478 | | ((RectTransform)transform).sizeDelta = Vector2.zero; |
| 11244 | 479 | | return; |
| | 480 | | } |
| | 481 | |
|
| 1947 | 482 | | if (model.constraint == Constraint.FixedColumnCount) |
| | 483 | | { |
| 1927 | 484 | | int numRows = (int)Mathf.Ceil((float)currentNumberOfItems / model.constraintCount); |
| 1927 | 485 | | ((RectTransform)transform).sizeDelta = new Vector2( |
| | 486 | | model.adaptHorizontallyItemSizeToContainer ? ((RectTransform)transform).sizeDelta.x : (model.constraintC |
| | 487 | | (numRows * model.itemSize.y) + (model.spaceBetweenItems.y * (numRows - 1))); |
| | 488 | | } |
| 20 | 489 | | else if (model.constraint == Constraint.FixedRowCount) |
| | 490 | | { |
| 16 | 491 | | int numCols = (int)Mathf.Ceil((float)currentNumberOfItems / model.constraintCount); |
| 16 | 492 | | ((RectTransform)transform).sizeDelta = new Vector2( |
| | 493 | | (numCols * model.itemSize.x) + (model.spaceBetweenItems.x * (numCols - 1)), |
| | 494 | | model.adaptHorizontallyItemSizeToContainer ? ((RectTransform)transform).sizeDelta.y : (model.constraintC |
| | 495 | | } |
| | 496 | |
|
| 1947 | 497 | | SetSpaceBetweenItems(model.spaceBetweenItems); |
| 1947 | 498 | | } |
| | 499 | |
|
| | 500 | | private void RegisterCurrentInstantiatedItems() |
| | 501 | | { |
| 1394 | 502 | | instantiatedItems.Clear(); |
| | 503 | |
|
| 3096 | 504 | | foreach (Transform child in transform) |
| | 505 | | { |
| 154 | 506 | | BaseComponentView existingItem = child.GetComponent<BaseComponentView>(); |
| 154 | 507 | | if (existingItem != null) |
| 154 | 508 | | instantiatedItems.Add(existingItem); |
| | 509 | | else |
| 0 | 510 | | DestroyImmediate(child.gameObject); |
| | 511 | | } |
| | 512 | |
|
| 1394 | 513 | | SetItemSize(model.itemSize); |
| 1394 | 514 | | SetConstraintCount(model.constraintCount); |
| 1394 | 515 | | } |
| | 516 | | } |