VAUAdapter.cs 2.71 KB
Newer Older
1 2 3 4
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
5
using System.Runtime.InteropServices;
6 7 8
using VA;

// Adapter to VA (singleton)
9
/**
10
 *  Drop this file onto any GameObject. Add a VAUConfig script to the same object for special settings. Add VAUControl to control the running core.
11
 * */
12 13
public class VAUAdapter : MonoBehaviour
{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
14 15
    public static string Server = "localhost";
    public static int Port = 12340;
16

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
17
    public static string AdditionalSearchPath = ""; // Provide further paths where VA should look for audio files, directivities and HRIRs.
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
18 19 20
	public static bool ResetOnStart = true;
	public static bool ResetOnStop = true;
	public static bool DeactivateUnitySoundOnAwake = true;
21

22
    private static VANet _VA = null;
23 24

    // No public construction allowed
25
    private VAUAdapter() { }
26 27 28 29 30

    public static VANet VA
    {
        get
        {
31
            if (_VA == null)
32
            {
33
                _VA = new VANet();
34
				Init (); // Try to connect as early as possible
35 36 37 38 39 40 41
            }
            return _VA;
        }
    }

    private static void Init()
    {
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
42
		if (!VA.Connect(Server, Port))
43
        {
44 45 46
            Debug.LogError("Could not connect to VA server on " + Server + " using port " + Port);
            return;
        }
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
47

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
48 49
		if( ResetOnStart )
			VA.Reset ();
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
50

51
        // Add Asset folder as search path for VA (only works if VA is running on same host PC)
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
52
        if (!VA.AddSearchPath(Application.dataPath))
53
            Debug.LogError("Could not add application assets folder to VA search path, VA server running on remote host?");
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
54

55 56
        if (AdditionalSearchPath.Length > 0)
        {
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
57
            VA.AddSearchPath(AdditionalSearchPath);
58 59
            // ... append more if required.
        }
60
    }
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
61

62 63
	// Use this for initialization
	void OnEnable()
64
    {
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
65 66
        if (DeactivateUnitySoundOnAwake)
            DeactivateUnitySound();
67
    
68 69
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnGlobalAuralizationModeChanged;
70 71 72 73
    }

    void OnGlobalAuralizationModeChanged(string AuraMode)
    {
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
74
        VA.SetGlobalAuralizationMode(AuraMode);
75 76
    }

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
77
    void OnDestroy()
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
78 79 80 81
	{
		if( ResetOnStop )
			VA.Reset ();
        VA.Disconnect();
82 83 84 85 86 87 88 89 90
    }

    void DeactivateUnitySound()
    {
        AudioSource[] audioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
        foreach (AudioSource audioSource in audioSources)
        {
            audioSource.enabled = false;
        }
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
91

92 93 94 95 96
        AudioListener[] audioListeners = FindObjectsOfType(typeof(AudioListener)) as AudioListener[];
        foreach (AudioListener audioListener in audioListeners)
        {
            audioListener.enabled = false;
        }
97 98
    }
}