GameSession.cs 12 KB

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