DistanceSelectView.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6. public class DistanceSelectView : MonoBehaviour
  7. {
  8. static int[] distanceCanSelected = { 10, 20, 30, 50, 70 }; // { 6, 8, 10, 15, 20 };
  9. public Action<float> OnClickSelectDistance;
  10. void OnEnable()
  11. {
  12. SimulateMouseController.ins?.AddOpenLocker(this);
  13. Time.timeScale = 0;
  14. }
  15. void Start()
  16. {
  17. Transform layout = transform.Find("Layout");
  18. GameObject itemPrefab = layout.Find("Item").gameObject;
  19. for (int i = 1; i < distanceCanSelected.Length; i++)
  20. {
  21. Instantiate(
  22. itemPrefab, Vector3.zero, Quaternion.identity, transform.Find("Layout")
  23. );
  24. }
  25. for (int i = 0; i < distanceCanSelected.Length; i++)
  26. {
  27. int distance = distanceCanSelected[i];
  28. Transform item = layout.GetChild(i);
  29. var tal = item.transform.Find("Text").GetComponent<TextAutoLanguage2>();
  30. tal.textFormatArgs = new object[] { distance };
  31. tal.ApplyToText();
  32. Button btn = item.gameObject.GetComponent<Button>();
  33. GetComponentInParent<KeyBoardNavigation>()?.buttons.Add(btn);
  34. btn.onClick.AddListener(delegate () {
  35. AudioMgr.ins.PlayBtn();
  36. OnClickSelectDistance?.Invoke(distance);
  37. });
  38. }
  39. }
  40. void OnDisable()
  41. {
  42. SimulateMouseController.ins?.RemoveOpenLocker(this);
  43. Time.timeScale = 1;
  44. }
  45. private void OnDestroy()
  46. {
  47. Time.timeScale = 1;
  48. }
  49. }