GameSession.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. Debug.unityLogger.logEnabled = false;
  77. #endif
  78. UnityLog.Instance.Log(Application.persistentDataPath, 4);
  79. CameraEffect(false);
  80. if (!buildConfig && BuildConfigMgr.Instance.IsInit)
  81. {
  82. buildConfig = BuildConfig.Instance;
  83. }
  84. //print(UnityUtil.FindDepthTransf(transform, "GT26Dof (1)"));
  85. //TouchScreenKeyboardType
  86. base.Awake();
  87. //GameScreen.x = Screen.width;
  88. //GameScreen.y = Screen.height;
  89. Transform shadowSys = transform.Find(ARSDKName);
  90. Transform editor = transform.Find(PCCamera);
  91. //if (shadowSys)
  92. // shadowSys.gameObject.SetActive(false);
  93. //if (editor)
  94. // editor.gameObject.SetActive(false);
  95. #if UNITY_EDITOR && PLAYTYPE_PC
  96. if (editor)
  97. {
  98. editor.gameObject.SetActive(true);
  99. mainBody = editor.transform;
  100. mainCamera = editor.GetComponentInChildren<Camera>();
  101. eventCamera = mainCamera;
  102. if (shadowSys)
  103. shadowSys.gameObject.SetActive(false);
  104. gameHead = mainCamera.transform;
  105. }
  106. #else
  107. if (shadowSys)
  108. {
  109. shadowSys.gameObject.SetActive(true);
  110. mainBody = shadowSys.transform;
  111. mainCamera = OpenXRCamera.Instance.head.GetComponent<Camera>();
  112. eventCamera = OpenXRCamera.Instance.head.GetComponent<Camera>();
  113. gameHead = mainCamera.transform.parent;
  114. if (editor)
  115. editor.gameObject.SetActive(false);
  116. }
  117. #endif
  118. //gameHead = OpenXRCamera.Instance.head;
  119. InitSuccess = false;
  120. DontDestroyOnLoad(gameObject);
  121. StartCoroutine(CheckBaseInit());
  122. }
  123. /// <summary>
  124. /// 检测基础的配置是否存在
  125. /// 放在协程中处理,避免启动时某些原因卡死问题
  126. /// </summary>
  127. /// <returns></returns>
  128. private IEnumerator CheckBaseInit()
  129. {
  130. //SystemSettingMgr.Instance.InitSettings();
  131. ///初始化日志组件
  132. //UnityLog.Instance.InitLog();
  133. if (LanguageMgr.Instance.IsInit) ;
  134. if (ArtInfoMgr.Instance.IsInit) ;
  135. ResourcesManager.Instance.InitLoadType(buildConfig.assetsType);
  136. yield return 0;
  137. InitSuccess = true;
  138. #if PLAYTYPE_ARGLASS
  139. while (!SCInputModule.Instance)
  140. {
  141. yield return 0;
  142. }
  143. eventCamera = SCInputModule.Instance.RayCasterCamera;
  144. #endif
  145. #if UNITY_EDITOR
  146. UnityLog.Instance.logLevel = logLevel;
  147. #endif
  148. SessionInit?.Invoke();
  149. TimerMgr.Instance.CreateTimer(checkSVR, 1);
  150. }
  151. protected override void OnDestroy()
  152. {
  153. base.OnDestroy();
  154. UnityLog.Instance.ReleaseLog();
  155. }
  156. private void OnApplicationFocus(bool focus)
  157. {
  158. ///返回到游戏
  159. if (focus)
  160. {
  161. OnFocusGame?.Invoke();
  162. }
  163. ///离开游戏
  164. else
  165. {
  166. }
  167. }
  168. private void OnApplicationPause(bool pause)
  169. {
  170. ///暂停
  171. if (pause)
  172. {
  173. }
  174. ///继续
  175. else
  176. {
  177. OnFocusGame?.Invoke();
  178. }
  179. }
  180. public void ReSetBodyForward()
  181. {
  182. if (gameHead)
  183. {
  184. BodyForward = UnityUtil.RealForward(gameHead);
  185. }
  186. else
  187. {
  188. BodyForward = transform.forward;
  189. bodyForward.y = 0;
  190. }
  191. }
  192. private void LateUpdate()
  193. {
  194. if (gameHead)
  195. {
  196. Vector3 curForward = UnityUtil.RealForward(gameHead);
  197. if (Vector3.Angle(BodyForward, curForward) > headAngle)
  198. {
  199. BodyForward = Vector3.Lerp(BodyForward, curForward, Time.deltaTime * 6);
  200. }
  201. }
  202. #if UNITY_EDITOR
  203. if (logLevel != UnityLog.Instance.logLevel)
  204. {
  205. UnityLog.Instance.logLevel = logLevel;
  206. }
  207. #endif
  208. if (Math.Abs(Screen.width - GameScreen.x) > 1 || Math.Abs(Screen.height - GameScreen.y) > 1)
  209. {
  210. GameScreen.x = Screen.width;
  211. GameScreen.y = Screen.height;
  212. DeviceOrientation ori = Input.deviceOrientation;
  213. #if UNITY_EDITOR
  214. if (GameScreen.x > GameScreen.y)
  215. {
  216. deviceOrientation = DeviceOrientation.LandscapeLeft;
  217. }
  218. else
  219. {
  220. deviceOrientation = DeviceOrientation.Portrait;
  221. }
  222. ori = deviceOrientation;
  223. #endif
  224. ScreenChangeAction?.Invoke(ori, GameScreen);
  225. }
  226. }
  227. public Vector3 GetHeadForwadPos(float distance, bool isV = true)
  228. {
  229. Vector3 pos = Vector3.zero;
  230. if (gameHead)
  231. {
  232. Vector3 dir = gameHead.forward.normalized;
  233. if (isV)
  234. {
  235. dir.y = 0;
  236. }
  237. pos = gameHead.position + dir * distance;
  238. }
  239. return pos;
  240. }
  241. public Vector3 GetHeadForwad(int dir = 1)
  242. {
  243. Vector3 fowwad = transform.forward.normalized;
  244. if (gameHead)
  245. {
  246. fowwad = gameHead.forward.normalized * dir;
  247. fowwad.y = 0;
  248. return fowwad;
  249. }
  250. return fowwad;
  251. }
  252. /// <summary>
  253. /// 获取手的位置
  254. /// </summary>
  255. /// <param name="index"></param>
  256. /// <param name="position"></param>
  257. /// <returns></returns>
  258. public bool TryHandPosition(int index, out Vector3 position)
  259. {
  260. position = Vector3.zero;
  261. if (Module_InputSystem.instance && Module_InputSystem.instance.GetInputDeviceStatus(InputDeviceType.GGT26Dof))
  262. {
  263. InputDeviceHand hand = Module_InputSystem.instance.GetInputDevice<InputDeviceHand>(InputDeviceType.GGT26Dof);
  264. if (hand.inputDevicePartList[index].inputDataBase.isVaild)
  265. {
  266. position = ((InputDeviceGGT26DofPart)(hand.inputDevicePartList[index])).inputDeviceGGT26DofPartUI.modelGGT26Dof.fingerUI[(int)FINGER.forefinger].jointGameObject[(int)JOINT.Two].transform.position;
  267. if (Vector3.Distance(position, gameHead.position) < 0.003f)
  268. {
  269. return false;
  270. }
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. /// <summary>
  277. /// 获取左手坐标
  278. /// </summary>
  279. /// <param name="position"></param>
  280. /// <returns></returns>
  281. public bool TryHandLeftPosition(out Vector3 position, out Quaternion rotaion)
  282. {
  283. return TryHandPosition(0, out position, out rotaion);
  284. }
  285. /// <summary>
  286. /// 获取右手坐标
  287. /// </summary>
  288. /// <param name="position"></param>
  289. /// <returns></returns>
  290. public bool TryHandRightPosition(out Vector3 position, out Quaternion rotaion)
  291. {
  292. return TryHandPosition(1, out position, out rotaion);
  293. }
  294. private bool TryHandPosition(int index, out Vector3 position, out Quaternion rotaion)
  295. {
  296. position = Vector3.zero;
  297. rotaion = Quaternion.identity;
  298. var hand = index == 0 ? leftHand : rightHand;
  299. if (hand && hand.parent && hand.parent.gameObject.activeInHierarchy)
  300. {
  301. position = hand.position;
  302. rotaion = hand.rotation;
  303. return true;
  304. }
  305. return false;
  306. }
  307. /// <summary>
  308. /// 获取身体的坐标,相对于头部坐标减去0.5米
  309. /// 仅给射线使用
  310. /// </summary>
  311. public Vector3 BoadyPosition
  312. {
  313. get
  314. {
  315. Vector3 bodyPos = Vector3.zero;
  316. if (gameHead)
  317. {
  318. bodyPos = gameHead.position;
  319. bodyPos.y -= 0.5f;
  320. }
  321. return bodyPos;
  322. }
  323. }
  324. /// <summary>
  325. /// 身体的方向
  326. /// </summary>
  327. public Vector3 BodyForward
  328. {
  329. get
  330. {
  331. return bodyForward;
  332. }
  333. set
  334. {
  335. bodyForward = value;
  336. }
  337. }
  338. }
  339. }