1 using System; 2 using UnityEngine; 3 using System.Collections; 4 using Pvr_UnitySDKAPI; 5 6 7 namespace Pvr_UnitySDKAPI 8 { 9 public enum ControllerVariety//定义枚举类型的控制种类变量 10 { 11 Controller0, 12 Controller1, 13 } 14 } 15 16 public class Pvr_ControllerModuleInit : MonoBehaviour 17 { 18 19 public ControllerVariety Variety; 20 public bool IsCustomModel = false;//是否是定制的模型 21 [SerializeField] 22 private GameObject dot;//游戏对象-点 23 [SerializeField] 24 private GameObject rayLine;//游戏对象射线 25 [SerializeField] 26 private GameObject controller;//邮箱对象控制器 27 private int controllerDof = -1; 28 private int mainHand = 0; 29 private bool moduleState = true; //模块状态 30 31 void Awake() 32 { 33 Pvr_ControllerManager.PvrServiceStartSuccessEvent += ServiceStartSuccess; 34 Pvr_ControllerManager.SetControllerAbilityEvent += CheckControllerStateOfAbility; 35 Pvr_ControllerManager.ControllerStatusChangeEvent += CheckControllerStateForGoblin; 36 } 37 void OnDestroy() 38 { 39 Pvr_ControllerManager.PvrServiceStartSuccessEvent -= ServiceStartSuccess; 40 Pvr_ControllerManager.SetControllerAbilityEvent -= CheckControllerStateOfAbility; 41 Pvr_ControllerManager.ControllerStatusChangeEvent -= CheckControllerStateForGoblin; 42 } 43 44 private void ServiceStartSuccess() 45 { 46 mainHand = Controller.UPvr_GetMainHandNess();//设置主手柄 47 if (Variety == ControllerVariety.Controller0) 48 { 49 StartCoroutine(ShowAndHideRay(mainHand == 0 && Pvr_ControllerManager.controllerlink.controller0Connected)); 50 51 } 52 if (Variety == ControllerVariety.Controller1) 53 { 54 StartCoroutine(ShowAndHideRay(mainHand == 1 && Pvr_ControllerManager.controllerlink.controller1Connected)); 55 } 56 } 57 58 private void CheckControllerStateForGoblin(string state) 59 { 60 if (Variety == ControllerVariety.Controller0) 61 { 62 StartCoroutine(ShowAndHideRay(Convert.ToBoolean(Convert.ToInt16(state)))); 63 } 64 } 65 66 private void CheckControllerStateOfAbility(string data) 67 { 68 mainHand = Controller.UPvr_GetMainHandNess(); 69 if (Variety == ControllerVariety.Controller0) 70 { 71 StartCoroutine(ShowAndHideRay(mainHand == 0 && Pvr_ControllerManager.controllerlink.controller0Connected)); 72 73 } 74 if (Variety == ControllerVariety.Controller1) 75 { 76 StartCoroutine(ShowAndHideRay(mainHand == 1 && Pvr_ControllerManager.controllerlink.controller1Connected)); 77 } 78 } 79 80 private IEnumerator ShowAndHideRay(bool state) 81 { 82 yield return null; 83 yield return null; 84 if (moduleState) 85 { 86 dot.SetActive(state); 87 rayLine.SetActive(state); 88 } 89 } 90 91 public void ForceHideOrShow(bool state) 92 { 93 dot.SetActive(state); 94 rayLine.SetActive(state); 95 controller.SetActive(state); 96 moduleState = state; 97 } 98 }