TestInput.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class TestInput : MonoBehaviour
  7. {
  8. public InputField inputField;
  9. public RectTransform parent;
  10. public RectTransform item;
  11. public GameObject linedown;
  12. public string[] content;
  13. void Start()
  14. {
  15. content = new string[] { "给水压力", "给水温度", "给水流量", "给水电动调节阀", "烟气压力","烟气温度","燃油压力","燃油流量","燃油阀开度","进风压力",
  16. "进风温度","风机频率","锅筒给水温度","锅筒给水压力","锅筒给水流量","过热蒸汽压力","过热蒸汽温度","过热蒸汽流量","过热蒸汽安全阀开度",
  17. "上锅筒压力","饱和蒸汽温度","上锅筒水温","上锅筒水位","上锅筒安全阀开度"};
  18. inputField.onValueChanged.AddListener((value) =>
  19. {
  20. if (value == "")
  21. {
  22. linedown.SetActive(false);
  23. }
  24. else
  25. {
  26. linedown.SetActive(true);
  27. }
  28. if (parent.childCount > 1)
  29. {
  30. for (int i = 1; i < parent.childCount; i++)
  31. {
  32. DestroyImmediate(parent.GetChild(i).gameObject);
  33. }
  34. }
  35. if (!string.IsNullOrEmpty(value))
  36. {
  37. if (parent.childCount > 1)
  38. {
  39. for (int i = 1; i < parent.childCount; i++)
  40. {
  41. DestroyImmediate(parent.GetChild(i).gameObject);
  42. }
  43. }
  44. foreach (var ch in content)
  45. {
  46. //关键比对代码
  47. if (ch.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
  48. {
  49. RectTransform rt = Instantiate(item, parent);
  50. rt.GetChild(0).GetComponent<Text>().text = ch;
  51. rt.GetComponent<Toggle>().isOn = true; //怎么准确获取到呢
  52. rt.GetComponent<Toggle>().onValueChanged.AddListener((value) =>
  53. {
  54. //发送事件同步变化,发送value
  55. });
  56. rt.gameObject.SetActive(true);
  57. }
  58. }
  59. }
  60. else
  61. {
  62. if (parent.childCount > 1)
  63. {
  64. for (int i = 1; i < parent.childCount; i++)
  65. {
  66. DestroyImmediate(parent.GetChild(i).gameObject);
  67. }
  68. }
  69. }
  70. });
  71. }
  72. }