From 62801f22e9a62d31db9828327f81da761914de59 Mon Sep 17 00:00:00 2001 From: changyunjia <905640960@qq.com> Date: Wed, 15 Jul 2026 15:30:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Plugins/iOS/HapticFeedbackManager.h | 8 ++ .../Plugins/iOS/HapticFeedbackManager.h.meta | 33 +++++++ Assets/Plugins/iOS/HapticFeedbackManager.m | 95 +++++++++++++++++++ .../Plugins/iOS/HapticFeedbackManager.m.meta | 33 +++++++ Assets/Plugins/iOS/pluginIOs.mm | 4 +- 5 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 Assets/Plugins/iOS/HapticFeedbackManager.h create mode 100644 Assets/Plugins/iOS/HapticFeedbackManager.h.meta create mode 100644 Assets/Plugins/iOS/HapticFeedbackManager.m create mode 100644 Assets/Plugins/iOS/HapticFeedbackManager.m.meta diff --git a/Assets/Plugins/iOS/HapticFeedbackManager.h b/Assets/Plugins/iOS/HapticFeedbackManager.h new file mode 100644 index 0000000..2214e46 --- /dev/null +++ b/Assets/Plugins/iOS/HapticFeedbackManager.h @@ -0,0 +1,8 @@ +// HapticFeedbackManager.h +#import + +@interface HapticFeedbackManager : NSObject + ++ (void)TriggerCustomHaptic:(float)intensity sharpness:(float)sharpness duration:(float)duration; + +@end diff --git a/Assets/Plugins/iOS/HapticFeedbackManager.h.meta b/Assets/Plugins/iOS/HapticFeedbackManager.h.meta new file mode 100644 index 0000000..ad3c285 --- /dev/null +++ b/Assets/Plugins/iOS/HapticFeedbackManager.h.meta @@ -0,0 +1,33 @@ +fileFormatVersion: 2 +guid: c9ce6d3a070acdf4394bf7556c953c27 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + iPhone: iOS + second: + enabled: 1 + settings: + AddToEmbeddedBinaries: false + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/iOS/HapticFeedbackManager.m b/Assets/Plugins/iOS/HapticFeedbackManager.m new file mode 100644 index 0000000..36a5eb4 --- /dev/null +++ b/Assets/Plugins/iOS/HapticFeedbackManager.m @@ -0,0 +1,95 @@ +// HapticFeedbackManager.m +#import "HapticFeedbackManager.h" +#import +#import +#import + +@implementation HapticFeedbackManager + +static CHHapticEngine *hapticEngine; + ++ (void)initializeHapticEngine { + if (!hapticEngine) { + NSError *error = nil; + hapticEngine = [[CHHapticEngine alloc] initAndReturnError:&error]; + if (error) { + NSLog(@"Failed to create haptic engine: %@", error.localizedDescription); + } else { + [hapticEngine startWithCompletionHandler:^(NSError * _Nullable error) { + if (error) { + NSLog(@"Failed to start haptic engine: %@", error.localizedDescription); + } + }]; + } + } +} + ++ (void)TriggerCustomHaptic:(float)intensity sharpness:(float)sharpness duration:(float)duration { + if (@available(iOS 13.0, *)) { + + if (![CHHapticEngine capabilitiesForHardware].supportsHaptics) { + NSLog(@"Haptic feedback is not supported on this device."); + [self triggerFallbackHaptic]; + return; + } + + [self initializeHapticEngine]; + + NSError *error = nil; + CHHapticEventParameter *intensityParameter = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticIntensity value:intensity]; + CHHapticEventParameter *sharpnessParameter = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticSharpness value:sharpness]; + CHHapticEvent *event = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticContinuous parameters:@[intensityParameter, sharpnessParameter] relativeTime:0 duration:duration]; + + CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithEvents:@[event] parameters:@[] error:&error]; + if (error) { + NSLog(@"Failed to create haptic pattern: %@", error.localizedDescription); + return; + } + + id player = [hapticEngine createPlayerWithPattern:pattern error:&error]; + if (error) { + NSLog(@"Failed to create haptic player: %@", error.localizedDescription); + return; + } + + [player startAtTime:CHHapticTimeImmediate error:&error]; + if (error) { + NSLog(@"Failed to start haptic player: %@", error.localizedDescription); + } + } else { + NSLog(@"Core Haptics is not available on this version of iOS."); + } +} + ++ (void)triggerFallbackHaptic { + // 使用轻微震动作为替代 +// UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] +// initWithStyle:UIImpactFeedbackStyleLight]; +// [generator prepare]; +// [generator impactOccurred]; + +// UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] +// initWithStyle:UIImpactFeedbackStyleHeavy]; +// [generator prepare]; +// [generator impactOccurred]; + + // 使用 UINotificationFeedbackGenerator 触发通知风格的震动 +// UINotificationFeedbackGenerator *generator = [[UINotificationFeedbackGenerator alloc] init]; +// [generator prepare]; +// [generator notificationOccurred:UINotificationFeedbackTypeSuccess]; + + // 使用系统震动 + AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); +} + + +#ifdef __cplusplus +extern "C" { +#endif + void TriggerCustomHaptic(float intensity, float sharpness, float duration) { [HapticFeedbackManager TriggerCustomHaptic:intensity sharpness:sharpness duration:duration]; + } +#ifdef __cplusplus +} +#endif +@end + diff --git a/Assets/Plugins/iOS/HapticFeedbackManager.m.meta b/Assets/Plugins/iOS/HapticFeedbackManager.m.meta new file mode 100644 index 0000000..562e249 --- /dev/null +++ b/Assets/Plugins/iOS/HapticFeedbackManager.m.meta @@ -0,0 +1,33 @@ +fileFormatVersion: 2 +guid: 4c2e3a62020ffae4998ebaa981d7c1e6 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + iPhone: iOS + second: + enabled: 1 + settings: + AddToEmbeddedBinaries: false + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/iOS/pluginIOs.mm b/Assets/Plugins/iOS/pluginIOs.mm index 148ff39..717b264 100644 --- a/Assets/Plugins/iOS/pluginIOs.mm +++ b/Assets/Plugins/iOS/pluginIOs.mm @@ -37,6 +37,8 @@ void SetBtn(int left, int top, int right, int bottom){ void OpenWv (bool show) { [[H5View shared] OpenWv]; } - +void showGameA(bool flag){//显示游戏a + [NativeUIManager showMainMenu]; +} }