GameSession.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using BeinLab.Util;
  2. using SC.XR.Unity.Module_InputSystem;
  3. using SC.XR.Unity.Module_InputSystem.InputDeviceHand;
  4. using SC.XR.Unity.Module_InputSystem.InputDeviceHand.GGT26Dof;
  5. using ShadowStudio.Model;
  6. using System;
  7. using System.Collections;
  8. using System.Linq;
  9. using UnityEngine;
  10. namespace XRTool.Util
  11. {
  12. /// <summary>
  13. /// 游戏会话,场景配置,引用
  14. /// 获取发布设置相关
  15. /// 获取系统信息相关
  16. /// 此对象场景中必须存在,且不可删除
  17. /// </summary>
  18. public class GameSession : UnitySingleton<GameSession>
  19. {
  20. public BuildConfig buildConfig;
  21. public event Action SessionInit;
  22. /// <summary>
  23. /// 初始化完毕
  24. /// </summary>
  25. public static bool InitSuccess;
  26. [HideInInspector]
  27. public static Vector2 GameScreen = Vector2.zero;
  28. /// <summary>
  29. /// 是否是横屏
  30. /// </summary>
  31. public event Action<DeviceOrientation, Vector2> ScreenChangeAction;
  32. public bool isOutLogEditor = false;
  33. [HideInInspector]
  34. public static DeviceOrientation deviceOrientation = DeviceOrientation.LandscapeLeft;
  35. [HideInInspector]
  36. public Transform mainBody;
  37. public string ARSDKName = "ShadowSDK";
  38. public string PCCamera = "Main Camera";
  39. public Camera mainCamera;
  40. public Camera eventCamera;
  41. public static event Action OnFocusGame;
  42. public Transform leftHand;
  43. public Transform rightHand;
  44. [HideInInspector]
  45. public Transform gameHead;
  46. public EdgeCheckAnimation[] effectList;
  47. public float headAngle = 45f;
  48. private Vector3 bodyForward;
  49. #if UNITY_EDITOR
  50. [Range(0, 4)]
  51. public int logLevel = 1;
  52. #endif
  53. public void CameraEffect(bool active = true)
  54. {
  55. active = false;
  56. if (effectList != null)
  57. {
  58. for (int i = 0; i < effectList.Length; i++)
  59. {
  60. if (effectList[i])
  61. effectList[i].enabled = active;
  62. }
  63. }
  64. }
  65. private void checkSVR()
  66. {
  67. }
  68. /// <summary>
  69. /// 初始化完成的任务
  70. /// </summary>
  71. protected override void Awake()
  72. {
  73. ReSetBodyForward();
  74. #if UNITY_ANDROID && !UNITY_EDITOR
  75. AndroidPermission.getInstant.GetPermission(true,true,true,true);
  76. #endif
  77. UnityLog.Instance.Log(Application.persistentDataPath, 4);
  78. CameraEffect(false);
  79. if (!buildConfig && BuildConfigMgr.Instance.IsInit)
  80. {
  81. buildConfig = BuildConfig.Instance;
  82. }
  83. //print(UnityUtil.FindDepthTransf(transform, "GT26Dof (1)"));
  84. //TouchScreenKeyboardType
  85. base.Awake();
  86. //GameScreen.x = Screen.width;
  87. //GameScreen.y = Screen.height;
  88. Transform shadowSys = transform.Find(ARSDKName);
  89. Transform editor = transform.Find(PCCamera);
  90. //if (shadowSys)
  91. // shadowSys.gameObject.SetActive(false);
  92. //if (editor)
  93. // editor.gameObject.SetActive(false);
  94. #if UNITY_EDITOR && PLAYTYPE_PC
  95. if (editor)
  96. {
  97. editor.gameObject.SetActive(true);
  98. mainBody = editor.transform;
  99. mainCamera = editor.GetComponentInChildren<Camera>();
  100. eventCamera = mainCamera;
  101. if (shadowSys)
  102. shadowSys.gameObject.SetActive(false);
  103. gameHead = mainCamera.transform;
  104. }
  105. #else
  106. if (shadowSys)
  107. {
  108. shadowSys.gameObject.SetActive(true);
  109. mainBody = shadowSys.transform;
  110. mainCamera = OpenXRCamera.Instance.head.GetComponent<Camera>();
  111. eventCamera = OpenXRCamera.Instance.head.GetComponent<Camera>();
  112. gameHead = mainCamera.transform.parent;
  113. if (editor)
  114. editor.gameObject.SetActive(false);
  115. }
  116. #endif
  117. InitSuccess = false;
  118. DontDestroyOnLoad(gameObject);
  119. StartCoroutine(CheckBaseInit());
  120. }
  121. /// <summary>
  122. /// 检测基础的配置是否存在
  123. /// 放在协程中处理,避免启动时某些原因卡死问题
  124. /// </summary>
  125. /// <returns></returns>
  126. private IEnumerator CheckBaseInit()
  127. {
  128. //SystemSettingMgr.Instance.InitSettings();
  129. ///初始化日志组件
  130. //UnityLog.Instance.InitLog();
  131. if (LanguageMgr.Instance.IsInit) ;
  132. if (ArtInfoMgr.Instance.IsInit) ;
  133. ResourcesManager.Instance.InitLoadType(buildConfig.assetsType);
  134. yield return 0;
  135. InitSuccess = true;
  136. #if PLAYTYPE_ARGLASS
  137. while (!SCInputModule.Instance)
  138. {
  139. yield return 0;
  140. }
  141. eventCamera = SCInputModule.Instance.RayCasterCamera;
  142. #endif
  143. #if UNITY_EDITOR
  144. UnityLog.Instance.logLevel = logLevel;
  145. #endif
  146. SessionInit?.Invoke();
  147. TimerMgr.Instance.CreateTimer(checkSVR, 1);
  148. }
  149. protected override void OnDestroy()
  150. {
  151. base.OnDestroy();
  152. UnityLog.Instance.ReleaseLog();
  153. }
  154. private void OnApplicationFocus(bool focus)
  155. {
  156. ///返回到游戏
  157. if (focus)
  158. {
  159. OnFocusGame?.Invoke();
  160. }
  161. ///离开游戏
  162. else
  163. {
  164. }
  165. }
  166. private void OnApplicationPause(bool pause)
  167. {
  168. ///暂停
  169. if (pause)
  170. {
  171. }
  172. ///继续
  173. else
  174. {
  175. OnFocusGame?.Invoke();
  176. }
  177. }
  178. public void ReSetBodyForward()
  179. {
  180. if (gameHead)
  181. {
  182. BodyForward = UnityUtil.RealForward(gameHead);
  183. }
  184. else
  185. {
  186. BodyForward = transform.forward;
  187. bodyForward.y = 0;
  188. }
  189. }
  190. private void LateUpdate()
  191. {
  192. if (gameHead)
  193. {
  194. Vector3 curForward = UnityUtil.RealForward(gameHead);
  195. if (Vector3.Angle(BodyForward, curForward) > headAngle)
  196. {
  197. BodyForward = Vector3.Lerp(BodyForward, curForward, Time.deltaTime * 6);
  198. }
  199. }
  200. #if UNITY_EDITOR
  201. if (logLevel != UnityLog.Instance.logLevel)
  202. {
  203. UnityLog.Instance.logLevel = logLevel;
  204. }
  205. #endif
  206. if (Math.Abs(Screen.width - GameScreen.x) > 1 || Math.Abs(Screen.height - GameScreen.y) > 1)
  207. {
  208. GameScreen.x = Screen.width;
  209. GameScreen.y = Screen.height;
  210. DeviceOrientation ori = Input.deviceOrientation;
  211. #if UNITY_EDITOR
  212. if (GameScreen.x > GameScreen.y)
  213. {
  214. deviceOrientation = DeviceOrientation.LandscapeLeft;
  215. }
  216. else
  217. {
  218. deviceOrientation = DeviceOrientation.Portrait;
  219. }
  220. ori = deviceOrientation;
  221. #endif
  222. ScreenChangeAction?.Invoke(ori, GameScreen);
  223. }
  224. }
  225. public Vector3 GetHeadForwadPos(float distance, bool isV = true)
  226. {
  227. Vector3 pos = Vector3.zero;
  228. if (gameHead)
  229. {
  230. Vector3 dir = gameHead.forward.normalized;
  231. if (isV)
  232. {
  233. dir.y = 0;
  234. }
  235. pos = gameHead.position + dir * distance;
  236. }
  237. return pos;
  238. }
  239. public Vector3 GetHeadForwad(int dir = 1)
  240. {
  241. Vector3 fowwad = transform.forward.normalized;
  242. if (gameHead)
  243. {
  244. fowwad = gameHead.forward.normalized * dir;
  245. fowwad.y = 0;
  246. return fowwad;
  247. }
  248. return fowwad;
  249. }
  250. /// <summary>
  251. /// 获取手的位置
  252. /// </summary>
  253. /// <param name="index"></param>
  254. /// <param name="position"></param>
  255. /// <returns></returns>
  256. public bool TryHandPosition(int index, out Vector3 position)
  257. {
  258. position = Vector3.zero;
  259. if (Module_InputSystem.instance && Module_InputSystem.instance.GetInputDeviceStatus(InputDeviceType.GGT26Dof))
  260. {
  261. InputDeviceHand hand = Module_InputSystem.instance.GetInputDevice<InputDeviceHand>(InputDeviceType.GGT26Dof);
  262. if (hand.inputDevicePartList[index].inputDataBase.isVaild)
  263. {
  264. position = ((InputDeviceGGT26DofPart)(hand.inputDevicePartList[index])).inputDeviceGGT26DofPartUI.modelGGT26Dof.fingerUI[(int)FINGER.forefinger].jointGameObject[(int)JOINT.Two].transform.position;
  265. if (Vector3.Distance(position, gameHead.position) < 0.003f)
  266. {
  267. return false;
  268. }
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274. /// <summary>
  275. /// 获取左手坐标
  276. /// </summary>
  277. /// <param name="position"></param>
  278. /// <returns></returns>
  279. public bool TryHandLeftPosition(out Vector3 position, out Quaternion rotaion)
  280. {
  281. return TryHandPosition(0, out position, out rotaion);
  282. }
  283. /// <summary>
  284. /// 获取右手坐标
  285. /// </summary>
  286. /// <param name="position"></param>
  287. /// <returns></returns>
  288. public bool TryHandRightPosition(out Vector3 position, out Quaternion rotaion)
  289. {
  290. return TryHandPosition(1, out position, out rotaion);
  291. }
  292. private bool TryHandPosition(int index, out Vector3 position, out Quaternion rotaion)
  293. {
  294. position = Vector3.zero;
  295. rotaion = Quaternion.identity;
  296. var hand = index == 0 ? leftHand : rightHand;
  297. if (hand && hand.parent && hand.parent.gameObject.activeInHierarchy)
  298. {
  299. position = hand.position;
  300. rotaion = hand.rotation;
  301. return true;
  302. }
  303. return false;
  304. }
  305. /// <summary>
  306. /// 获取身体的坐标,相对于头部坐标减去0.5米
  307. /// 仅给射线使用
  308. /// </summary>
  309. public Vector3 BoadyPosition
  310. {
  311. get
  312. {
  313. Vector3 bodyPos = Vector3.zero;
  314. if (gameHead)
  315. {
  316. bodyPos = gameHead.position;
  317. bodyPos.y -= 0.5f;
  318. }
  319. return bodyPos;
  320. }
  321. }
  322. /// <summary>
  323. /// 身体的方向
  324. /// </summary>
  325. public Vector3 BodyForward
  326. {
  327. get
  328. {
  329. return bodyForward;
  330. }
  331. set
  332. {
  333. bodyForward = value;
  334. }
  335. }
  336. }
  337. }