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
{
///
/// 游戏会话,场景配置,引用
/// 获取发布设置相关
/// 获取系统信息相关
/// 此对象场景中必须存在,且不可删除
///
public class GameSession : UnitySingleton
{
public BuildConfig buildConfig;
public event Action SessionInit;
///
/// 初始化完毕
///
public static bool InitSuccess;
[HideInInspector]
public static Vector2 GameScreen = Vector2.zero;
///
/// 是否是横屏
///
public event Action 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()
{
}
///
/// 初始化完成的任务
///
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();
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();
eventCamera = mainCamera;
gameHead = mainCamera.transform.parent;
if (editor)
editor.gameObject.SetActive(false);
}
#endif
InitSuccess = false;
DontDestroyOnLoad(gameObject);
StartCoroutine(CheckBaseInit());
}
///
/// 检测基础的配置是否存在
/// 放在协程中处理,避免启动时某些原因卡死问题
///
///
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;
}
///
/// 获取手的位置
///
///
///
///
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(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;
}
///
/// 获取左手坐标
///
///
///
public bool TryHandLeftPosition(out Vector3 position, out Quaternion rotaion)
{
return TryHandPosition(0, out position, out rotaion);
}
///
/// 获取右手坐标
///
///
///
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;
}
///
/// 获取身体的坐标,相对于头部坐标减去0.5米
/// 仅给射线使用
///
public Vector3 BoadyPosition
{
get
{
Vector3 bodyPos = Vector3.zero;
if (gameHead)
{
bodyPos = gameHead.position;
bodyPos.y -= 0.5f;
}
return bodyPos;
}
}
///
/// 身体的方向
///
public Vector3 BodyForward
{
get
{
return bodyForward;
}
set
{
bodyForward = value;
}
}
}
}