| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public interface ICarouselComponentView |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Fill the model and updates the carousel with this data. |
| | 10 | | /// </summary> |
| | 11 | | /// <param name="model">Data to configure the carousel.</param> |
| | 12 | | void Configure(CarouselComponentModel model); |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Set the distance between carousel items. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="newSpace">Distance between items.</param> |
| | 18 | | void SetSpaceBetweenItems(float newSpace); |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Set the time that will be pass between carousel items. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="newTime">Time between items.</param> |
| | 24 | | void SetTimeBetweenItems(float newTime); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Set the time that will be pass during the transition between items. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="newTime">Transition time between items.</param> |
| | 30 | | void SetAnimationTransitionTime(float newTime); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Set the animation curve that will be used for the animation between items. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="newCurve">Animation curve between items.</param> |
| | 36 | | void SetAnimationCurve(AnimationCurve newCurve); |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Set the color of the carousel background. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="newColor">Background color.</param> |
| | 42 | | void SetBackgroundColor(Color newColor); |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Activates/Deactivates the controls to go to the next/previous item manually. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="isActived">True for activating the manual controls.</param> |
| | 48 | | void SetManualControlsActive(bool isActived); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Set the items of the carousel. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="items">List of UI components.</param> |
| | 54 | | /// <param name="instantiateNewCopyOfItems">Indicates if the items provided will be instantiated as a new copy or no |
| | 55 | | void SetItems(List<BaseComponentView> items, bool instantiateNewCopyOfItems = true); |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Get an item of the carousel. |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="index">Index of the list of items.</param> |
| | 61 | | /// <returns>A specific UI component.</returns> |
| | 62 | | BaseComponentView GetItem(int index); |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Get all the items of the carousel. |
| | 66 | | /// </summary> |
| | 67 | | /// <returns>The list of items.</returns> |
| | 68 | | List<BaseComponentView> GetAllItems(); |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Start carousel animation. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="fromIndex">It specifies from where item the carousel will start.</param> |
| | 74 | | /// <param name="startInmediately">True to directly execute the first transition.</param> |
| | 75 | | /// <param name="direction">Set the direction of the carousel animations: right or left.</param> |
| | 76 | | /// <param name="changeDirectionAfterFirstTransition">True to change the carousel direction just after the first tra |
| | 77 | | void StartCarousel(int fromIndex, bool startInmediately, CarouselDirection direction, bool changeDirectionAfterFirst |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Stop carousel animation. |
| | 81 | | /// </summary> |
| | 82 | | void StopCarousel(); |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Force the carousel to show the previous item. |
| | 86 | | /// </summary> |
| | 87 | | void GoToPreviousItem(); |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Force the carousel to show the next item. |
| | 91 | | /// </summary> |
| | 92 | | void GoToNextItem(); |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public enum CarouselDirection |
| | 96 | | { |
| | 97 | | Right, |
| | 98 | | Left |
| | 99 | | } |
| | 100 | |
|
| | 101 | | public class CarouselComponentView : BaseComponentView, ICarouselComponentView |
| | 102 | | { |
| | 103 | | [Header("Prefab References")] |
| | 104 | | [SerializeField] internal RectTransform itemsContainer; |
| | 105 | | [SerializeField] internal HorizontalLayoutGroup horizontalLayout; |
| | 106 | | [SerializeField] internal ScrollRect itemsScroll; |
| | 107 | | [SerializeField] internal RectTransform viewport; |
| | 108 | | [SerializeField] internal Image background; |
| | 109 | | [SerializeField] internal Button previousButton; |
| | 110 | | [SerializeField] internal Button nextButton; |
| | 111 | |
|
| | 112 | | [Header("Configuration")] |
| | 113 | | [SerializeField] internal CarouselComponentModel model; |
| | 114 | |
|
| 25 | 115 | | internal List<BaseComponentView> instantiatedItems = new List<BaseComponentView>(); |
| | 116 | | internal Coroutine itemsCoroutine; |
| | 117 | | internal int currentItemIndex = 0; |
| | 118 | | internal float currentFinalNormalizedPos; |
| | 119 | | internal bool isInTransition = false; |
| | 120 | | internal bool destroyOnlyUnnecesaryItems = false; |
| | 121 | |
|
| | 122 | | public override void PostInitialization() |
| | 123 | | { |
| 1 | 124 | | Configure(model); |
| 1 | 125 | | ConfigureManualButtonsEvents(); |
| 1 | 126 | | StartCarousel(); |
| 1 | 127 | | } |
| | 128 | |
|
| | 129 | | public void Configure(CarouselComponentModel model) |
| | 130 | | { |
| 1 | 131 | | this.model = model; |
| 1 | 132 | | RefreshControl(); |
| 1 | 133 | | } |
| | 134 | |
|
| | 135 | | public override void RefreshControl() |
| | 136 | | { |
| 1 | 137 | | if (model == null) |
| 0 | 138 | | return; |
| | 139 | |
|
| 1 | 140 | | SetSpaceBetweenItems(model.spaceBetweenItems); |
| 1 | 141 | | SetTimeBetweenItems(model.timeBetweenItems); |
| 1 | 142 | | SetAnimationTransitionTime(model.animationTransitionTime); |
| 1 | 143 | | SetAnimationCurve(model.animationCurve); |
| 1 | 144 | | SetBackgroundColor(model.backgroundColor); |
| 1 | 145 | | SetManualControlsActive(model.showManualControls); |
| 1 | 146 | | SetItems(model.items); |
| 1 | 147 | | } |
| | 148 | |
|
| | 149 | | public override void PostScreenSizeChanged() |
| | 150 | | { |
| 0 | 151 | | base.PostScreenSizeChanged(); |
| | 152 | |
|
| 0 | 153 | | ResizeAllItems(); |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | public override void Dispose() |
| | 157 | | { |
| 1 | 158 | | base.Dispose(); |
| | 159 | |
|
| 1 | 160 | | StopCarousel(); |
| 1 | 161 | | DestroyInstantiatedItems(true); |
| 1 | 162 | | } |
| | 163 | |
|
| | 164 | | public void SetSpaceBetweenItems(float newSpace) |
| | 165 | | { |
| 1 | 166 | | model.spaceBetweenItems = newSpace; |
| | 167 | |
|
| 1 | 168 | | if (horizontalLayout == null) |
| 0 | 169 | | return; |
| | 170 | |
|
| 1 | 171 | | horizontalLayout.spacing = newSpace; |
| 1 | 172 | | } |
| | 173 | |
|
| 0 | 174 | | public void SetTimeBetweenItems(float newTime) { model.timeBetweenItems = newTime; } |
| | 175 | |
|
| 0 | 176 | | public void SetAnimationTransitionTime(float newTime) { model.animationTransitionTime = newTime; } |
| | 177 | |
|
| 0 | 178 | | public void SetAnimationCurve(AnimationCurve newCurve) { model.animationCurve = newCurve; } |
| | 179 | |
|
| | 180 | | public void SetBackgroundColor(Color newColor) |
| | 181 | | { |
| 1 | 182 | | model.backgroundColor = newColor; |
| | 183 | |
|
| 1 | 184 | | if (background == null) |
| 0 | 185 | | return; |
| | 186 | |
|
| 1 | 187 | | background.color = newColor; |
| 1 | 188 | | } |
| | 189 | |
|
| | 190 | | public void SetManualControlsActive(bool isActived) |
| | 191 | | { |
| 1 | 192 | | model.showManualControls = isActived; |
| | 193 | |
|
| 1 | 194 | | if (previousButton == null || nextButton == null) |
| 0 | 195 | | return; |
| | 196 | |
|
| 1 | 197 | | previousButton.gameObject.SetActive(isActived); |
| 1 | 198 | | nextButton.gameObject.SetActive(isActived); |
| 1 | 199 | | } |
| | 200 | |
|
| | 201 | | public void SetItems(List<BaseComponentView> items, bool instantiateNewCopyOfItems = true) |
| | 202 | | { |
| 1 | 203 | | model.items = items; |
| | 204 | |
|
| 1 | 205 | | DestroyInstantiatedItems(!destroyOnlyUnnecesaryItems); |
| | 206 | |
|
| 8 | 207 | | for (int i = 0; i < items.Count; i++) |
| | 208 | | { |
| 3 | 209 | | CreateItem(items[i], $"Item{i}", instantiateNewCopyOfItems && !destroyOnlyUnnecesaryItems); |
| | 210 | | } |
| | 211 | |
|
| 1 | 212 | | if (!instantiateNewCopyOfItems) |
| 0 | 213 | | destroyOnlyUnnecesaryItems = true; |
| 1 | 214 | | } |
| | 215 | |
|
| | 216 | | public BaseComponentView GetItem(int index) |
| | 217 | | { |
| 0 | 218 | | if (index >= instantiatedItems.Count) |
| 0 | 219 | | return null; |
| | 220 | |
|
| 0 | 221 | | return instantiatedItems[index]; |
| | 222 | | } |
| | 223 | |
|
| 0 | 224 | | public List<BaseComponentView> GetAllItems() { return instantiatedItems; } |
| | 225 | |
|
| | 226 | | public void StartCarousel( |
| | 227 | | int fromIndex = 0, |
| | 228 | | bool startInmediately = false, |
| | 229 | | CarouselDirection direction = CarouselDirection.Right, |
| | 230 | | bool changeDirectionAfterFirstTransition = false) |
| | 231 | | { |
| 1 | 232 | | itemsCoroutine = CoroutineStarter.Start(RunCarouselCoroutine(fromIndex, startInmediately, direction, changeDirec |
| 1 | 233 | | } |
| | 234 | |
|
| | 235 | | public void StopCarousel() |
| | 236 | | { |
| 1 | 237 | | if (itemsCoroutine == null) |
| 0 | 238 | | return; |
| | 239 | |
|
| 1 | 240 | | CoroutineStarter.Stop(itemsCoroutine); |
| 1 | 241 | | itemsCoroutine = null; |
| 1 | 242 | | } |
| | 243 | |
|
| | 244 | | public void GoToPreviousItem() |
| | 245 | | { |
| 0 | 246 | | if (isInTransition) |
| 0 | 247 | | return; |
| | 248 | |
|
| 0 | 249 | | StopCarousel(); |
| 0 | 250 | | StartCarousel( |
| | 251 | | fromIndex: currentItemIndex, |
| | 252 | | startInmediately: true, |
| | 253 | | direction: CarouselDirection.Left, |
| | 254 | | changeDirectionAfterFirstTransition: true); |
| 0 | 255 | | } |
| | 256 | |
|
| | 257 | | public void GoToNextItem() |
| | 258 | | { |
| 0 | 259 | | if (isInTransition) |
| 0 | 260 | | return; |
| | 261 | |
|
| 0 | 262 | | StopCarousel(); |
| 0 | 263 | | StartCarousel( |
| | 264 | | fromIndex: currentItemIndex, |
| | 265 | | startInmediately: true, |
| | 266 | | direction: CarouselDirection.Right, |
| | 267 | | changeDirectionAfterFirstTransition: false); |
| 0 | 268 | | } |
| | 269 | |
|
| | 270 | | internal void ConfigureManualButtonsEvents() |
| | 271 | | { |
| 1 | 272 | | if (previousButton != null) |
| | 273 | | { |
| 1 | 274 | | previousButton.onClick.RemoveAllListeners(); |
| 1 | 275 | | previousButton.onClick.AddListener(GoToPreviousItem); |
| | 276 | | } |
| | 277 | |
|
| 1 | 278 | | if (nextButton != null) |
| | 279 | | { |
| 1 | 280 | | nextButton.onClick.RemoveAllListeners(); |
| 1 | 281 | | nextButton.onClick.AddListener(GoToNextItem); |
| | 282 | | } |
| 1 | 283 | | } |
| | 284 | |
|
| | 285 | | internal void CreateItem(BaseComponentView newItem, string name, bool instantiateNewCopyOfItem = true) |
| | 286 | | { |
| 3 | 287 | | if (Application.isPlaying) |
| | 288 | | { |
| 3 | 289 | | InstantiateItem(newItem, name, instantiateNewCopyOfItem); |
| 3 | 290 | | } |
| | 291 | | else |
| | 292 | | { |
| 0 | 293 | | if (isActiveAndEnabled) |
| 0 | 294 | | StartCoroutine(IntantiateItemOnEditor(newItem, name)); |
| | 295 | | } |
| 0 | 296 | | } |
| | 297 | |
|
| | 298 | | internal void InstantiateItem(BaseComponentView newItem, string name, bool instantiateNewCopyOfItem = true) |
| | 299 | | { |
| 3 | 300 | | if (newItem == null) |
| 0 | 301 | | return; |
| | 302 | |
|
| | 303 | | BaseComponentView newGO; |
| 3 | 304 | | if (instantiateNewCopyOfItem) |
| | 305 | | { |
| 3 | 306 | | newGO = Instantiate(newItem, itemsContainer); |
| 3 | 307 | | } |
| | 308 | | else |
| | 309 | | { |
| 0 | 310 | | newGO = newItem; |
| 0 | 311 | | newGO.transform.SetParent(itemsContainer); |
| 0 | 312 | | newGO.transform.localPosition = Vector3.zero; |
| 0 | 313 | | newGO.transform.localScale = Vector3.one; |
| | 314 | | } |
| | 315 | |
|
| 3 | 316 | | newGO.name = name; |
| 3 | 317 | | instantiatedItems.Add(newGO); |
| 3 | 318 | | ResizeItem(newGO); |
| 3 | 319 | | } |
| | 320 | |
|
| | 321 | | internal void ResizeItem(BaseComponentView item) |
| | 322 | | { |
| 3 | 323 | | ((RectTransform)item.transform).sizeDelta = new Vector2(viewport.rect.width, viewport.rect.height); |
| | 324 | |
|
| 3 | 325 | | itemsContainer.offsetMin = Vector2.zero; |
| 3 | 326 | | if (instantiatedItems.Count > 1) |
| | 327 | | { |
| 2 | 328 | | float extraSpace = (instantiatedItems.Count - 1) * model.spaceBetweenItems; |
| 2 | 329 | | itemsContainer.offsetMax = new Vector2(viewport.rect.width * (instantiatedItems.Count - 1) + extraSpace, 0); |
| | 330 | | } |
| 3 | 331 | | } |
| | 332 | |
|
| | 333 | | internal void ResizeAllItems() |
| | 334 | | { |
| 0 | 335 | | foreach (BaseComponentView item in instantiatedItems) |
| | 336 | | { |
| 0 | 337 | | ResizeItem(item); |
| | 338 | | } |
| 0 | 339 | | } |
| | 340 | |
|
| | 341 | | internal IEnumerator IntantiateItemOnEditor(BaseComponentView newItem, string name) |
| | 342 | | { |
| 0 | 343 | | yield return null; |
| 0 | 344 | | InstantiateItem(newItem, name); |
| 0 | 345 | | } |
| | 346 | |
|
| | 347 | | internal void DestroyInstantiatedItems(bool forzeToDestroyAll) |
| | 348 | | { |
| 2 | 349 | | if (forzeToDestroyAll) |
| | 350 | | { |
| 22 | 351 | | foreach (Transform child in itemsContainer) |
| | 352 | | { |
| 9 | 353 | | if (Application.isPlaying) |
| | 354 | | { |
| 9 | 355 | | Destroy(child.gameObject); |
| 9 | 356 | | } |
| | 357 | | else |
| | 358 | | { |
| 0 | 359 | | if (isActiveAndEnabled) |
| 0 | 360 | | StartCoroutine(DestroyGameObjectOnEditor(child.gameObject)); |
| | 361 | | } |
| | 362 | | } |
| | 363 | | } |
| | 364 | | else |
| | 365 | | { |
| 0 | 366 | | foreach (BaseComponentView child in instantiatedItems) |
| | 367 | | { |
| 0 | 368 | | if (!model.items.Contains(child)) |
| | 369 | | { |
| 0 | 370 | | Destroy(child.gameObject); |
| | 371 | | } |
| | 372 | | } |
| | 373 | | } |
| | 374 | |
|
| 2 | 375 | | instantiatedItems.Clear(); |
| | 376 | |
|
| 2 | 377 | | itemsContainer.offsetMin = Vector2.zero; |
| 2 | 378 | | itemsContainer.offsetMax = Vector2.zero; |
| 2 | 379 | | } |
| | 380 | |
|
| | 381 | | internal IEnumerator DestroyGameObjectOnEditor(GameObject go) |
| | 382 | | { |
| 0 | 383 | | yield return null; |
| 0 | 384 | | DestroyImmediate(go); |
| 0 | 385 | | } |
| | 386 | |
|
| | 387 | | internal IEnumerator RunCarouselCoroutine( |
| | 388 | | int fromIndex = 0, |
| | 389 | | bool startInmediately = false, |
| | 390 | | CarouselDirection direction = CarouselDirection.Right, |
| | 391 | | bool changeDirectionAfterFirstTransition = false) |
| | 392 | | { |
| 1 | 393 | | currentItemIndex = fromIndex; |
| | 394 | |
|
| 0 | 395 | | while (true) |
| | 396 | | { |
| 1 | 397 | | if (!startInmediately) |
| 1 | 398 | | yield return new WaitForSeconds(model.timeBetweenItems); |
| | 399 | | else |
| 0 | 400 | | startInmediately = false; |
| | 401 | |
|
| 0 | 402 | | if (instantiatedItems.Count > 0) |
| | 403 | | { |
| 0 | 404 | | if (direction == CarouselDirection.Right) |
| | 405 | | { |
| 0 | 406 | | yield return RunRightAnimation(); |
| | 407 | |
|
| 0 | 408 | | if (changeDirectionAfterFirstTransition) |
| | 409 | | { |
| 0 | 410 | | direction = CarouselDirection.Left; |
| 0 | 411 | | changeDirectionAfterFirstTransition = false; |
| | 412 | | } |
| 0 | 413 | | } |
| | 414 | | else |
| | 415 | | { |
| 0 | 416 | | yield return RunLeftAnimation(); |
| | 417 | |
|
| 0 | 418 | | if (changeDirectionAfterFirstTransition) |
| | 419 | | { |
| 0 | 420 | | direction = CarouselDirection.Right; |
| 0 | 421 | | changeDirectionAfterFirstTransition = false; |
| | 422 | | } |
| | 423 | | } |
| | 424 | | } |
| | 425 | | } |
| | 426 | | } |
| | 427 | |
|
| | 428 | | internal IEnumerator RunRightAnimation() |
| | 429 | | { |
| 0 | 430 | | if (currentItemIndex == instantiatedItems.Count - 1) |
| | 431 | | { |
| 0 | 432 | | currentItemIndex = 1; |
| 0 | 433 | | itemsScroll.horizontalNormalizedPosition = 0f; |
| 0 | 434 | | itemsContainer.GetChild(itemsContainer.childCount - 1).SetAsFirstSibling(); |
| | 435 | |
|
| 0 | 436 | | yield return RunAnimationCoroutine(CarouselDirection.Right); |
| 0 | 437 | | } |
| | 438 | | else |
| | 439 | | { |
| 0 | 440 | | yield return RunAnimationCoroutine(CarouselDirection.Right); |
| | 441 | |
|
| 0 | 442 | | currentItemIndex++; |
| 0 | 443 | | if (currentItemIndex >= instantiatedItems.Count - 1) |
| | 444 | | { |
| 0 | 445 | | currentItemIndex = 0; |
| 0 | 446 | | itemsScroll.horizontalNormalizedPosition = 0f; |
| 0 | 447 | | itemsContainer.GetChild(itemsContainer.childCount - 1).SetAsFirstSibling(); |
| | 448 | | } |
| | 449 | | } |
| 0 | 450 | | } |
| | 451 | |
|
| | 452 | | internal IEnumerator RunLeftAnimation() |
| | 453 | | { |
| 0 | 454 | | if (currentItemIndex == 0) |
| | 455 | | { |
| 0 | 456 | | currentItemIndex = instantiatedItems.Count - 2; |
| 0 | 457 | | itemsScroll.horizontalNormalizedPosition = 1f; |
| 0 | 458 | | itemsContainer.GetChild(0).SetAsLastSibling(); |
| | 459 | |
|
| 0 | 460 | | yield return RunAnimationCoroutine(CarouselDirection.Left); |
| 0 | 461 | | } |
| | 462 | | else |
| | 463 | | { |
| 0 | 464 | | yield return RunAnimationCoroutine(CarouselDirection.Left); |
| | 465 | |
|
| 0 | 466 | | currentItemIndex--; |
| 0 | 467 | | if (currentItemIndex <= 0) |
| | 468 | | { |
| 0 | 469 | | currentItemIndex = instantiatedItems.Count - 1; |
| 0 | 470 | | itemsScroll.horizontalNormalizedPosition = 1f; |
| 0 | 471 | | itemsContainer.GetChild(0).SetAsLastSibling(); |
| | 472 | | } |
| | 473 | | } |
| 0 | 474 | | } |
| | 475 | |
|
| | 476 | | internal IEnumerator RunAnimationCoroutine(CarouselDirection direction) |
| | 477 | | { |
| 0 | 478 | | isInTransition = true; |
| 0 | 479 | | float currentAnimationTime = 0f; |
| 0 | 480 | | float initialNormalizedPos = itemsScroll.horizontalNormalizedPosition; |
| | 481 | |
|
| 0 | 482 | | if (direction == CarouselDirection.Right) |
| 0 | 483 | | currentFinalNormalizedPos = initialNormalizedPos + (1f / (instantiatedItems.Count - 1)); |
| | 484 | | else |
| 0 | 485 | | currentFinalNormalizedPos = initialNormalizedPos - (1f / (instantiatedItems.Count - 1)); |
| | 486 | |
|
| 0 | 487 | | while (currentAnimationTime <= model.animationTransitionTime) |
| | 488 | | { |
| 0 | 489 | | itemsScroll.horizontalNormalizedPosition = Mathf.Clamp01(Mathf.Lerp( |
| | 490 | | initialNormalizedPos, |
| | 491 | | currentFinalNormalizedPos, |
| | 492 | | model.animationCurve.Evaluate(currentAnimationTime / model.animationTransitionTime))); |
| | 493 | |
|
| 0 | 494 | | currentAnimationTime += Time.deltaTime; |
| | 495 | |
|
| 0 | 496 | | yield return null; |
| | 497 | | } |
| | 498 | |
|
| 0 | 499 | | itemsScroll.horizontalNormalizedPosition = currentFinalNormalizedPos; |
| 0 | 500 | | isInTransition = false; |
| 0 | 501 | | } |
| | 502 | | } |