AutoResetView.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using AppUI.Bluetooth;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using UnityEngine.UI;
  8. /*
  9. 按下自动校准键出现倒计时3秒,同时伴有文字提示用户“三秒后即将开始校准,
  10. 请扶稳弓箭。”倒计时3秒后出现一个进度条也是三秒,用户在3秒内自己尽量扶稳弓即可,
  11. 进度条完成后,取一个平均值作为校准值。
  12. */
  13. public class AutoResetView : MonoBehaviour
  14. {
  15. public static AutoResetView ins;
  16. public static Action onInstantiate;
  17. public static void DoIdentity() {
  18. //if (InfraredDemo.running) return;
  19. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) return;
  20. if (SceneManager.GetActiveScene().name.StartsWith("GameDouble"))
  21. {
  22. if (GameObject.Find("AutoResetViewNewLeft")) return;
  23. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  24. resetView.name = "AutoResetViewNewLeft";
  25. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  26. //九轴双人也用新的这个
  27. autoResetViewNewScript.setTextKey("new-user-guider_tip_视角归位-瞄准-infraredD");
  28. autoResetViewNewScript.setPosLeft();
  29. autoResetViewNewScript.action_OnDestroy += () =>
  30. {
  31. //使用旧模式重置1p
  32. SmartBowDeviceHub.ins.Aim?.DoIdentity();
  33. };
  34. }
  35. else if (SceneManager.GetActiveScene().name == "InfraredGameDouble" || SceneManager.GetActiveScene().name == "WildAttackDouble")
  36. {
  37. if (GameObject.Find("AutoResetViewNewLeft")) return;
  38. GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
  39. resetView.name = "AutoResetViewNewLeft";
  40. AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
  41. autoResetViewNewScript.setTextKey("new-user-guider_tip_视角归位-瞄准-infraredD");
  42. autoResetViewNewScript.setPosLeft();
  43. autoResetViewNewScript.action_OnDestroy += () =>
  44. {
  45. if (SceneManager.GetActiveScene().name == "InfraredGameDouble")
  46. {
  47. InfraredDemo._ins?.SetAdjustPointsOffset(PlayerType.FirstPlayer);
  48. }else
  49. if (SceneManager.GetActiveScene().name == "WildAttackDouble")
  50. {
  51. WildAttack.GameMananger.GetInstance().ResetAim(WildAttack.PlayerTypeEnum.First);
  52. }
  53. };
  54. }
  55. else if (SceneManager.GetActiveScene().name.StartsWith("Game"))
  56. {
  57. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  58. }
  59. else if (SceneManager.GetActiveScene().name.StartsWith("DuckHunter"))
  60. {
  61. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  62. }
  63. //else if (SceneManager.GetActiveScene().name.Equals("WildAttackDouble"))
  64. //{
  65. // //双人场景校准直接校准1p
  66. // WildAttack.GameMananger.GetInstance().ResetAim(WildAttack.PlayerTypeEnum.First);
  67. //}
  68. else if (SceneManager.GetActiveScene().name.StartsWith("WildAttack"))
  69. {
  70. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  71. }
  72. else if (SceneManager.GetActiveScene().name.StartsWith("FruitMaster"))
  73. {
  74. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  75. }
  76. else if (SceneManager.GetActiveScene().name.StartsWith("Hyperspace"))
  77. {
  78. //Hyperspace01 Hyperspace02 Hyperspace03
  79. Instantiate(Resources.Load<GameObject>("Prefabs/Views/AutoResetView"));
  80. }
  81. else
  82. {
  83. SmartBowDeviceHub.ins.Aim?.DoIdentity();
  84. }
  85. }
  86. /// <summary>
  87. /// 引导页面弹出校准
  88. /// </summary>
  89. public static void onInfraredGuiderAutoResetView() {
  90. CreateResetViewPath("Prefabs/Views/AutoResetView");
  91. }
  92. /// <summary>
  93. /// 带背景的校准
  94. /// </summary>
  95. public static void onInfraredGuiderAutoResetViewOrBg()
  96. {
  97. CreateResetViewPath("Prefabs/Views/AutoResetViewOrBg");
  98. }
  99. static void CreateResetViewPath(string path) {
  100. AutoResetView autoResetView = Instantiate(Resources.Load<GameObject>(path)).GetComponent<AutoResetView>();
  101. autoResetView.bInfraredGuider = true;
  102. autoResetView.infraredGuiderTime = GlobalData.MyDeviceMode == DeviceMode.Gun ? CommonConfig.calibrationTimeGun : CommonConfig.calibrationTimeArchery;
  103. }
  104. /// <summary>
  105. /// 是否是引导
  106. /// </summary>
  107. public bool bInfraredGuider = false;
  108. public float infraredGuiderTime = 10;
  109. void Awake() {
  110. if (ins) {
  111. Destroy(gameObject);
  112. return;
  113. }
  114. ins = this;
  115. }
  116. void Start() {
  117. //进入引导游戏场景时候,设置时间
  118. if (bInfraredGuider)
  119. {
  120. prepareTime = infraredGuiderTime;
  121. }
  122. else {
  123. prepareTime = UserSettings.ins.calibrationTime;
  124. }
  125. Debug.Log("弓箭重置时间:" + prepareTime);
  126. if (SceneManager.GetActiveScene().name == "Game") {
  127. (transform.Find("IconHumanShoot") as RectTransform).anchoredPosition = new Vector2(-193, -85);
  128. }
  129. GetGuideTip().textFormatArgs = new object[]{showedPrepareTime = Mathf.CeilToInt(prepareTime)};
  130. GetGuideTip().ApplyToText();
  131. ChallengeTargetForResetView.Show();
  132. onInstantiate?.Invoke();
  133. }
  134. public Action action_OnDestroy;
  135. void OnDestroy() {
  136. if (ins == this) ins = null;
  137. action_OnDestroy?.Invoke();
  138. }
  139. float prepareTime = 3;
  140. int showedPrepareTime;
  141. bool bEnd = false;
  142. void Update() {
  143. // 使用 unscaledDeltaTime 代替 deltaTime
  144. prepareTime -= Time.unscaledDeltaTime;//Time.deltaTime;
  145. if (prepareTime <= 0) {
  146. //防止多次调用
  147. if (bEnd) return;
  148. bEnd = true;
  149. try {
  150. if (SceneManager.GetActiveScene().name.Equals("WildAttackDouble"))
  151. {
  152. //双人场景校准两个
  153. WildAttack.GameMananger.GetInstance().ResetAim(WildAttack.PlayerTypeEnum.First);
  154. WildAttack.GameMananger.GetInstance().ResetAim(WildAttack.PlayerTypeEnum.Second);
  155. }else if (SceneManager.GetActiveScene().name.StartsWith("WildAttack"))
  156. {
  157. WildAttack.GameMananger.GetInstance().ResetAim();
  158. }
  159. else
  160. {
  161. if (SmartBowDeviceHub.ins?.Aim?.bRuning9Axis()==true)
  162. {
  163. SmartBowDeviceHub.ins?.Aim?.DoIdentity();
  164. }
  165. else
  166. {
  167. //红外设备校准偏离点
  168. InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  169. }
  170. }
  171. }
  172. catch (Exception) {}
  173. //先隐藏子物体
  174. foreach (Transform child in transform)
  175. {
  176. child.gameObject.SetActive(false);
  177. }
  178. Destroy(gameObject,2.0f);
  179. } else {
  180. int curTime = Mathf.CeilToInt(prepareTime);
  181. if (showedPrepareTime != curTime) {
  182. showedPrepareTime = curTime;
  183. TextAutoLanguage2 gt = GetGuideTip();
  184. gt.textFormatArgs[0] = Mathf.CeilToInt(prepareTime);
  185. gt.ApplyToText();
  186. }
  187. }
  188. }
  189. TextAutoLanguage2 _guideTip;
  190. TextAutoLanguage2 GetGuideTip() {
  191. if (_guideTip == null) {
  192. if (SmartBowDeviceHub.ins?.Aim?.bRuning9Axis() == true)
  193. {
  194. Transform text = transform.Find("FrameTip/Text");
  195. text.gameObject.SetActive(true);
  196. _guideTip = text.GetComponentInChildren<TextAutoLanguage2>();
  197. }
  198. else {
  199. if (GlobalData.MyDeviceMode == DeviceMode.Gun)
  200. {
  201. Transform textInfrared = transform.Find("FrameTip/Text-Infrared-Gun");
  202. textInfrared.gameObject.SetActive(true);
  203. _guideTip = textInfrared.GetComponentInChildren<TextAutoLanguage2>();
  204. //如果连接M416则替换文字
  205. if (SmartBowDeviceHub.ins.GetMainConnectGunType().gunType == AimDeviceType.RifleM416)
  206. {
  207. _guideTip.SetTextKey("infrared-gun-m416");
  208. }
  209. }
  210. else {
  211. Transform textInfrared = transform.Find("FrameTip/Text-Infrared");
  212. textInfrared.gameObject.SetActive(true);
  213. Text text = textInfrared.GetComponentInChildren<Text>();
  214. text.alignment = TextAutoLanguage2.GetLanguage() == LanguageEnum.English? TextAnchor.MiddleCenter: TextAnchor.MiddleLeft;
  215. _guideTip = textInfrared.GetComponentInChildren<TextAutoLanguage2>();
  216. }
  217. }
  218. }
  219. return _guideTip;
  220. }
  221. }