Beispiel #1
0
        Camera GetFirstActiveCamera()
        {
            // Get last camera on the list
            for (int i = 0; i < m_Cameras.Count; i++)
            {
                if (m_Cameras[i].m_Active && m_Cameras[i].m_Camera != null && m_Cameras[i].m_Camera.enabled == true)
                {
                    return(m_Cameras[i].m_Camera);
                }
            }
            // If not cameras on the list, get the active camera for display 1 with the higher depth
            Camera[] cameras = GameObject.FindObjectsOfType <Camera>();
            Camera   best    = null;

            foreach (Camera cam in cameras)
            {
#if (UNITY_5_4_OR_NEWER)
                if (cam.enabled == true && cam.targetDisplay == 0)
                {
#else
                if (cam.enabled == true)
                {
#endif
                    if (best == null || cam.depth < best.depth)
                    {
                        best = cam;
                    }
                }
            }
            if (best != null)
            {
                return(best);
            }
            // Return camera tagged as MainCamera
            return(Camera.main);
        }

        Camera GetLastActiveCamera()
        {
            // Get last camera on the list
            for (int i = m_Cameras.Count - 1; i >= 0; i--)
            {
                if (m_Cameras[i].m_Active && m_Cameras[i].m_Camera != null && m_Cameras[i].m_Camera.enabled == true)
                {
                    return(m_Cameras[i].m_Camera);
                }
            }
            // If not cameras on the list, get the active camera for display 1 with the higher depth
            Camera[] cameras = GameObject.FindObjectsOfType <Camera>();
            Camera   best    = null;

            foreach (Camera cam in cameras)
            {
#if (UNITY_5_4_OR_NEWER)
                if (cam.enabled == true && cam.targetDisplay == 0)
                {
#else
                if (cam.enabled == true)
                {
#endif
                    if (best == null || cam.depth > best.depth)
                    {
                        best = cam;
                    }
                }
            }
            if (best != null)
            {
                return(best);
            }
            // Return camera tagged as MainCamera
            return(Camera.main);
        }

        IEnumerator MultiDisplayCopyRenderBufferToTextureCoroutine(Texture2D targetTexture)
        {
            Camera lastCamera = GetLastActiveCamera();

            // On multi display we need to wait for the last camera to capture to be rendered
            if (lastCamera != null)
            {
                // Add a capture camera component and start the capture process
                if (lastCamera.GetComponent <MultiDisplayCameraCapture>() == null)
                {
                    lastCamera.gameObject.AddComponent <MultiDisplayCameraCapture>();
                }
                MultiDisplayCameraCapture capture = lastCamera.GetComponent <MultiDisplayCameraCapture>();
                capture.CaptureCamera(targetTexture);
                // Wait for capture
                while (!capture.CopyIsOver())
                {
                    yield return(null);
                }
                // Clean
                GameObject.DestroyImmediate(capture);
            }
            else
            {
                // Just read the actual render buffer
                CopyScreenToTexture(targetTexture);
            }
        }

        #endregion


        #region TEXTURE

        Texture2D GetOrCreateTexture(ScreenshotResolution resolution, ColorFormat colorFormat, bool noScale = false)
        {
            // Compute real dimensions
            int width  = noScale ? resolution.m_Width : resolution.ComputeTargetWidth();
            int height = noScale ? resolution.m_Height : resolution.ComputeTargetHeight();

            // Create texture if needed
            if (resolution.m_Texture == null)
            {
                resolution.m_Texture = new Texture2D(width, height, colorFormat == ColorFormat.RGBA ? TextureFormat.ARGB32 : TextureFormat.RGB24, false);
            }
            else if (resolution.m_Texture.width != width || resolution.m_Texture.height != height ||
                     (resolution.m_Texture.format == TextureFormat.ARGB32 && colorFormat != ColorFormat.RGBA) ||
                     (resolution.m_Texture.format == TextureFormat.RGB24 && colorFormat != ColorFormat.RGB))
            {
                resolution.m_Texture.Resize(width, height, colorFormat == ColorFormat.RGBA ? TextureFormat.ARGB32 : TextureFormat.RGB24, false);
            }

            return(resolution.m_Texture);
        }

        RenderTexture GetOrCreateRenderTexture(ScreenshotResolution resolution, int antiAliasing = 0)
        {
            // Compute real resolutions
            int width  = resolution.ComputeTargetWidth();
            int height = resolution.ComputeTargetHeight();

            // Create render texture if needed
            if (!m_RenderTextureCache.ContainsKey(resolution) || m_RenderTextureCache[resolution] == null ||
                m_RenderTextureCache[resolution].width != width || m_RenderTextureCache[resolution].height != height ||
                m_RenderTextureCache[resolution].antiAliasing != antiAliasing)
            {
                m_RenderTextureCache[resolution] = new RenderTexture(width, height, 32, RenderTextureFormat.ARGB32);
                if (antiAliasing != 0)
                {
                    m_RenderTextureCache[resolution].antiAliasing = antiAliasing;
                }
            }

            return(m_RenderTextureCache[resolution]);
        }
        Camera GetFirstActiveCamera()
        {
            // Get last camera on the list
            for (int i = 0; i < m_Cameras.Count; i++)
            {
                if (m_Cameras[i].m_Active && m_Cameras[i].m_Camera != null && m_Cameras[i].m_Camera.enabled == true)
                {
                    return(m_Cameras[i].m_Camera);
                }
            }
            // If not cameras on the list, get the active camera for display 1 with the higher depth
            Camera[] cameras = GameObject.FindObjectsOfType <Camera>();
            Camera   best    = null;

            foreach (Camera cam in cameras)
            {
#if (UNITY_5_4_OR_NEWER)
                if (cam.enabled == true && cam.targetDisplay == 0)
                {
#else
                if (cam.enabled == true)
                {
#endif
                    if (best == null || cam.depth < best.depth)
                    {
                        best = cam;
                    }
                }
            }
            if (best != null)
            {
                return(best);
            }
            // Return camera tagged as MainCamera
            return(Camera.main);
        }

        Camera GetLastActiveCamera()
        {
            // Get last camera on the list
            for (int i = m_Cameras.Count - 1; i >= 0; i--)
            {
                if (m_Cameras[i].m_Active && m_Cameras[i].m_Camera != null && m_Cameras[i].m_Camera.enabled == true)
                {
                    return(m_Cameras[i].m_Camera);
                }
            }
            // If not cameras on the list, get the active camera for display 1 with the higher depth
            Camera[] cameras = GameObject.FindObjectsOfType <Camera>();
            Camera   best    = null;

            foreach (Camera cam in cameras)
            {
#if (UNITY_5_4_OR_NEWER)
                if (cam.enabled == true && cam.targetDisplay == 0)
                {
#else
                if (cam.enabled == true)
                {
#endif
                    if (best == null || cam.depth > best.depth)
                    {
                        best = cam;
                    }
                }
            }
            if (best != null)
            {
                return(best);
            }
            // Return camera tagged as MainCamera
            return(Camera.main);
        }

        IEnumerator MultiDisplayCopyRenderBufferToTextureCoroutine(Texture2D targetTexture)
        {
            // We get the last camera on the list or the last rendered camera
            Camera cameraToCapture = GetLastActiveCamera();

            // On multi display we need to wait for the last camera to capture to be rendered
            if (cameraToCapture != null)
            {
                MultiDisplayCameraCapture captureMultiCam = cameraToCapture.GetComponent <MultiDisplayCameraCapture>();
                // Add a capture camera component and start the capture process
                if (captureMultiCam == null)
                {
                    captureMultiCam = cameraToCapture.gameObject.AddComponent <MultiDisplayCameraCapture>();
                }
                captureMultiCam.CaptureCamera(targetTexture);
                // Wait for capture
                while (!captureMultiCam.CopyIsOver())
                {
                    yield return(null);
                }
                // Clean
                GameObject.DestroyImmediate(captureMultiCam);
            }
            else
            {
                // Just read the actual render buffer
                CaptureScreenToTexture(targetTexture);
            }
        }

        #endregion


        #region TEXTURE


        public static Texture2D GetOrCreateTexture(ScreenshotResolution resolution, ColorFormat colorFormat, bool noScale = false)