137 lines
4.5 KiB
C#
137 lines
4.5 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using BITKit;
|
||
|
using BITKit.UX;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.InputSystem;
|
||
|
using UnityEngine.InputSystem.Interactions;
|
||
|
using UnityEngine.UIElements;
|
||
|
|
||
|
namespace Net.Like.Xue.Tokyo.SNS.Demo
|
||
|
{
|
||
|
public class FlyCameraDemo : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField] private float initialSpeed;
|
||
|
[SerializeField] private float accelerationSpeed;
|
||
|
|
||
|
[SerializeField] private InputActionReference viewAction;
|
||
|
[SerializeField] private InputActionReference moveAction;
|
||
|
[SerializeField] private InputActionReference entryViewAction;
|
||
|
[SerializeField] private InputActionReference accelerateAction;
|
||
|
[SerializeField] private InputActionReference verticalAction;
|
||
|
|
||
|
private Vector2 _inputDirection;
|
||
|
private Vector2 _viewDirection;
|
||
|
private float _currentAccelerate;
|
||
|
private float _verticalFactor;
|
||
|
private bool _isViewing;
|
||
|
private bool _isAccelerated;
|
||
|
private Vector3 _currentVelocity;
|
||
|
|
||
|
[UXBindPath("sensitivity-slider")]
|
||
|
private Slider _sensitivitySlider;
|
||
|
[UXBindPath("open-document-button")]
|
||
|
private Button _openDocumentButton;
|
||
|
|
||
|
private readonly InputActionGroup _inputActionGroup = new()
|
||
|
{
|
||
|
allowGlobalActivation = false
|
||
|
};
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
destroyCancellationToken.Register(Dispose);
|
||
|
|
||
|
_inputActionGroup.RegisterCallback(viewAction, OnView);
|
||
|
_inputActionGroup.RegisterCallback(moveAction, OnMove);
|
||
|
_inputActionGroup.RegisterCallback(entryViewAction, OnEntryView);
|
||
|
_inputActionGroup.RegisterCallback(accelerateAction, OnAccelerate);
|
||
|
_inputActionGroup.RegisterCallback(verticalAction, OnVertical);
|
||
|
|
||
|
_inputActionGroup.allowInput.AddElement(this);
|
||
|
|
||
|
UXUtils.Inject(this);
|
||
|
|
||
|
//Quaternion.Euler(-_viewDirection.y,_viewDirection.x,0);
|
||
|
var direction = MathV.TransientRotationAxis(transform.eulerAngles);
|
||
|
_viewDirection.x = direction.y;
|
||
|
_viewDirection.y = -direction.x;
|
||
|
|
||
|
_openDocumentButton.clicked += () =>
|
||
|
{
|
||
|
Application.OpenURL("https://docs.qq.com/aio/DU3NsV3J4SnNNZHdV?p=l3kba61Nvtd9jKXAeQvnQV");
|
||
|
};
|
||
|
}
|
||
|
|
||
|
private void OnVertical(InputAction.CallbackContext obj)
|
||
|
{
|
||
|
_verticalFactor = obj.ReadValue<float>();
|
||
|
}
|
||
|
|
||
|
private void OnAccelerate(InputAction.CallbackContext obj)
|
||
|
{
|
||
|
_isAccelerated=obj switch
|
||
|
{
|
||
|
{interaction:PressInteraction,performed:true}=>true,
|
||
|
{interaction:PressInteraction,canceled:true}=>false,
|
||
|
_ => _isAccelerated,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
private void OnEntryView(InputAction.CallbackContext obj)
|
||
|
{
|
||
|
_isViewing = obj switch
|
||
|
{
|
||
|
{interaction:PressInteraction,performed:true}=>true,
|
||
|
{interaction:PressInteraction,canceled:true}=>false,
|
||
|
_ => _isViewing,
|
||
|
};
|
||
|
BITAppForUnity.AllowCursor.SetElements(0,_isViewing is false);
|
||
|
}
|
||
|
|
||
|
private void FixedUpdate()
|
||
|
{
|
||
|
var direction = _inputDirection;
|
||
|
var velocity = transform.rotation * new Vector3(direction.x, _verticalFactor, direction.y);
|
||
|
|
||
|
velocity *= initialSpeed * Time.deltaTime;
|
||
|
|
||
|
_currentAccelerate = _isAccelerated ? _currentAccelerate += accelerationSpeed * Time.fixedDeltaTime : 0;
|
||
|
|
||
|
velocity += velocity * _currentAccelerate;
|
||
|
|
||
|
_currentVelocity = Vector3.Lerp(_currentVelocity, velocity, 16 * Time.fixedDeltaTime);
|
||
|
|
||
|
transform.position += _currentVelocity;
|
||
|
}
|
||
|
|
||
|
private void LateUpdate()
|
||
|
{
|
||
|
if(_isViewing is false)return;
|
||
|
transform.rotation = Quaternion.Euler(-_viewDirection.y,_viewDirection.x,0);
|
||
|
}
|
||
|
|
||
|
private void OnMove(InputAction.CallbackContext obj)
|
||
|
{
|
||
|
_inputDirection = obj.ReadValue<Vector2>();
|
||
|
}
|
||
|
|
||
|
private void OnView(InputAction.CallbackContext obj)
|
||
|
{
|
||
|
if(_isViewing is false)return;
|
||
|
var value = obj.ReadValue<Vector2>() * 0.018f * _sensitivitySlider.value;
|
||
|
_viewDirection.x += value.x;
|
||
|
_viewDirection.y += value.y;
|
||
|
|
||
|
_viewDirection.y = Mathf.Clamp(_viewDirection.y, -80, 80);
|
||
|
}
|
||
|
|
||
|
private void Dispose()
|
||
|
{
|
||
|
_inputActionGroup.Dispose();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|