Skip to content

Instantly share code, notes, and snippets.

@dilmerv
Created December 18, 2023 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dilmerv/da74fba4e7f71ae4bd528cab253c144d to your computer and use it in GitHub Desktop.
Save dilmerv/da74fba4e7f71ae4bd528cab253c144d to your computer and use it in GitHub Desktop.
A basic example of Meta Haptics SDK Singleton to play Haptic Files
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