void OnDestroy() { if (isBrInited && sensorData != null && sensorData.sensorInterface != null) { // finish background removal sensorData.sensorInterface.FinishBackgroundRemoval(sensorData); } isBrInited = false; instance = null; }
//----------------------------------- end of public functions --------------------------------------// void Start() { try { // get sensor data KinectManager kinectManager = KinectManager.Instance; if (kinectManager && kinectManager.IsInitialized()) { sensorData = kinectManager.GetSensorData(); } if (sensorData == null || sensorData.sensorInterface == null) { throw new Exception("Background removal cannot be started, because KinectManager is missing or not initialized."); } // ensure the needed dlls are in place and speech recognition is available for this interface bool bNeedRestart = false; bool bSuccess = sensorData.sensorInterface.IsBackgroundRemovalAvailable(ref bNeedRestart); if (bSuccess) { if (bNeedRestart) { KinectInterop.RestartLevel(gameObject, "BR"); return; } } else { string sInterfaceName = sensorData.sensorInterface.GetType().Name; throw new Exception(sInterfaceName + ": Background removal is not supported!"); } // Initialize the background removal bSuccess = sensorData.sensorInterface.InitBackgroundRemoval(sensorData, colorCameraResolution); if (!bSuccess) { throw new Exception("Background removal could not be initialized."); } // create the foreground image and alpha-image int imageLength = sensorData.sensorInterface.GetForegroundFrameLength(sensorData, colorCameraResolution); foregroundImage = new byte[imageLength]; // get the needed rectangle Rect neededFgRect = sensorData.sensorInterface.GetForegroundFrameRect(sensorData, colorCameraResolution); // create the foreground texture foregroundTex = new Texture2D((int)neededFgRect.width, (int)neededFgRect.height, TextureFormat.RGBA32, false); // calculate the foreground rectangle if (foregroundCamera != null) { Rect cameraRect = foregroundCamera.pixelRect; float rectHeight = cameraRect.height; float rectWidth = cameraRect.width; if (rectWidth > rectHeight) rectWidth = Mathf.Round(rectHeight * neededFgRect.width / neededFgRect.height); else rectHeight = Mathf.Round(rectWidth * neededFgRect.height / neededFgRect.width); foregroundRect = new Rect((cameraRect.width - rectWidth) / 2, cameraRect.height - (cameraRect.height - rectHeight) / 2, rectWidth, -rectHeight); } instance = this; isBrInited = true; //DontDestroyOnLoad(gameObject); } catch (DllNotFoundException ex) { Debug.LogError(ex.ToString()); if (debugText != null) debugText.GetComponent<GUIText>().text = "Please check the Kinect and BR-Library installations."; } catch (Exception ex) { Debug.LogError(ex.ToString()); if (debugText != null) debugText.GetComponent<GUIText>().text = ex.Message; } }