DistanceSelectView.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. }
  14. void Start()
  15. {
  16. Transform layout = transform.Find("Layout");
  17. GameObject itemPrefab = layout.Find("Item").gameObject;
  18. for (int i = 1; i < distanceCanSelected.Length; i++)
  19. {
  20. Instantiate(
  21. itemPrefab, Vector3.zero, Quaternion.identity, transform.Find("Layout")
  22. );
  23. }
  24. for (int i = 0; i < distanceCanSelected.Length; i++)
  25. {
  26. int distance = distanceCanSelected[i];
  27. Transform item = layout.GetChild(i);
  28. var tal = item.transform.Find("Text").GetComponent<TextAutoLanguage2>();
  29. tal.textFormatArgs = new object[] { distance };
  30. tal.ApplyToText();
  31. Button btn = item.gameObject.GetComponent<Button>();
  32. GetComponentInParent<KeyBoardNavigation>()?.buttons.Add(btn);
  33. btn.onClick.AddListener(delegate () {
  34. AudioMgr.ins.PlayBtn();
  35. OnClickSelectDistance?.Invoke(distance);
  36. });
  37. }
  38. }
  39. void OnDisable()
  40. {
  41. SimulateMouseController.ins?.RemoveOpenLocker(this);
  42. }
  43. }