123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- using BeinLab.Util;
- using SC.XR.Unity.Module_InputSystem;
- using SC.XR.Unity.Module_InputSystem.InputDeviceHand;
- using SC.XR.Unity.Module_InputSystem.InputDeviceHand.GGT26Dof;
- using ShadowStudio.Model;
- using System;
- using System.Collections;
- using System.Linq;
- using UnityEngine;
- namespace XRTool.Util
- {
- /// <summary>
- /// 游戏会话,场景配置,引用
- /// 获取发布设置相关
- /// 获取系统信息相关
- /// 此对象场景中必须存在,且不可删除
- /// </summary>
- public class GameSession : UnitySingleton<GameSession>
- {
- public BuildConfig buildConfig;
- public event Action SessionInit;
- /// <summary>
- /// 初始化完毕
- /// </summary>
- public static bool InitSuccess;
- [HideInInspector]
- public static Vector2 GameScreen = Vector2.zero;
- /// <summary>
- /// 是否是横屏
- /// </summary>
- public event Action<DeviceOrientation, Vector2> ScreenChangeAction;
- public bool isOutLogEditor = false;
- [HideInInspector]
- public static DeviceOrientation deviceOrientation = DeviceOrientation.LandscapeLeft;
- [HideInInspector]
- public Transform mainBody;
- public string ARSDKName = "ShadowSDK";
- public string PCCamera = "Main Camera";
- public Camera mainCamera;
- public Camera eventCamera;
- public static event Action OnFocusGame;
- public Transform leftHand;
- public Transform rightHand;
- [HideInInspector]
- public Transform gameHead;
- public EdgeCheckAnimation[] effectList;
- public float headAngle = 45f;
- private Vector3 bodyForward;
- #if UNITY_EDITOR
- [Range(0, 4)]
- public int logLevel = 1;
- #endif
- public void CameraEffect(bool active = true)
- {
- active = false;
- if (effectList != null)
- {
- for (int i = 0; i < effectList.Length; i++)
- {
- if (effectList[i])
- effectList[i].enabled = active;
- }
- }
- }
- private void checkSVR()
- {
- }
- /// <summary>
- /// 初始化完成的任务
- /// </summary>
- protected override void Awake()
- {
- base.Awake();
- Invoke("initSDK",0.1f);
- }
- void initSDK()
- {
- ReSetBodyForward();
- #if UNITY_ANDROID && !UNITY_EDITOR
- AndroidPermission.getInstant.GetPermission(true,true,true,true);
- #endif
- UnityLog.Instance.Log(Application.persistentDataPath, 4);
- CameraEffect(false);
- if (!buildConfig && BuildConfigMgr.Instance.IsInit)
- {
- buildConfig = BuildConfig.Instance;
- }
- //print(UnityUtil.FindDepthTransf(transform, "GT26Dof (1)"));
- //TouchScreenKeyboardType
- //GameScreen.x = Screen.width;
- //GameScreen.y = Screen.height;
- Transform shadowSys = transform.Find(ARSDKName);
- Transform editor = transform.Find(PCCamera);
- //if (shadowSys)
- // shadowSys.gameObject.SetActive(false);
- //if (editor)
- // editor.gameObject.SetActive(false);
- #if UNITY_EDITOR && PLAYTYPE_PC
- if (editor)
- {
- editor.gameObject.SetActive(true);
- mainBody = editor.transform;
- mainCamera = editor.GetComponentInChildren<Camera>();
- eventCamera = mainCamera;
- if (shadowSys)
- shadowSys.gameObject.SetActive(false);
- gameHead = mainCamera.transform;
- }
- #else
- if (shadowSys)
- {
- shadowSys.gameObject.SetActive(true);
- mainBody = shadowSys.transform;
- mainCamera = OpenXRCamera.Instance.head.GetComponent<Camera>();
- eventCamera = mainCamera;
- gameHead = mainCamera.transform.parent;
- if (editor)
- editor.gameObject.SetActive(false);
- }
- #endif
- InitSuccess = false;
- DontDestroyOnLoad(gameObject);
- StartCoroutine(CheckBaseInit());
- }
- /// <summary>
- /// 检测基础的配置是否存在
- /// 放在协程中处理,避免启动时某些原因卡死问题
- /// </summary>
- /// <returns></returns>
- private IEnumerator CheckBaseInit()
- {
- //SystemSettingMgr.Instance.InitSettings();
- ///初始化日志组件
- //UnityLog.Instance.InitLog();
- if (LanguageMgr.Instance.IsInit) ;
- if (ArtInfoMgr.Instance.IsInit) ;
- ResourcesManager.Instance.InitLoadType(buildConfig.assetsType);
- yield return 0;
- InitSuccess = true;
- #if PLAYTYPE_ARGLASS
- while (!SCInputModule.Instance)
- {
- yield return 0;
- }
- eventCamera = SCInputModule.Instance.RayCasterCamera;
- #endif
- #if UNITY_EDITOR
- UnityLog.Instance.logLevel = logLevel;
- #endif
- SessionInit?.Invoke();
- TimerMgr.Instance.CreateTimer(checkSVR, 1);
- }
- protected override void OnDestroy()
- {
- base.OnDestroy();
- UnityLog.Instance.ReleaseLog();
- }
- private void OnApplicationFocus(bool focus)
- {
- ///返回到游戏
- if (focus)
- {
- OnFocusGame?.Invoke();
- }
- ///离开游戏
- else
- {
- }
- }
- private void OnApplicationPause(bool pause)
- {
- ///暂停
- if (pause)
- {
- }
- ///继续
- else
- {
- OnFocusGame?.Invoke();
- }
- }
- public void ReSetBodyForward()
- {
- if (gameHead)
- {
- BodyForward = UnityUtil.RealForward(gameHead);
- }
- else
- {
- BodyForward = transform.forward;
- bodyForward.y = 0;
- }
- }
- private void LateUpdate()
- {
- if (gameHead)
- {
- Vector3 curForward = UnityUtil.RealForward(gameHead);
- if (Vector3.Angle(BodyForward, curForward) > headAngle)
- {
- BodyForward = Vector3.Lerp(BodyForward, curForward, Time.deltaTime * 6);
- }
- }
- #if UNITY_EDITOR
- if (logLevel != UnityLog.Instance.logLevel)
- {
- UnityLog.Instance.logLevel = logLevel;
- }
- #endif
- if (Math.Abs(Screen.width - GameScreen.x) > 1 || Math.Abs(Screen.height - GameScreen.y) > 1)
- {
- GameScreen.x = Screen.width;
- GameScreen.y = Screen.height;
- DeviceOrientation ori = Input.deviceOrientation;
- #if UNITY_EDITOR
- if (GameScreen.x > GameScreen.y)
- {
- deviceOrientation = DeviceOrientation.LandscapeLeft;
- }
- else
- {
- deviceOrientation = DeviceOrientation.Portrait;
- }
- ori = deviceOrientation;
- #endif
- ScreenChangeAction?.Invoke(ori, GameScreen);
- }
- }
- public Vector3 GetHeadForwadPos(float distance, bool isV = true)
- {
- Vector3 pos = Vector3.zero;
- if (gameHead)
- {
- Vector3 dir = gameHead.forward.normalized;
- if (isV)
- {
- dir.y = 0;
- }
- pos = gameHead.position + dir * distance;
- }
- return pos;
- }
- public Vector3 GetHeadForwad(int dir = 1)
- {
- Vector3 fowwad = transform.forward.normalized;
- if (gameHead)
- {
- fowwad = gameHead.forward.normalized * dir;
- fowwad.y = 0;
- return fowwad;
- }
- return fowwad;
- }
- /// <summary>
- /// 获取手的位置
- /// </summary>
- /// <param name="index"></param>
- /// <param name="position"></param>
- /// <returns></returns>
- public bool TryHandPosition(int index, out Vector3 position)
- {
- position = Vector3.zero;
- if (Module_InputSystem.instance && Module_InputSystem.instance.GetInputDeviceStatus(InputDeviceType.GGT26Dof))
- {
- InputDeviceHand hand = Module_InputSystem.instance.GetInputDevice<InputDeviceHand>(InputDeviceType.GGT26Dof);
- if (hand.inputDevicePartList[index].inputDataBase.isVaild)
- {
- position = ((InputDeviceGGT26DofPart)(hand.inputDevicePartList[index])).inputDeviceGGT26DofPartUI.modelGGT26Dof.fingerUI[(int)FINGER.forefinger].jointGameObject[(int)JOINT.Two].transform.position;
- if (Vector3.Distance(position, gameHead.position) < 0.003f)
- {
- return false;
- }
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 获取左手坐标
- /// </summary>
- /// <param name="position"></param>
- /// <returns></returns>
- public bool TryHandLeftPosition(out Vector3 position, out Quaternion rotaion)
- {
- return TryHandPosition(0, out position, out rotaion);
- }
- /// <summary>
- /// 获取右手坐标
- /// </summary>
- /// <param name="position"></param>
- /// <returns></returns>
- public bool TryHandRightPosition(out Vector3 position, out Quaternion rotaion)
- {
- return TryHandPosition(1, out position, out rotaion);
- }
- private bool TryHandPosition(int index, out Vector3 position, out Quaternion rotaion)
- {
- position = Vector3.zero;
- rotaion = Quaternion.identity;
- var hand = index == 0 ? leftHand : rightHand;
- if (hand && hand.parent && hand.parent.gameObject.activeInHierarchy)
- {
- position = hand.position;
- rotaion = hand.rotation;
- return true;
- }
- return false;
- }
- /// <summary>
- /// 获取身体的坐标,相对于头部坐标减去0.5米
- /// 仅给射线使用
- /// </summary>
- public Vector3 BoadyPosition
- {
- get
- {
- Vector3 bodyPos = Vector3.zero;
- if (gameHead)
- {
- bodyPos = gameHead.position;
- bodyPos.y -= 0.5f;
- }
- return bodyPos;
- }
- }
- /// <summary>
- /// 身体的方向
- /// </summary>
- public Vector3 BodyForward
- {
- get
- {
- return bodyForward;
- }
- set
- {
- bodyForward = value;
- }
- }
- }
- }
|