1
This commit is contained in:
@@ -3,19 +3,20 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class ShowProfiler : BITBehavior, IDiagnostics
|
||||
public class ShowProfiler : BITBehavior
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SubclassSelector, SerializeReference] public References pingAddress;
|
||||
[Header(Constant.Header.Output)]
|
||||
public Provider fpsOutput;
|
||||
public Provider pingOutput;
|
||||
DeltaTimer timer = new();
|
||||
Ping ping;
|
||||
[SerializeField,SerializeReference,SubclassSelector] private IProvider fpsOutput;
|
||||
[SerializeField,SerializeReference,SubclassSelector] private IProvider pingOutput;
|
||||
[SerializeField,SerializeReference,SubclassSelector] private IProvider resolutionOutput;
|
||||
[SerializeField,SerializeReference,SubclassSelector] private IProvider frameRateOutput;
|
||||
private readonly DeltaTimer timer = new();
|
||||
private Ping ping;
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
public int frameRate;
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
private int frameRate;
|
||||
private void Update()
|
||||
{
|
||||
timer.Update();
|
||||
frameRate = timer;
|
||||
@@ -26,20 +27,14 @@ namespace BITKit
|
||||
{
|
||||
pingOutput.Set(ping.time.ToString());
|
||||
}
|
||||
ping = new(pingAddress);
|
||||
ping = new Ping(pingAddress);
|
||||
}
|
||||
resolutionOutput?.Set(Screen.currentResolution.ToString());
|
||||
frameRateOutput?.Set(Application.targetFrameRate is -1 or 0 ? "Unlimited" : Application.targetFrameRate.ToString());
|
||||
}
|
||||
public override string GetName()
|
||||
{
|
||||
return nameof(ShowProfiler);
|
||||
}
|
||||
public override object GetDiagnostics()
|
||||
{
|
||||
Dictionary<string, string> dictioanry = new();
|
||||
dictioanry.Add(nameof(fpsOutput), fpsOutput ? "有效" : "未定义");
|
||||
dictioanry.Add(nameof(pingOutput), pingOutput ? "有效" : "未定义");
|
||||
dictioanry.Add("FPS", timer);
|
||||
return dictioanry;
|
||||
}
|
||||
}
|
||||
}
|
@@ -6,8 +6,8 @@ namespace BITKit
|
||||
{
|
||||
public class ShowVersion : MonoBehaviour
|
||||
{
|
||||
public Provider output;
|
||||
void Start()
|
||||
[SerializeField,SerializeReference,SubclassSelector] private IProvider output;
|
||||
private void Start()
|
||||
{
|
||||
output?.Set(Application.version);
|
||||
}
|
||||
|
@@ -1,360 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using Cinemachine;
|
||||
using Cinemachine.Utility;
|
||||
using TouchPhase = UnityEngine.InputSystem.TouchPhase;
|
||||
using UnityEngine.InputSystem.EnhancedTouch;
|
||||
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
|
||||
using BITKit.IO;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using Cysharp.Threading.Tasks;
|
||||
namespace BITKit
|
||||
{
|
||||
public class SimpleCameraLook : MonoBehaviour
|
||||
{
|
||||
private string fileName => Path.Combine(Utility.Path.persistentDataPath, gameObject.scene.name, $"{gameObject.GetInstanceID()}.bit");
|
||||
public enum State
|
||||
{
|
||||
Mouse,
|
||||
Touchscreen,
|
||||
}
|
||||
[Header(Constant.Header.Gameobjects)]
|
||||
public Transform cameraRoot;
|
||||
public CinemachineVirtualCamera virtualCamera;
|
||||
[Header(Constant.Header.Settings)]
|
||||
public TransformMode touchMode;
|
||||
[Header(Constant.Header.Settings)]
|
||||
public InputActionReference viewAction;
|
||||
public InputActionReference movementAction;
|
||||
public InputActionReference adsAction;
|
||||
public InputActionReference entryAction;
|
||||
public InputActionReference entryMoveAction;
|
||||
public InputActionGroup inputGroup = new();
|
||||
[Header(Constant.Header.Settings)]
|
||||
public float minEntrySqrMagnitude = 1;
|
||||
public LayerMask layerMask;
|
||||
[Header(Constant.Header.Settings)]
|
||||
public AnimationCurve scollCurve = new();
|
||||
public AnimationCurve moveCurve = new();
|
||||
public AnimationCurve touchMoveCurve = new();
|
||||
public Vector2 cameraDistanceLimit = new(1, 64);
|
||||
[Header(Constant.Header.Debug)]
|
||||
public int touchesCount;
|
||||
public bool isParallelVector;
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
public Vector3 lookInput;
|
||||
public Vector3 moveVector;
|
||||
public ValidHandle freeLooking = new();
|
||||
public ValidHandle moving = new();
|
||||
private Cinemachine3rdPersonFollow tpv;
|
||||
private Location startLocation;
|
||||
private Location savedLocation;
|
||||
private float savedDistance;
|
||||
private float startDistance;
|
||||
private CinemachineBrain _brain;
|
||||
public void Reset()
|
||||
{
|
||||
lookInput = MathV.TransientRotationAxis(startLocation.position);
|
||||
cameraRoot.position = startLocation;
|
||||
tpv.CameraDistance = startDistance;
|
||||
}
|
||||
public void DisableInput(bool disable)
|
||||
{
|
||||
inputGroup.allowInput.SetDisableElements(nameof(DisableInput), disable);
|
||||
}
|
||||
public void SetTouchMode(int index)
|
||||
{
|
||||
SetTouchMode((TransformMode)index);
|
||||
}
|
||||
public void SetTouchMode(TransformMode mode)
|
||||
{
|
||||
touchMode = mode;
|
||||
}
|
||||
public void Align(Transform target)
|
||||
{
|
||||
if (target.TryGetComponent<SimpleCameraLook>(out var x))
|
||||
{
|
||||
cameraRoot.position = x.cameraRoot.position;
|
||||
//lookInput = x.lookInput;
|
||||
lookInput = MathV.TransientRotationAxis(x.cameraRoot.rotation.eulerAngles);
|
||||
if (x.tpv)
|
||||
tpv.CameraDistance = x.tpv.CameraDistance;
|
||||
}
|
||||
else if (target.TryGetComponent<CinemachineVirtualCamera>(out var vCam))
|
||||
{
|
||||
cameraRoot.position = vCam.Follow.position;
|
||||
lookInput = MathV.TransientRotationAxis(vCam.LookAt.rotation.eulerAngles);
|
||||
var e = vCam.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
|
||||
if (e)
|
||||
{
|
||||
tpv.CameraDistance = e.CameraDistance;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
cameraRoot.position = target.position;
|
||||
lookInput = MathV.TransientRotationAxis(target.eulerAngles);
|
||||
}
|
||||
}
|
||||
public void SaveLocation()
|
||||
{
|
||||
savedLocation = new(cameraRoot);
|
||||
savedDistance = tpv.CameraDistance;
|
||||
BITAssets.Build(
|
||||
PathHelper.GetFilePath(fileName),
|
||||
new IAsset[]
|
||||
{
|
||||
new BITAsset(nameof(savedLocation),JsonUtility.ToJson(savedLocation)),
|
||||
new BITAsset(nameof(savedDistance),JsonConvert.SerializeObject(savedDistance)),
|
||||
}
|
||||
);
|
||||
}
|
||||
public void LoadLocation()
|
||||
{
|
||||
lookInput = MathV.TransientRotationAxis(savedLocation.rotation.eulerAngles);
|
||||
cameraRoot.position = savedLocation;
|
||||
tpv.CameraDistance = savedDistance;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
freeLooking.AddListener(OnFreeLook);
|
||||
tpv = virtualCamera.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
|
||||
|
||||
savedLocation = startLocation = new(cameraRoot);
|
||||
savedDistance = startDistance = tpv.CameraDistance;
|
||||
|
||||
inputGroup.RegisterCallback(viewAction, OnView);
|
||||
inputGroup.RegisterCallback(movementAction, OnMovement);
|
||||
inputGroup.RegisterCallback(adsAction, OnAds);
|
||||
inputGroup.RegisterCallback(entryAction, OnEntry);
|
||||
inputGroup.RegisterCallback(entryMoveAction, OnEntryMoveAction);
|
||||
|
||||
if (Camera.main != null) _brain = Camera.main.GetComponent<CinemachineBrain>();
|
||||
}
|
||||
|
||||
private async void Start()
|
||||
{
|
||||
var fileName = this.fileName;
|
||||
await UniTask.SwitchToThreadPool();
|
||||
if (File.Exists(fileName))
|
||||
{
|
||||
savedLocation = BITAssets.Read<Location>(fileName, nameof(savedLocation));
|
||||
savedDistance = BITAssets.Read<float>(fileName, nameof(savedDistance));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
SetEnabled(true);
|
||||
lookInput = MathV.TransientRotationAxis(cameraRoot.eulerAngles);
|
||||
|
||||
freeLooking.RemoveDisableElements(this);
|
||||
|
||||
virtualCamera.enabled = true;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
SetEnabled(false);
|
||||
freeLooking.AddDisableElements(this);
|
||||
|
||||
virtualCamera.enabled = false;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
inputGroup.Dispose();
|
||||
}
|
||||
|
||||
private void SetEnabled(bool enabled)
|
||||
{
|
||||
//BIT4Log.Log<SimpleCameraLook>($"正在设置输入状态:{enabled}");
|
||||
BITAppForUnity.AllowTouchSupport.SetElements(this, enabled);
|
||||
inputGroup.allowInput.SetElements(this, enabled);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
//var debuger = DI.Get<IDebuger>();
|
||||
var playerConfig = PlayerConfig.singleton;
|
||||
var sensitivity = playerConfig.touchSensitivity;
|
||||
|
||||
//debuger.Log(nameof(Touch.activeTouches), Touch.activeTouches.Count.ToString());
|
||||
touchesCount = Touch.activeTouches.Count;
|
||||
|
||||
if (Mouse.current.leftButton.wasPressedThisFrame)
|
||||
{
|
||||
var ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
|
||||
if (Physics.Raycast(ray, out var raycast, 256, layerMask))
|
||||
{
|
||||
cameraRoot.position = raycast.point;
|
||||
}
|
||||
}
|
||||
|
||||
if (_brain)
|
||||
{
|
||||
inputGroup.allowInput.SetDisableElements(nameof(CinemachineBrain),virtualCamera != (CinemachineVirtualCamera)_brain.ActiveVirtualCamera);
|
||||
}
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
lookInput.x = Mathf.Clamp(lookInput.x, -80, 80);
|
||||
var newRotation = Quaternion.Euler(lookInput);
|
||||
cameraRoot.rotation = Quaternion.Lerp(cameraRoot.rotation, newRotation, 48 * Time.deltaTime);
|
||||
var offset = cameraRoot.localRotation * moveVector;
|
||||
cameraRoot.localPosition -= offset * Time.deltaTime;
|
||||
}
|
||||
|
||||
private void OnView(InputAction.CallbackContext context)
|
||||
{
|
||||
var playerConfig = PlayerConfig.singleton;
|
||||
var sensitivity = playerConfig.sensitivity * playerConfig.m_yaw;
|
||||
var delta = context.ReadValue<Vector2>();
|
||||
switch (context.control.device)
|
||||
{
|
||||
case Mouse mouse:
|
||||
if (freeLooking && !moving)
|
||||
{
|
||||
lookInput.x -= delta.y * sensitivity;
|
||||
lookInput.y += delta.x * sensitivity;
|
||||
}
|
||||
break;
|
||||
case Touchscreen touchscreen:
|
||||
/* lookInput.x -= delta.y * sensitivity;
|
||||
lookInput.y += delta.x * sensitivity; */
|
||||
switch (touchMode)
|
||||
{
|
||||
case TransformMode.Move:
|
||||
break;
|
||||
case TransformMode.Rotate:
|
||||
lookInput.x -= delta.y * sensitivity;
|
||||
lookInput.y += delta.x * sensitivity;
|
||||
moveVector = default;
|
||||
break;
|
||||
case TransformMode.Scale:
|
||||
break;
|
||||
}
|
||||
if (touchMode is TransformMode.Scale)
|
||||
{
|
||||
OnAds(context);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAds(InputAction.CallbackContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
BITAppForUnity.ThrowIfWindowNotFocus();
|
||||
switch (context.control.device)
|
||||
{
|
||||
case Mouse mouse:
|
||||
OnAds(context.ReadValue<Vector2>().y);
|
||||
break;
|
||||
case Touchscreen touchscreen:
|
||||
/* lookInput.x -= delta.y * sensitivity;
|
||||
lookInput.y += delta.x * sensitivity; */
|
||||
switch (touchMode)
|
||||
{
|
||||
case TransformMode.Move:
|
||||
break;
|
||||
case TransformMode.Rotate:
|
||||
moveVector = default;
|
||||
break;
|
||||
case TransformMode.Scale:
|
||||
OnAds(context.ReadValue<Vector2>().y);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (MouseNotOverGameViewException)
|
||||
{
|
||||
|
||||
}
|
||||
catch (System.Exception)
|
||||
{
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnAds(float delta)
|
||||
{
|
||||
var newDistance = tpv.CameraDistance -
|
||||
delta * Time.deltaTime * scollCurve.Evaluate(tpv.CameraDistance);
|
||||
tpv.CameraDistance = Mathf.Clamp(newDistance, cameraDistanceLimit.x, cameraDistanceLimit.y);
|
||||
}
|
||||
|
||||
private void OnFreeLook(bool active)
|
||||
{
|
||||
BITAppForUnity.AllowCursor.SetDisableElements(this, active);
|
||||
}
|
||||
|
||||
private void OnEntry(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.started)
|
||||
{
|
||||
var delta = viewAction.action.ReadValue<Vector2>();
|
||||
if (delta.sqrMagnitude > minEntrySqrMagnitude)
|
||||
freeLooking.AddElement(this);
|
||||
}
|
||||
else if (context.canceled)
|
||||
{
|
||||
freeLooking.RemoveElement(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMovement(InputAction.CallbackContext context)
|
||||
{
|
||||
switch (context.control.device)
|
||||
{
|
||||
case Mouse mouse:
|
||||
if (moving && freeLooking)
|
||||
{
|
||||
moveVector = context.ReadValue<Vector2>() * moveCurve.Evaluate(tpv.CameraDistance);
|
||||
}
|
||||
break;
|
||||
case Touchscreen touchscreen:
|
||||
/* lookInput.x -= delta.y * sensitivity;
|
||||
lookInput.y += delta.x * sensitivity; */
|
||||
switch (touchMode)
|
||||
{
|
||||
case TransformMode.Move:
|
||||
moveVector = context.ReadValue<Vector2>() * moveCurve.Evaluate(tpv.CameraDistance);
|
||||
break;
|
||||
case TransformMode.Rotate:
|
||||
break;
|
||||
case TransformMode.Scale:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (context.canceled)
|
||||
{
|
||||
moveVector = default;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEntryMoveAction(InputAction.CallbackContext context)
|
||||
{
|
||||
|
||||
if (context.started)
|
||||
{
|
||||
moving.AddElement(this);
|
||||
}
|
||||
else if (context.canceled)
|
||||
{
|
||||
moving.RemoveElement(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 462eee3907e22aa4bb427eca250554bc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -9,20 +9,32 @@ namespace BITKit
|
||||
public class WorldToScreenPoint : MonoBehaviour
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeField] Transform root;
|
||||
[SerializeField] Vector3 offset;
|
||||
[SerializeField] Optional<float> activeDistance;
|
||||
[SerializeField] private Transform root;
|
||||
[SerializeField] private Optional<Vector3> localOffset;
|
||||
[SerializeField] private Optional<Vector3> worldOffset;
|
||||
[SerializeField] private Optional<float> activeDistance;
|
||||
|
||||
[Header(Constant.Header.Events)]
|
||||
[SerializeField] private UnityEvent<bool> setHeaderEnabled;
|
||||
[SerializeField] private UnityEvent<Vector3> setHeaderPosition;
|
||||
Transform cameraTrans;
|
||||
void Start()
|
||||
|
||||
private Transform cameraTrans;
|
||||
private void Start()
|
||||
{
|
||||
if (Camera.main != null) cameraTrans = Camera.main.transform;
|
||||
}
|
||||
void Update()
|
||||
private void Update()
|
||||
{
|
||||
var position = root.position + root.rotation * offset;
|
||||
var position = root.position;
|
||||
if (localOffset.Allow)
|
||||
{
|
||||
position += root.rotation * localOffset;
|
||||
}
|
||||
|
||||
if (worldOffset.Allow)
|
||||
{
|
||||
position+=worldOffset;
|
||||
}
|
||||
|
||||
setHeaderPosition.Invoke(position);
|
||||
|
||||
|
Reference in New Issue
Block a user