1. 程式人生 > 其它 >Unity轉微信小遊戲無法喚起震動的解決方案

Unity轉微信小遊戲無法喚起震動的解決方案

技術標籤:遊戲研發客戶端實習日記遊戲小程式遊戲開發微信遊戲

問題描述

大致是這樣,unity小遊戲在導包以後無法在微信程式裡觸發震動。

解決方法

這個只需要以用微信的介面就可以了。
目前在學習的專案中大部分使用了適應各個平臺的震動差點,比如NiceVibrate這種,但並不支援小遊戲。原生存app由於使用外掛對震動的精確細分有很多,使用了列舉。那麼在保持原有不變的情況下,來重寫新的指令碼。只需要不同列舉型別同樣震動介面就可以了。

快速一點的解決辦法是直接刪除外掛整體,在接入SDK的地方接入新寫的指令碼。
我新寫了一個震動指令碼 MMVibrationManager.cs ,適用於在Unity下除錯。

**SetHapticsActive()**方法預留了遊戲中震動開關,在開關相應的位置傳入true和false開控制整個遊戲中震動遮蔽和觸發的效果。

using System.Collections;
using System.Collections.Generic;
using MoreMountains.NiceVibrations;
using UnityEngine;
using WeChatWASM;

public class MMVibrationManager : MonoBehaviour
{
    
    private static bool _vibrationsActive =
true; /// 啟用或禁用通過這個類呼叫的所有觸覺 public static void SetHapticsActive(bool status) { Debug.LogError("設定震動開關狀態: " + status); _vibrationsActive = status; } /// 震動反饋 public static void Haptic(HapticTypes type, bool defaultToRegularVibrate = false, bool alsoRumble =
false, MonoBehaviour coroutineSupport = null, int controllerID = -1) { if (!_vibrationsActive) { Debug.LogError("vibrationsActive不震動時的值: "+ _vibrationsActive); // ------------------------------ [ 不震動測試 ] ------------------------------ switch (type) { case HapticTypes.None: Debug.LogError("------ 00 -----"); break; case HapticTypes.Selection: Debug.LogError("------ 11 -----"); break; case HapticTypes.Success: Debug.LogError("------ 22 -----"); break; case HapticTypes.Warning: Debug.LogError("------ 33 -----"); break; case HapticTypes.Failure: Debug.LogError("------ 44 -----"); break; case HapticTypes.LightImpact: Debug.LogError("------ 55 -----"); break; case HapticTypes.MediumImpact: Debug.LogError("------ 66 -----"); break; case HapticTypes.HeavyImpact: Debug.LogError("------ 77 -----"); break; case HapticTypes.RigidImpact: Debug.LogError("------ 88 -----"); break; case HapticTypes.SoftImpact: Debug.LogError("------ 99 -----"); break; } return; } // ------------------------------ [ 震動測試 ] ------------------------------ Debug.LogError("進入實現微信震動介面: "); Debug.LogError("vibrationsActive的值: "+ _vibrationsActive); #if WXGAME && !UNITY_EDITOR switch (type) { case HapticTypes.None: Debug.LogError("------ 0 -----"); break; case HapticTypes.Selection: Debug.LogError("------ 1 -----"); WX.VibrateLong(); break; case HapticTypes.Success: Debug.LogError("------ 2 -----"); WX.VibrateLong(); break; case HapticTypes.Warning: Debug.LogError("------ 3 -----"); WX.VibrateLong(); break; case HapticTypes.Failure: Debug.LogError("------ 4 -----"); WX.VibrateLong(); break; case HapticTypes.LightImpact: Debug.LogError("------ 5 -----"); WX.VibrateLong(); break; case HapticTypes.MediumImpact: Debug.LogError("------ 6 -----"); WX.VibrateLong(); break; case HapticTypes.HeavyImpact: Debug.LogError("------ 7 -----"); WX.VibrateLong(); break; case HapticTypes.RigidImpact: Debug.LogError("------ 8 -----"); WX.VibrateLong(); break; case HapticTypes.SoftImpact: Debug.LogError("------ 9 -----"); WX.VibrateLong(); break; } #endif switch (type) { case HapticTypes.None: Debug.LogError("------ 0 -----"); break; case HapticTypes.Selection: Debug.LogError("------ 1 -----"); break; case HapticTypes.Success: Debug.LogError("------ 2 -----"); break; case HapticTypes.Warning: Debug.LogError("------ 3 -----"); break; case HapticTypes.Failure: Debug.LogError("------ 4 -----"); break; case HapticTypes.LightImpact: Debug.LogError("------ 5 -----"); break; case HapticTypes.MediumImpact: Debug.LogError("------ 6 -----"); break; case HapticTypes.HeavyImpact: Debug.LogError("------ 7 -----"); break; case HapticTypes.RigidImpact: Debug.LogError("------ 8 -----"); break; case HapticTypes.SoftImpact: Debug.LogError("------ 9 -----"); break; } } }

可能存在問題的優化思考

目前微信的震動介面只有短幅和長幅,分比別是15ms WX.VibrateShort()和 400msWX.VibrateLong() 。15過短大部分機型體驗欠佳,400太長適合遊戲結束player失敗之類的。

在網上看到一個帖子說是用定時器Timer來實現,多次觸發達到連續震動的效果,比如30ms就用兩個短震動來解決。自己還沒驗證,後面驗證親測成功後就在這篇文章進行更新。