架构大更
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Lofelt.NiceVibrations
|
||||
{
|
||||
public class DemoManager : MonoBehaviour
|
||||
{
|
||||
[Header("Demo")]
|
||||
public AudioSource DebugAudioEmphasis;
|
||||
[Header("Demo")] public AudioSource DebugAudioEmphasis;
|
||||
|
||||
public AudioSource DebugAudioContinuous;
|
||||
public MMUIShaker Logo;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace Lofelt.NiceVibrations
|
||||
{
|
||||
@@ -11,39 +9,43 @@ namespace Lofelt.NiceVibrations
|
||||
{
|
||||
public float Amplitude;
|
||||
public float Frequency;
|
||||
public bool Shaking = false;
|
||||
public bool Shaking;
|
||||
|
||||
protected Vector3 _initialPosition;
|
||||
protected Vector3 _shakePosition;
|
||||
protected RectTransform _rectTransform;
|
||||
protected Vector3 _shakePosition;
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
_rectTransform = this.gameObject.GetComponent<RectTransform>();
|
||||
_rectTransform = gameObject.GetComponent<RectTransform>();
|
||||
_initialPosition = _rectTransform.localPosition;
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
if (!Shaking)
|
||||
{
|
||||
_rectTransform.localPosition = _initialPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
_shakePosition.x = Mathf.PerlinNoise(-Time.time * Frequency, Time.time * Frequency) * Amplitude -
|
||||
Amplitude / 2f;
|
||||
_shakePosition.y =
|
||||
Mathf.PerlinNoise(-(Time.time + 0.25f) * Frequency, Time.time * Frequency) * Amplitude -
|
||||
Amplitude / 2f;
|
||||
_shakePosition.z =
|
||||
Mathf.PerlinNoise(-(Time.time + 0.5f) * Frequency, Time.time * Frequency) * Amplitude -
|
||||
Amplitude / 2f;
|
||||
_rectTransform.localPosition = _initialPosition + _shakePosition;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual IEnumerator Shake(float duration)
|
||||
{
|
||||
Shaking = true;
|
||||
yield return new WaitForSeconds(duration);
|
||||
Shaking = false;
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
if (!Shaking)
|
||||
{
|
||||
_rectTransform.localPosition = _initialPosition;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
_shakePosition.x = Mathf.PerlinNoise(-(Time.time) * Frequency, Time.time * Frequency) * Amplitude - Amplitude / 2f;
|
||||
_shakePosition.y = Mathf.PerlinNoise(-(Time.time + 0.25f) * Frequency, Time.time * Frequency) * Amplitude - Amplitude / 2f;
|
||||
_shakePosition.z = Mathf.PerlinNoise(-(Time.time + 0.5f) * Frequency, Time.time * Frequency) * Amplitude - Amplitude / 2f;
|
||||
_rectTransform.localPosition = _initialPosition + _shakePosition;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
@@ -10,148 +8,66 @@ namespace Lofelt.NiceVibrations
|
||||
{
|
||||
public class WobbleButton : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler
|
||||
{
|
||||
public RenderMode ParentCanvasRenderMode { get; protected set; }
|
||||
[Header("Bindings")] public Camera TargetCamera;
|
||||
|
||||
[Header("Bindings")]
|
||||
public Camera TargetCamera;
|
||||
public AudioSource SpringAudioSource;
|
||||
public Animator TargetAnimator;
|
||||
|
||||
[Header("Haptics")]
|
||||
public HapticSource SpringHapticSource;
|
||||
[Header("Haptics")] public HapticSource SpringHapticSource;
|
||||
|
||||
[Header("Colors")]
|
||||
public Image TargetModel;
|
||||
[Header("Colors")] public Image TargetModel;
|
||||
|
||||
[Header("Wobble")] public float OffDuration = 0.1f;
|
||||
|
||||
[Header("Wobble")]
|
||||
public float OffDuration = 0.1f;
|
||||
public float MaxRange;
|
||||
public AnimationCurve WobbleCurve;
|
||||
public float DragResetDuration = 4f;
|
||||
public float WobbleFactor = 2f;
|
||||
|
||||
protected Vector3 _neutralPosition;
|
||||
protected Canvas _canvas;
|
||||
protected Vector3 _newTargetPosition;
|
||||
protected Vector3 _eventPosition;
|
||||
protected Vector2 _workPosition;
|
||||
protected float _initialZPosition;
|
||||
protected bool _dragging;
|
||||
protected int _pointerID;
|
||||
protected PointerEventData _pointerEventData;
|
||||
protected RectTransform _rectTransform;
|
||||
protected float _dragEndedAt;
|
||||
|
||||
protected Vector3 _dragEndedPosition;
|
||||
protected float _dragEndedAt;
|
||||
protected bool _draggedOnce;
|
||||
protected bool _dragging;
|
||||
protected Vector3 _dragResetDirection;
|
||||
protected bool _pointerOn = false;
|
||||
protected bool _draggedOnce = false;
|
||||
protected Vector3 _eventPosition;
|
||||
protected float _initialZPosition;
|
||||
|
||||
protected Vector3 _neutralPosition;
|
||||
protected Vector3 _newTargetPosition;
|
||||
protected PointerEventData _pointerEventData;
|
||||
protected int _pointerID;
|
||||
protected bool _pointerOn;
|
||||
protected RectTransform _rectTransform;
|
||||
protected int _sparkAnimationParameter;
|
||||
protected int[] _wobbleAndroidAmplitude = { 0, 40, 0, 80 };
|
||||
|
||||
protected long[] _wobbleAndroidPattern = { 0, 40, 40, 80 };
|
||||
protected int[] _wobbleAndroidAmplitude = { 0, 40, 0, 80 };
|
||||
protected Vector2 _workPosition;
|
||||
public RenderMode ParentCanvasRenderMode { get; protected set; }
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void SetPitch(float newPitch)
|
||||
{
|
||||
SpringAudioSource.pitch = newPitch;
|
||||
SpringHapticSource.frequencyShift = NiceVibrationsDemoHelpers.Remap(newPitch, 0.3f, 1f, -1.0f, 1.0f);
|
||||
}
|
||||
|
||||
public virtual void Initialization()
|
||||
{
|
||||
_sparkAnimationParameter = Animator.StringToHash("Spark");
|
||||
ParentCanvasRenderMode = GetComponentInParent<Canvas>().renderMode;
|
||||
_canvas = GetComponentInParent<Canvas>();
|
||||
_initialZPosition = transform.position.z;
|
||||
_rectTransform = this.gameObject.GetComponent<RectTransform>();
|
||||
SetNeutralPosition();
|
||||
}
|
||||
|
||||
public virtual void SetNeutralPosition()
|
||||
{
|
||||
_neutralPosition = _rectTransform.transform.position;
|
||||
}
|
||||
|
||||
protected virtual Vector3 GetWorldPosition(Vector3 testPosition)
|
||||
{
|
||||
if (ParentCanvasRenderMode == RenderMode.ScreenSpaceCamera)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(_canvas.transform as RectTransform, testPosition, _canvas.worldCamera, out _workPosition);
|
||||
return _canvas.transform.TransformPoint(_workPosition);
|
||||
}
|
||||
else
|
||||
{
|
||||
return testPosition;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
if (_pointerOn && !_dragging)
|
||||
{
|
||||
_newTargetPosition = GetWorldPosition(_pointerEventData.position);
|
||||
|
||||
float distance = (_newTargetPosition - _neutralPosition).magnitude;
|
||||
var distance = (_newTargetPosition - _neutralPosition).magnitude;
|
||||
|
||||
if (distance < MaxRange)
|
||||
{
|
||||
_dragging = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_dragging = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (_dragging)
|
||||
{
|
||||
StickToPointer();
|
||||
}
|
||||
else
|
||||
{
|
||||
GoBackToInitialPosition();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void StickToPointer()
|
||||
{
|
||||
_draggedOnce = true;
|
||||
_eventPosition = _pointerEventData.position;
|
||||
|
||||
_newTargetPosition = GetWorldPosition(_eventPosition);
|
||||
|
||||
// We clamp the stick's position to let it move only inside its defined max range
|
||||
_newTargetPosition = Vector2.ClampMagnitude(_newTargetPosition - _neutralPosition, MaxRange);
|
||||
_newTargetPosition = _neutralPosition + _newTargetPosition;
|
||||
_newTargetPosition.z = _initialZPosition;
|
||||
|
||||
transform.position = _newTargetPosition;
|
||||
}
|
||||
|
||||
protected virtual void GoBackToInitialPosition()
|
||||
{
|
||||
if (!_draggedOnce)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Time.time - _dragEndedAt < DragResetDuration)
|
||||
{
|
||||
float time = Remap(Time.time - _dragEndedAt, 0f, DragResetDuration, 0f, 1f);
|
||||
float value = WobbleCurve.Evaluate(time) * WobbleFactor;
|
||||
_newTargetPosition = Vector3.LerpUnclamped(_neutralPosition, _dragEndedPosition, value);
|
||||
_newTargetPosition.z = _initialZPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
_newTargetPosition = _neutralPosition;
|
||||
_newTargetPosition.z = _initialZPosition;
|
||||
}
|
||||
transform.position = _newTargetPosition;
|
||||
}
|
||||
|
||||
public virtual void OnPointerEnter(PointerEventData data)
|
||||
@@ -181,11 +97,78 @@ namespace Lofelt.NiceVibrations
|
||||
SpringHapticSource.Play();
|
||||
}
|
||||
|
||||
public virtual void SetPitch(float newPitch)
|
||||
{
|
||||
SpringAudioSource.pitch = newPitch;
|
||||
SpringHapticSource.frequencyShift = NiceVibrationsDemoHelpers.Remap(newPitch, 0.3f, 1f, -1.0f, 1.0f);
|
||||
}
|
||||
|
||||
public virtual void Initialization()
|
||||
{
|
||||
_sparkAnimationParameter = Animator.StringToHash("Spark");
|
||||
ParentCanvasRenderMode = GetComponentInParent<Canvas>().renderMode;
|
||||
_canvas = GetComponentInParent<Canvas>();
|
||||
_initialZPosition = transform.position.z;
|
||||
_rectTransform = gameObject.GetComponent<RectTransform>();
|
||||
SetNeutralPosition();
|
||||
}
|
||||
|
||||
public virtual void SetNeutralPosition()
|
||||
{
|
||||
_neutralPosition = _rectTransform.transform.position;
|
||||
}
|
||||
|
||||
protected virtual Vector3 GetWorldPosition(Vector3 testPosition)
|
||||
{
|
||||
if (ParentCanvasRenderMode == RenderMode.ScreenSpaceCamera)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(_canvas.transform as RectTransform,
|
||||
testPosition, _canvas.worldCamera, out _workPosition);
|
||||
return _canvas.transform.TransformPoint(_workPosition);
|
||||
}
|
||||
|
||||
return testPosition;
|
||||
}
|
||||
|
||||
protected virtual void StickToPointer()
|
||||
{
|
||||
_draggedOnce = true;
|
||||
_eventPosition = _pointerEventData.position;
|
||||
|
||||
_newTargetPosition = GetWorldPosition(_eventPosition);
|
||||
|
||||
// We clamp the stick's position to let it move only inside its defined max range
|
||||
_newTargetPosition = Vector2.ClampMagnitude(_newTargetPosition - _neutralPosition, MaxRange);
|
||||
_newTargetPosition = _neutralPosition + _newTargetPosition;
|
||||
_newTargetPosition.z = _initialZPosition;
|
||||
|
||||
transform.position = _newTargetPosition;
|
||||
}
|
||||
|
||||
protected virtual void GoBackToInitialPosition()
|
||||
{
|
||||
if (!_draggedOnce) return;
|
||||
|
||||
if (Time.time - _dragEndedAt < DragResetDuration)
|
||||
{
|
||||
var time = Remap(Time.time - _dragEndedAt, 0f, DragResetDuration, 0f, 1f);
|
||||
var value = WobbleCurve.Evaluate(time) * WobbleFactor;
|
||||
_newTargetPosition = Vector3.LerpUnclamped(_neutralPosition, _dragEndedPosition, value);
|
||||
_newTargetPosition.z = _initialZPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
_newTargetPosition = _neutralPosition;
|
||||
_newTargetPosition.z = _initialZPosition;
|
||||
}
|
||||
|
||||
transform.position = _newTargetPosition;
|
||||
}
|
||||
|
||||
protected virtual float Remap(float x, float A, float B, float C, float D)
|
||||
{
|
||||
float remappedValue = C + (x - A) / (B - A) * (D - C);
|
||||
var remappedValue = C + (x - A) / (B - A) * (D - C);
|
||||
return remappedValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +1,22 @@
|
||||
{
|
||||
"name": "Lofelt.NiceVibrations.Demo",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:57a0b9bc628ab4740af4b6f1f0b2e134"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [
|
||||
{
|
||||
"name": "com.unity.modules.physics2d",
|
||||
"expression": "1.0.0",
|
||||
"define": "MM_PHYSICS2D"
|
||||
}
|
||||
],
|
||||
"noEngineReferences": false
|
||||
"name": "Lofelt.NiceVibrations.Demo",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:57a0b9bc628ab4740af4b6f1f0b2e134"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [
|
||||
{
|
||||
"name": "com.unity.modules.physics2d",
|
||||
"expression": "1.0.0",
|
||||
"define": "MM_PHYSICS2D"
|
||||
}
|
||||
],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
Reference in New Issue
Block a user