GroundHeightStep.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using SC.XR.Unity;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class GroundHeightStep : AbstractSafetyAreaStep<SafetyAreaMono>
  6. {
  7. //private string PLANE_HEIGHT_SAVE_KEY = "PLANE_HEIGHT_SAVE_KEY";
  8. private float GROUND_HEIGHT_SET_DELAY = 2f;
  9. private bool canSetPlaneHeight = false;
  10. private float planeHeight;
  11. public GroundHeightStep(SafetyAreaMono safetyAreaMono) : base(safetyAreaMono)
  12. {
  13. LoadPlaneHeight();
  14. }
  15. public override void OnEnterStep(object data)
  16. {
  17. //reference.groundHeightUI.gameObject.SetActive(true);
  18. //reference.safetyGreyCameraUI.gameObject.SetActive(true);
  19. reference.groundHeightUI.Init();
  20. reference.groundHeightUI.OnConfirmClick += SwitchToPlayAreaStep;
  21. reference.groundHeightUI.OnResetClick += ResetPlaneHeight;
  22. reference.groundHeightUI.OnCancelClick += ExitSafetyAreaProcess;
  23. canSetPlaneHeight = false;
  24. SafetyAreaManager.Instance.DelayShow(2f, GroundHeightSetDelay);
  25. //目前只用于,超出安全区域直接点击原地区域出现
  26. if (data != null)
  27. {
  28. LoadPlaneHeight();
  29. }
  30. else
  31. {
  32. ResetPlaneHeight();
  33. }
  34. reference.CreateSafetyPlane();
  35. if (!SafetyAreaManager.Instance.onlySettingGroundHeight && (data == null))
  36. {
  37. SafetyAreaManager.Instance.DelayShow(1f, DelayShowUI);
  38. }
  39. else
  40. {
  41. reference.groundHeightUI.gameObject.SetActive(true);
  42. reference.EnablePlaneMeshRenderer();
  43. SetPlayAreaMat();
  44. reference.safetyPlaneMono.StopPlaneAnimation();
  45. UnFreezePlaneHeight();
  46. }
  47. }
  48. public override void OnExitStep()
  49. {
  50. SavePlaneHeight();
  51. FreezePlaneHeight();
  52. SetPlayAreaConfirmedMat();
  53. reference.groundHeightUI.OnConfirmClick -= SwitchToPlayAreaStep;
  54. reference.groundHeightUI.OnResetClick -= ResetPlaneHeight;
  55. reference.groundHeightUI.OnCancelClick -= ExitSafetyAreaProcess;
  56. reference.groundHeightUI.Release();
  57. reference.groundHeightUI.gameObject.SetActive(false);
  58. //reference.safetyGreyCameraUI.gameObject.SetActive(false);
  59. }
  60. public override SafetyAreaStepEnum GetStepEnum()
  61. {
  62. return SafetyAreaStepEnum.GroundHeight;
  63. }
  64. private void GroundHeightSetDelay()
  65. {
  66. canSetPlaneHeight = true;
  67. }
  68. private void DelayShowUI()
  69. {
  70. if (SafetyAreaManager.Instance.GetCurrentStepEnum() == SafetyAreaStepEnum.GroundHeight)
  71. {
  72. reference.groundHeightUI.gameObject.SetActive(true);
  73. reference.EnablePlaneMeshRenderer();
  74. SetPlayAreaMat();
  75. reference.safetyPlaneMono.StopPlaneAnimation();
  76. UnFreezePlaneHeight();
  77. }
  78. }
  79. private void ExitSafetyAreaProcess()
  80. {
  81. reference.DestroySafetyPlane();
  82. SafetyAreaManager.Instance.ExitSafeAreaStep();
  83. }
  84. private void DelaySafetyAreaProcess()
  85. {
  86. SafetyAreaManager.Instance.DelayExitSafeAreaStep(0.7f, reference.DestroySafetyPlane);
  87. }
  88. //设置确定地面高度材质
  89. public void SetPlayAreaMat()
  90. {
  91. if (reference.safetyPlaneMono == null) return;
  92. reference.safetyPlaneMono.SetPlaneMat(reference.safetyPlaneMat);
  93. reference.safetyPlaneMono.SetHoverPlaneMat(reference.safetyPlaneMat);
  94. }
  95. //设置以确定地面高度参数
  96. public void SetPlayAreaConfirmedMat()
  97. {
  98. if (reference.safetyPlaneMono == null) return;
  99. reference.safetyPlaneMono.SetPlaneMat(reference.safetyPlaneConfirmedMat);
  100. reference.safetyPlaneMono.SetHoverPlaneMat(reference.safetyPlaneHoverMat);
  101. }
  102. //允许平面高度变化
  103. public void UnFreezePlaneHeight()
  104. {
  105. if (reference.safetyPlaneMono == null)
  106. {
  107. Debug.LogError("safetyPlaneMono is Null UnFreezePlaneHeight");
  108. return;
  109. }
  110. reference.safetyPlaneMono.UnFreezePlaneHeight();
  111. }
  112. //冻结平面高度
  113. public void FreezePlaneHeight()
  114. {
  115. if (reference.safetyPlaneMono == null)
  116. {
  117. return;
  118. }
  119. reference.safetyPlaneMono.FreezePlaneHeight();
  120. }
  121. private void SwitchToPlayAreaStep()
  122. {
  123. reference.safetyPlaneMono.StartPlaneAnimation();
  124. if (SafetyAreaManager.Instance.onlySettingGroundHeight)
  125. {
  126. //SafetyAreaManager.Instance.ResetSafetyAreaHeight();
  127. //ExitSafetyAreaProcess();
  128. DelaySafetyAreaProcess();
  129. return;
  130. }
  131. SafetyAreaManager.Instance.DelayChangeStep(SafetyAreaStepEnum.PlayArea, 0.7f);
  132. }
  133. public void SetHeadPosition(Vector3 headPosition)
  134. {
  135. if (!canSetPlaneHeight) return;
  136. float largestHeight = headPosition.y - PlayAreaConstant.DEFAULT_HEIGHT_FROM_HEAD;// - API_Module_SDKSystem.GetSDKSystemHeight();
  137. if (largestHeight < planeHeight)
  138. {
  139. planeHeight = largestHeight;
  140. }
  141. }
  142. public void SetPlaneHeight(float interactionObjectHeight)
  143. {
  144. if (!canSetPlaneHeight)
  145. {
  146. return;
  147. }
  148. if (interactionObjectHeight < planeHeight)
  149. {
  150. planeHeight = interactionObjectHeight;
  151. }
  152. }
  153. public float GetPlaneHeight()
  154. {
  155. return planeHeight;//+ API_Module_SDKSystem.GetSDKSystemHeight();
  156. }
  157. public void ResetPlaneHeight()
  158. {
  159. planeHeight = 10000f;//float.MaxValue;
  160. }
  161. public void LoadPlaneHeight()
  162. {
  163. //ResetPlaneHeight();
  164. planeHeight = 0f;//-float.Parse(API_SDKGlobalConfiguration.GetString("Module_SafetyArea", "Height", "0"));//PlayerPrefs.GetFloat(PLANE_HEIGHT_SAVE_KEY, float.MaxValue);
  165. }
  166. public void SavePlaneHeight()
  167. {
  168. float originHeight = API_Module_SDKSystem.GetSDKSystemHeight();
  169. API_SDKGlobalConfiguration.SetConfiguration("Module_SafetyArea", "Height", (originHeight - planeHeight).ToString());
  170. API_SDKGlobalConfiguration.SaveAllConfigToFile();
  171. API_Module_SDKSystem.SetSDKSystemHeight(originHeight - planeHeight);
  172. //SDKGlobalConfiguration.getInstance.SaveConfigsToNative();
  173. //PlayerPrefs.SetFloat(PLANE_HEIGHT_SAVE_KEY, planeHeight);
  174. //PlayerPrefs.Save();
  175. }
  176. public override void OnUserCancel()
  177. {
  178. ExitSafetyAreaProcess();
  179. }
  180. }