VAUAdapter.cs 2.37 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 to control parameters.
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
WIP  
Dipl.-Ing. Jonas Stienen committed
17 18 19
	public static bool ResetOnStart = true;
	public static bool ResetOnStop = true;
	public static bool DeactivateUnitySoundOnAwake = true;
20

21
    private static VANet _VA = null;
22 23

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

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

    private static void Init()
    {
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
41
		if (!VA.Connect(Server, Port))
42
        {
43 44 45
            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
46

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

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

55 56
	// Use this for initialization
	void OnEnable()
57
    {
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
58 59
        if (DeactivateUnitySoundOnAwake)
            DeactivateUnitySound();
60
    
61 62
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnGlobalAuralizationModeChanged;
63 64 65 66
    }

    void OnGlobalAuralizationModeChanged(string AuraMode)
    {
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
67
        VA.SetGlobalAuralizationMode(AuraMode);
68 69
    }

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
70
    void OnDestroy()
Dipl.-Ing. Jonas Stienen's avatar
WIP  
Dipl.-Ing. Jonas Stienen committed
71 72 73 74
	{
		if( ResetOnStop )
			VA.Reset ();
        VA.Disconnect();
75 76 77 78 79 80 81 82 83
    }

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

85 86 87 88 89
        AudioListener[] audioListeners = FindObjectsOfType(typeof(AudioListener)) as AudioListener[];
        foreach (AudioListener audioListener in audioListeners)
        {
            audioListener.enabled = false;
        }
90 91
    }
}