Skip to content

Instantly share code, notes, and snippets.

@HofiOne
HofiOne / NativeVibrateController.m
Last active March 20, 2026 09:38
Native iOS, tvOS vibration of the first available controller
#if TARGET_OS_IOS || TARGET_OS_TV
static CHHapticEngine* cachedHapticEngine = nil;
static GCController* cachedController = nil;
bool Native_VibrateController(float intensity, float duration)
{
// Clamp input
intensity = fmaxf(0.0f, fminf(intensity, 1.0f));
duration = fmaxf(0.02f, duration);
@HofiOne
HofiOne / NativePlayHaptic.m
Last active March 20, 2026 10:16
Native iOS device vibration
#if TARGET_OS_IOS
static CHHapticEngine* hapticEngine = nil;
void Native_PlayHaptic(float intensity, float duration)
{
// Core Haptics available?
if (@available(iOS 13.0,*)) {
NSError *error = nil;
if (!hapticEngine) {
@HofiOne
HofiOne / NativeAndroidVibrate.cs
Last active March 22, 2026 15:10
Native Android device vibration
if UNITY_ANDROID
// Cache Android vibrator objects to avoid allocations on every call
private static AndroidJavaObject cachedVibrator = null;
private static bool supportsAmplitudeControl = false;
private static bool vibratorInitialized = false;
private static void InitializeAndroidVibrator()
{
if (vibratorInitialized)
return;