GameSession.cs 11 KB

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