Skip to content
Snippets Groups Projects
Select Git revision
  • develop
  • master default protected
  • feature/build
  • VA_v2022a
  • v2021.a
  • v2020.a
  • v2019.a
  • v2018.b
  • v2017.c
  • v2017.a
  • v2016.a
11 results

VAAdapter.cs

Blame
  • CarolinReimers's avatar
    Carolin Breuer authored
    a1ed562d
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    VAAdapter.cs 2.39 KiB
    using System;
    using UnityEngine;
    using VA;
    
    namespace VAUnity
    {
        /// <summary>
        /// establish and close the connection from Unity to VA, get called by VAUnity
        /// </summary>
        [CreateAssetMenu(fileName = "VAAdapter", menuName = "VAUnity/VAAdapter", order = 1)]
        public class VAAdapter : ScriptableObject
        {
            [Tooltip("Ip address of the VA Server, default is localhost.")]
            [SerializeField] private string server = "localhost";
            
            [Tooltip("Port of the VA Server, default is 12340.")]
            [SerializeField] private int port = 12340;
            
            [Tooltip("Toggle Reset Server on Start")]
            [SerializeField] private bool resetOnStart = true;
            
            [Tooltip("Toggle Reset Server on Stop")]
            [SerializeField] private bool resetOnStop = true;
    
            public bool IsLocalhost => server == "localhost" || server == "127.0.0.1";
    
            private VANet _va;
    
            /// <summary>
            /// Is called in the Scene by VAUnity
            /// </summary>
            public VANet Init()
            {
                if (_va != null) return _va;
                
                _va = new VANet();
                
                // insert timeout to try and connect to the VA server, since some PCs are too slow
                // VAUnity will crash if connection to server cannot be established in time
    
                DateTime start = DateTime.Now;
                bool started = false;
                float connectionTimeout = 5;
                
                while (!started && ((DateTime.Now - start).TotalSeconds < 5))
                {
                    started = _va.Connect(server, port);
    
                    if (!started)
                    {
                        Debug.LogError("Could not connect to VA server on " + server + " using port " + port + 
                            " . Time left until connect timeout: " + (5-(DateTime.Now - start).TotalSeconds));
                    }
                }
    
                if (!started)
                {
                    AppHelper.Quit();
                    _va = null;
                    return _va;
                }
    
                if( resetOnStart )
                    _va?.Reset ();
    
                return _va;
                // isInit = true;
            }
    
            /// <summary>
            /// reset and disconnect server 
            /// </summary>
            public void OnDestroy()
            {
                if( resetOnStop )
                    _va?.Reset ();
                _va?.Disconnect();
                // isInit = false;
            }
        }
    }