You've already forked taptap2024_GJ_chidouren
init
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using Framework.Utils.Extend;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Game.Component
|
||||
{
|
||||
public class GyroscopeEulerController : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] private Vector3 _minGravity;
|
||||
[SerializeField] private Vector3 _maxGravity;
|
||||
[SerializeField] private float _offset;
|
||||
|
||||
private Vector3 _curGravity;
|
||||
private Vector3 _baseRotation;
|
||||
private Vector3 _baseGravity;
|
||||
|
||||
void OnEnable ()
|
||||
{
|
||||
this._baseRotation = this.transform.localEulerAngles;
|
||||
this._baseGravity = GyroscopeMgr.Instance.Gyroscope.gravity;
|
||||
GyroscopeMgr.Instance.AddUpdater (DoUpdate);
|
||||
}
|
||||
|
||||
void DoUpdate (Vector3 gravity)
|
||||
{
|
||||
var gyroGravity = gravity - this._baseGravity;
|
||||
this._curGravity
|
||||
= Vector3.Slerp (this._curGravity , (gyroGravity * this._offset).Clamp (this._minGravity , this._maxGravity) , 0.25f);
|
||||
this.transform.localEulerAngles = this._baseRotation + new Vector3 (this._curGravity.y , -this._curGravity.x , 0);
|
||||
}
|
||||
|
||||
private void OnDisable ()
|
||||
{
|
||||
GyroscopeMgr.Instance.RemoveUpdater (DoUpdate);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c567b9a3c702d4a4290fda9201262c4b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Framework.Timer;
|
||||
using Framework.Utils.SingletonTemplate;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Game.Component
|
||||
{
|
||||
public class GyroscopeMgr : MgrMonoBase<GyroscopeMgr>
|
||||
{
|
||||
public Gyroscope Gyroscope => Input.gyro;
|
||||
|
||||
protected override void InitMgr ()
|
||||
{
|
||||
this._UpdateList = new List<Action<Vector3>> ();
|
||||
}
|
||||
|
||||
private void OnEnable ()
|
||||
{
|
||||
GameUpdateMgr.Instance.AddUpdater (DoUpdate);
|
||||
}
|
||||
|
||||
private void OnDisable ()
|
||||
{
|
||||
this._UpdateList?.Clear ();
|
||||
GameUpdateMgr.Instance.RemoveUpdater (DoUpdate);
|
||||
}
|
||||
|
||||
private void CheckEnable ()
|
||||
{
|
||||
if (this._UpdateList is { Count: > 0 } && !Input.gyro.enabled)
|
||||
{
|
||||
Input.gyro.enabled = true;
|
||||
Input.gyro.updateInterval = 0.1f;
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._UpdateList!.Count == 0 && Input.gyro.enabled)
|
||||
{
|
||||
Input.gyro.enabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private List<Action<Vector3>> _UpdateList;
|
||||
|
||||
private void _InvokeUpdate(List<Action<Vector3>> list)
|
||||
{
|
||||
if (list == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
for (int i = list.Count - 1; i >= 0; i--)
|
||||
{
|
||||
//在遍历时可能会出现外部操作list导致Count改变
|
||||
if (i >= list.Count)
|
||||
continue;
|
||||
if (list[i] == null)
|
||||
{
|
||||
list.RemoveAt(i);
|
||||
}
|
||||
else
|
||||
{
|
||||
list[i].Invoke(Input.gyro.gravity);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void DoUpdate() => this._InvokeUpdate(this._UpdateList);
|
||||
|
||||
public void AddUpdater(Action<Vector3> updater)
|
||||
{
|
||||
this._UpdateList.Add(updater);
|
||||
CheckEnable ();
|
||||
}
|
||||
|
||||
public void RemoveUpdater(Action<Vector3> updater)
|
||||
{
|
||||
this._UpdateList.Remove(updater);
|
||||
CheckEnable ();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fda42c789e884142ae13551ed0f3398f
|
||||
timeCreated: 1701243139
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using Framework.Utils.Extend;
|
||||
using Sirenix.OdinInspector;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Game.Component
|
||||
{
|
||||
public class GyroscopePositionController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Vector3 _minGravity;
|
||||
[SerializeField] private Vector3 _maxGravity;
|
||||
[SerializeField] private float _gravityOffset;
|
||||
[SerializeField, Range (0, 1)] private float _offset = 0.2f;
|
||||
[SerializeField] private bool _isFlip;
|
||||
[SerializeField] private bool _isDelay;
|
||||
[SerializeField, ShowIf ("_isDelay")] private float _delayTime = 0.75f;
|
||||
|
||||
private Vector3 _curGravity;
|
||||
private Vector3 _basePosition;
|
||||
private Vector3 _baseGravity;
|
||||
private bool _isUnlock;
|
||||
|
||||
void OnEnable ()
|
||||
{
|
||||
if (this._isDelay)
|
||||
{
|
||||
this._isUnlock = false;
|
||||
this.Invoke ("RefreshBasePosition" , this._delayTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
RefreshBasePosition ();
|
||||
}
|
||||
|
||||
GyroscopeMgr.Instance.AddUpdater (DoUpdate);
|
||||
}
|
||||
|
||||
private void RefreshBasePosition ()
|
||||
{
|
||||
this._basePosition = this.transform.localPosition;
|
||||
this._baseGravity = GyroscopeMgr.Instance.Gyroscope.gravity;
|
||||
this._isUnlock = true;
|
||||
}
|
||||
|
||||
void DoUpdate (Vector3 gravity)
|
||||
{
|
||||
if (!this._isUnlock)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var gyroGravity = (gravity - this._baseGravity) * (this._isFlip ? -1 : 1);
|
||||
this._curGravity
|
||||
= Vector3.Slerp (this._curGravity , (gyroGravity * this._gravityOffset).Clamp (this._minGravity , this._maxGravity) , 0.25f);
|
||||
|
||||
this.transform.localPosition = math.lerp (this.transform.localPosition ,
|
||||
this._basePosition + new Vector3 (this._curGravity.x , this._curGravity.y , 0) , this._offset);
|
||||
}
|
||||
|
||||
private void OnDisable ()
|
||||
{
|
||||
GyroscopeMgr.Instance.RemoveUpdater (DoUpdate);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a1b0ecafcaa14218a3df5c926d8bfef8
|
||||
timeCreated: 1696584635
|
||||
Reference in New Issue
Block a user