架构大更

This commit is contained in:
SoulliesOfficial
2026-03-20 11:56:50 -04:00
parent e60ef64d01
commit d09b58fd80
3663 changed files with 15232012 additions and 105579 deletions

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -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
}

View File

@@ -1,27 +1,27 @@
{
"name": "Lofelt.NiceVibrations",
"rootNamespace": "",
"references": [
"GUID:75469ad4d38634e559750d17036d5f7c"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [
{
"name": "com.unity.inputsystem",
"expression": "1.0",
"define": "NICE_VIBRATIONS_INPUTSYSTEM_INSTALLED"
},
{
"name": "com.unity.modules.physics2d",
"expression": "1.0.0",
"define": "MM_PHYSICS2D"
}
],
"noEngineReferences": false
"name": "Lofelt.NiceVibrations",
"rootNamespace": "",
"references": [
"GUID:75469ad4d38634e559750d17036d5f7c"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [
{
"name": "com.unity.inputsystem",
"expression": "1.0",
"define": "NICE_VIBRATIONS_INPUTSYSTEM_INSTALLED"
},
{
"name": "com.unity.modules.physics2d",
"expression": "1.0.0",
"define": "MM_PHYSICS2D"
}
],
"noEngineReferences": false
}

View File

@@ -1,20 +1,20 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System;
using UnityEngine;
using System.Text;
using UnityEngine;
#if UNITY_2020_2_OR_NEWER
using UnityEditor.AssetImporters;
#elif UNITY_2019_4_OR_NEWER
using UnityEditor.Experimental.AssetImporters;
#endif
namespace Lofelt.NiceVibrations
{
[ScriptedImporter(version: 3, ext: "haptic", AllowCaching = true)]
[ScriptedImporter(3, "haptic", AllowCaching = true)]
/// <summary>
/// Provides an importer for the HapticClip component.
/// </summary>
@@ -22,49 +22,10 @@ namespace Lofelt.NiceVibrations
/// The importer takes a <c>.haptic</c> file and converts it into a HapticClip.
public class HapticImporter : ScriptedImporter
{
#if !NICE_VIBRATIONS_DISABLE_GAMEPAD_SUPPORT
[DllImport("nice_vibrations_editor_plugin")]
private static extern IntPtr nv_plugin_convert_haptic_to_gamepad_rumble([In] byte[] bytes, long size);
[DllImport("nice_vibrations_editor_plugin")]
private static extern void nv_plugin_destroy(IntPtr gamepadRumble);
[DllImport("nice_vibrations_editor_plugin")]
private static extern UIntPtr nv_plugin_get_length(IntPtr gamepadRumble);
[DllImport("nice_vibrations_editor_plugin")]
private static extern void nv_plugin_get_durations(IntPtr gamepadRumble, [Out] int[] durations);
[DllImport("nice_vibrations_editor_plugin")]
private static extern void nv_plugin_get_low_frequency_motor_speeds(IntPtr gamepadRumble, [Out] float[] lowFrequencies);
[DllImport("nice_vibrations_editor_plugin")]
private static extern void nv_plugin_get_high_frequency_motor_speeds(IntPtr gamepadRumble, [Out] float[] highFrequencies);
// We can not use "[return: MarshalAs(UnmanagedType.LPUTF8Str)]" here, and have to use
// IntPtr for the return type instead. Otherwise the C# runtime tries to free the returned
// string, which is invalid as the native plugin keeps ownership of the string.
// We use PtrToStringUTF8() to manually convert the IntPtr to a string instead.
[DllImport("nice_vibrations_editor_plugin")]
private static extern IntPtr nv_plugin_get_last_error();
[DllImport("nice_vibrations_editor_plugin")]
private static extern UIntPtr nv_plugin_get_last_error_length();
// Alternative to Marshal.PtrToStringUTF8() which was introduced in .NET 5 and isn't yet
// supported by Unity
private string PtrToStringUTF8(IntPtr ptr, int length)
{
byte[] bytes = new byte[length];
Marshal.Copy(ptr, bytes, 0, length);
return Encoding.UTF8.GetString(bytes, 0, length);
}
#endif
public override void OnImportAsset(AssetImportContext ctx)
{
// Load .haptic clip from file
var fileName = System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath);
var fileName = Path.GetFileNameWithoutExtension(ctx.assetPath);
var jsonBytes = File.ReadAllBytes(ctx.assetPath);
var hapticClip = HapticClip.CreateInstance<HapticClip>();
hapticClip.json = jsonBytes;
@@ -74,12 +35,12 @@ namespace Lofelt.NiceVibrations
// library nice_vibrations_editor_plugin. That plugin is only used in the Unity editor, and
// not at runtime.
GamepadRumble rumble = default;
IntPtr nativeRumble = nv_plugin_convert_haptic_to_gamepad_rumble(jsonBytes, jsonBytes.Length);
var nativeRumble = nv_plugin_convert_haptic_to_gamepad_rumble(jsonBytes, jsonBytes.Length);
if (nativeRumble != IntPtr.Zero)
{
try
{
uint length = (uint)nv_plugin_get_length(nativeRumble);
var length = (uint)nv_plugin_get_length(nativeRumble);
rumble.durationsMs = new int[length];
rumble.lowFrequencyMotorSpeeds = new float[length];
rumble.highFrequencyMotorSpeeds = new float[length];
@@ -88,11 +49,8 @@ namespace Lofelt.NiceVibrations
nv_plugin_get_low_frequency_motor_speeds(nativeRumble, rumble.lowFrequencyMotorSpeeds);
nv_plugin_get_high_frequency_motor_speeds(nativeRumble, rumble.highFrequencyMotorSpeeds);
int totalDurationMs = 0;
foreach (int duration in rumble.durationsMs)
{
totalDurationMs += duration;
}
var totalDurationMs = 0;
foreach (var duration in rumble.durationsMs) totalDurationMs += duration;
rumble.totalDurationMs = totalDurationMs;
}
finally
@@ -115,5 +73,45 @@ namespace Lofelt.NiceVibrations
ctx.AddObjectToAsset("com.lofelt.HapticClip", hapticClip);
ctx.SetMainObject(hapticClip);
}
#if !NICE_VIBRATIONS_DISABLE_GAMEPAD_SUPPORT
[DllImport("nice_vibrations_editor_plugin")]
private static extern IntPtr nv_plugin_convert_haptic_to_gamepad_rumble([In] byte[] bytes, long size);
[DllImport("nice_vibrations_editor_plugin")]
private static extern void nv_plugin_destroy(IntPtr gamepadRumble);
[DllImport("nice_vibrations_editor_plugin")]
private static extern UIntPtr nv_plugin_get_length(IntPtr gamepadRumble);
[DllImport("nice_vibrations_editor_plugin")]
private static extern void nv_plugin_get_durations(IntPtr gamepadRumble, [Out] int[] durations);
[DllImport("nice_vibrations_editor_plugin")]
private static extern void nv_plugin_get_low_frequency_motor_speeds(IntPtr gamepadRumble,
[Out] float[] lowFrequencies);
[DllImport("nice_vibrations_editor_plugin")]
private static extern void nv_plugin_get_high_frequency_motor_speeds(IntPtr gamepadRumble,
[Out] float[] highFrequencies);
// We can not use "[return: MarshalAs(UnmanagedType.LPUTF8Str)]" here, and have to use
// IntPtr for the return type instead. Otherwise the C# runtime tries to free the returned
// string, which is invalid as the native plugin keeps ownership of the string.
// We use PtrToStringUTF8() to manually convert the IntPtr to a string instead.
[DllImport("nice_vibrations_editor_plugin")]
private static extern IntPtr nv_plugin_get_last_error();
[DllImport("nice_vibrations_editor_plugin")]
private static extern UIntPtr nv_plugin_get_last_error_length();
// Alternative to Marshal.PtrToStringUTF8() which was introduced in .NET 5 and isn't yet
// supported by Unity
private string PtrToStringUTF8(IntPtr ptr, int length)
{
var bytes = new byte[length];
Marshal.Copy(ptr, bytes, 0, length);
return Encoding.UTF8.GetString(bytes, 0, length);
}
#endif
}
}
}

View File

@@ -1,17 +1,17 @@
{
"name": "Lofelt.NiceVibrations.Editor",
"references": [
"GUID:57a0b9bc628ab4740af4b6f1f0b2e134"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
"name": "Lofelt.NiceVibrations.Editor",
"references": [
"GUID:57a0b9bc628ab4740af4b6f1f0b2e134"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}