protected virtual void Start()
        {
            GlobalState.Instance.SceneToSwitchTo = Config.Scenes.None;

            // Keep a static reference to this class to be able to display toast messages from other components (namely QrCodeCollection.cs).
            CameraScriptInstance = this;

            _coin = GetComponent<AudioSource>();

            _qrCodeCollection = new QrCodeCollection();

            // Reset the global state current question and coin every time we restart the camera scene.
            GlobalState.Instance.Reset();

            Application.RequestUserAuthorization(UserAuthorization.WebCam);
            if (Application.HasUserAuthorization(UserAuthorization.WebCam))
            {
                Debug.Log("access to webcam granted!");
                Debug.Log("#WebCamDevices: " + WebCamTexture.devices.GetLength(0));

                if (GlobalState.Instance.WebCamTexture == null)
                {
                    // Find a backfacing camera device.
                    foreach (WebCamDevice device in WebCamTexture.devices)
                    {
                        Debug.Log("WebCamDevice: " + device.name);
                        Debug.Log("FrontFacing? " + device.isFrontFacing);
                        if (!device.isFrontFacing)
                        {
                            _backFacing = device;
                        }
                    }

                    // Try to obtain a 1024x768 texture from the webcam.
                    _webcamTexture = new WebCamTexture(_backFacing.name, 1024, 768);
                    _webcamTexture.Play();

                    // The device might not support the requested resolution, so we try again with a lower one.
                    if (_webcamTexture.width != 1024 || _webcamTexture.height != 768)
                    {
                        _webcamTexture.Stop();
                        _webcamTexture = new WebCamTexture(_backFacing.name, 640, 480);
                        _webcamTexture.Play();
                    }

                    // Keep a global reference to the WebCamTexture to speed up scene initialization next time.
                    GlobalState.Instance.WebCamTexture = _webcamTexture;
                }
                else
                {
                    _webcamTexture = GlobalState.Instance.WebCamTexture;
                    _webcamTexture.Play();
                }

                GetComponent<Renderer>().material.SetTexture("_MainTex", _webcamTexture);
                Debug.Log(string.Format("Actual camera dimens: {0}x{1}", _webcamTexture.width, _webcamTexture.height));

                GlobalState.Instance.CamWidth = _webcamTexture.width;
                GlobalState.Instance.CamHeight = _webcamTexture.height;
                float camRatio = (float) _webcamTexture.width/_webcamTexture.height;
                float screenRatio = (float) Screen.width/Screen.height;

                // Scale plane so it fills the screen while keeping the camera's aspect ratio.
                // If the camera's aspect ratio differs from the screen's,
                // one side will match exactly and the other side will be larger than the screen's dimension.
                var idealHeight = 0.7f;
                if (screenRatio > camRatio)
                {
                    gameObject.transform.localScale = new Vector3(screenRatio*idealHeight, 1,
                        screenRatio*idealHeight/camRatio);
                }
                else
                {
                    gameObject.transform.localScale = new Vector3(camRatio*idealHeight, 1, idealHeight);
                }

                GlobalState.Instance.PlaneWidth = gameObject.transform.localScale.x*10;
                GlobalState.Instance.PlaneHeight = gameObject.transform.localScale.z*10;

                _qrCodeThread = new Thread(DecodeQr);
                _qrCodeThread.Start();
            }
            else
            {
                Debug.LogError("No User Authorization for Camera Device.");
            }

            // Check win condition: if the user has already collected all the coins, show a toast.
            if (GlobalState.Instance.AllQuestions.questions.Length == GlobalState.Instance.CollectedCoinCount())
            {
                SetToastToShow(StringResources.WinToastMessage, ToastLengthLong);
            }
        }
Пример #2
0
        protected virtual void Start()
        {
            GlobalState.Instance.SceneToSwitchTo = Config.Scenes.None;

            // Keep a static reference to this class to be able to display toast messages from other components (namely QrCodeCollection.cs).
            CameraScriptInstance = this;

            _coin = GetComponent <AudioSource>();

            _qrCodeCollection = new QrCodeCollection();

            // Reset the global state current question and coin every time we restart the camera scene.
            GlobalState.Instance.Reset();

            Application.RequestUserAuthorization(UserAuthorization.WebCam);
            if (Application.HasUserAuthorization(UserAuthorization.WebCam))
            {
                Debug.Log("access to webcam granted!");
                Debug.Log("#WebCamDevices: " + WebCamTexture.devices.GetLength(0));

                if (GlobalState.Instance.WebCamTexture == null)
                {
                    // Find a backfacing camera device.
                    foreach (WebCamDevice device in WebCamTexture.devices)
                    {
                        Debug.Log("WebCamDevice: " + device.name);
                        Debug.Log("FrontFacing? " + device.isFrontFacing);
                        if (!device.isFrontFacing)
                        {
                            _backFacing = device;
                        }
                    }

                    // Try to obtain a 1024x768 texture from the webcam.
                    _webcamTexture = new WebCamTexture(_backFacing.name, 1024, 768);
                    _webcamTexture.Play();

                    // The device might not support the requested resolution, so we try again with a lower one.
                    if (_webcamTexture.width != 1024 || _webcamTexture.height != 768)
                    {
                        _webcamTexture.Stop();
                        _webcamTexture = new WebCamTexture(_backFacing.name, 640, 480);
                        _webcamTexture.Play();
                    }

                    // Keep a global reference to the WebCamTexture to speed up scene initialization next time.
                    GlobalState.Instance.WebCamTexture = _webcamTexture;
                }
                else
                {
                    _webcamTexture = GlobalState.Instance.WebCamTexture;
                    _webcamTexture.Play();
                }

                GetComponent <Renderer>().material.SetTexture("_MainTex", _webcamTexture);
                Debug.Log(string.Format("Actual camera dimens: {0}x{1}", _webcamTexture.width, _webcamTexture.height));

                GlobalState.Instance.CamWidth  = _webcamTexture.width;
                GlobalState.Instance.CamHeight = _webcamTexture.height;
                float camRatio    = (float)_webcamTexture.width / _webcamTexture.height;
                float screenRatio = (float)Screen.width / Screen.height;

                // Scale plane so it fills the screen while keeping the camera's aspect ratio.
                // If the camera's aspect ratio differs from the screen's,
                // one side will match exactly and the other side will be larger than the screen's dimension.
                var idealHeight = 0.7f;
                if (screenRatio > camRatio)
                {
                    gameObject.transform.localScale = new Vector3(screenRatio * idealHeight, 1,
                                                                  screenRatio * idealHeight / camRatio);
                }
                else
                {
                    gameObject.transform.localScale = new Vector3(camRatio * idealHeight, 1, idealHeight);
                }

                GlobalState.Instance.PlaneWidth  = gameObject.transform.localScale.x * 10;
                GlobalState.Instance.PlaneHeight = gameObject.transform.localScale.z * 10;

                _qrCodeThread = new Thread(DecodeQr);
                _qrCodeThread.Start();
            }
            else
            {
                Debug.LogError("No User Authorization for Camera Device.");
            }

            // Check win condition: if the user has already collected all the coins, show a toast.
            if (GlobalState.Instance.AllQuestions.questions.Length == GlobalState.Instance.CollectedCoinCount())
            {
                SetToastToShow(StringResources.WinToastMessage, ToastLengthLong);
            }
        }