InputDeviceBase.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. namespace SC.XR.Unity.Module_InputSystem {
  8. public abstract class InputDeviceBase : SCModuleMono {
  9. /// <summary>
  10. /// 拖拽赋值
  11. /// </summary>
  12. public List<InputDevicePartBase> _inputDevicePartList;
  13. public List<InputDevicePartBase> inputDevicePartList {
  14. get {
  15. if(_inputDevicePartList == null || _inputDevicePartList.Count == 0) {
  16. _inputDevicePartList = GetComponentsInChildren<InputDevicePartBase>(true).ToList();
  17. }
  18. return _inputDevicePartList;
  19. }
  20. private set {
  21. _inputDevicePartList = value;
  22. }
  23. }
  24. /// <summary>
  25. /// 拖拽赋值
  26. /// </summary>
  27. [SerializeField]
  28. private InputDeviceUIBase _inputDeviceUI;
  29. public InputDeviceUIBase inputDeviceUI {
  30. get {
  31. if (_inputDeviceUI == null) {
  32. _inputDeviceUI = GetComponentInChildren<InputDeviceUIBase>(true);
  33. }
  34. return _inputDeviceUI;
  35. }
  36. private set {
  37. _inputDeviceUI = value;
  38. }
  39. }
  40. /// <summary>
  41. /// 输入设备类型
  42. /// </summary>
  43. public abstract InputDeviceType inputDeviceType { get; }
  44. public override void OnSCAwake() {
  45. base.OnSCAwake();
  46. foreach(var item in inputDevicePartList) {
  47. AddModule(item);
  48. }
  49. AddModule(inputDeviceUI);
  50. }
  51. public override void OnSCStart() {
  52. base.OnSCStart();
  53. InputDeviceStart();
  54. inputDeviceUI?.ModuleStart();
  55. }
  56. protected virtual void InputDeviceStart() {
  57. foreach(var item in inputDevicePartList) {
  58. item.ModuleStart();
  59. }
  60. }
  61. public override void OnSCDisable() {
  62. base.OnSCDisable();
  63. }
  64. public override void OnSCDestroy() {
  65. base.OnSCDestroy();
  66. inputDevicePartList = null;
  67. inputDeviceUI = null;
  68. }
  69. public void SetActiveInputDevicePart(InputDevicePartType partType, bool active) {
  70. if(active) {
  71. foreach(var devicePart in inputDevicePartList) {
  72. if(devicePart.PartType == partType) {
  73. if(false == devicePart.IsModuleStarted) {
  74. devicePart.ModuleStart();
  75. }
  76. break;
  77. }
  78. }
  79. } else {
  80. foreach(var devicePart in inputDevicePartList) {
  81. if(devicePart.PartType == partType) {
  82. if(devicePart.IsModuleStarted) {
  83. devicePart.ModuleStop();
  84. }
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. public virtual bool IsAnyPartVaild {
  91. get {
  92. foreach(var part in inputDevicePartList) {
  93. if(part.inputDataBase.isVaild) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. }
  100. }
  101. }