Created
December 18, 2023 01:20
-
-
Save dilmerv/da74fba4e7f71ae4bd528cab253c144d to your computer and use it in GitHub Desktop.
A basic example of Meta Haptics SDK Singleton to play Haptic Files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Oculus.Haptics; | |
using UnityEngine; | |
public class HapticsManager : MonoBehaviour | |
{ | |
public static HapticsManager Instance; | |
[SerializeField] private HapticClip clip1; | |
[SerializeField] private HapticClip clip2; | |
[SerializeField] private HapticClip clip3; | |
private HapticClipPlayer player; | |
private void Awake() | |
{ | |
if (Instance == null) | |
{ | |
Instance = this; | |
} | |
else if (Instance != null) | |
{ | |
Destroy(gameObject); | |
} | |
DontDestroyOnLoad(gameObject); | |
player = new HapticClipPlayer(clip1); | |
} | |
public void PlayClip1() | |
{ | |
player.clip = clip1; | |
player.Play(Controller.Both); | |
Debug.Log($"Haptics Played: {clip1.name}"); | |
} | |
public void PlayClip2() | |
{ | |
player.clip = clip2; | |
player.Play(Controller.Right); | |
Debug.Log($"Haptics Played: {clip2.name}"); | |
} | |
public void PlayClip3() | |
{ | |
player.clip = clip3; | |
player.Play(Controller.Right); | |
Debug.Log($"Haptics Played: {clip3.name}"); | |
} | |
public void PlayWithClipName(string clipName, | |
Controller controller = Controller.Right) | |
{ | |
string hapticFile = $"Haptics/{clipName}"; | |
var hapticsClip = Resources.Load<HapticClip>(hapticFile); | |
if (hapticsClip) | |
{ | |
player.clip = hapticsClip; | |
player.Play(controller); | |
Debug.Log($"Haptics Played: {hapticsClip.name}"); | |
} | |
} | |
private void OnDestroy() | |
{ | |
player.Dispose(); | |
} | |
private void OnApplicationQuit() | |
{ | |
Haptics.Instance.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment