Cell.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * FancyScrollView (https://github.com/setchi/FancyScrollView)
  3. * Copyright (c) 2020 setchi
  4. * Licensed under MIT (https://github.com/setchi/FancyScrollView/blob/master/LICENSE)
  5. */
  6. using System;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. namespace FancyScrollView.FocusOn
  11. {
  12. class Cell : FancyCell<ItemData, Context>
  13. {
  14. [SerializeField] Animator animator = default;
  15. [SerializeField] Text message = default;
  16. [SerializeField] Image image = default;
  17. [SerializeField] Button button = default;
  18. [SerializeField] Image icon = default;
  19. [SerializeField] Text title = default;
  20. [SerializeField] GameObject line = default;
  21. static class AnimatorHash
  22. {
  23. public static readonly int Scroll = Animator.StringToHash("scroll");
  24. }
  25. public override void Initialize()
  26. {
  27. button.onClick.AddListener(() => Context.OnCellClicked?.Invoke(Index,true));
  28. }
  29. public void onCellClickedEvent() {
  30. Context.OnCellClicked?.Invoke(Index, false);
  31. }
  32. public override void UpdateContent(ItemData itemData)
  33. {
  34. message.text = itemData.TextId;
  35. icon.sprite = itemData.Sprite;
  36. //title.text = itemData.Name;
  37. if (itemData.LanguageType == 0)
  38. {
  39. title.gameObject.AddComponent<TextAutoLanguage>().SetText(int.Parse(itemData.TextId));
  40. }
  41. else {
  42. title.gameObject.AddComponent<TextAutoLanguage2>().SetTextKey(itemData.TextId);
  43. }
  44. var selected = Context.SelectedIndex == Index;
  45. line.SetActive(selected);
  46. title.gameObject.SetActive(selected);
  47. //image.color = selected
  48. // ? new Color32(0, 255, 255, 100)
  49. // : new Color32(255, 255, 255, 77);
  50. }
  51. public override void UpdatePosition(float position)
  52. {
  53. currentPosition = position;
  54. if (animator.isActiveAndEnabled)
  55. {
  56. animator.Play(AnimatorHash.Scroll, -1, position);
  57. }
  58. animator.speed = 0;
  59. }
  60. // GameObject が非アクティブになると Animator がリセットされてしまうため
  61. // 現在位置を保持しておいて OnEnable のタイミングで現在位置を再設定します
  62. float currentPosition = 0;
  63. void OnEnable() => UpdatePosition(currentPosition);
  64. }
  65. }