Пример #1
0
        public static void TakeSceneViewSnapshot([NotNull] SceneView sceneView, OnTextureReady onTextureReadyCallback)
        {
            // Focus the sceneView and wait until it has fully focused
            sceneView.Focus();

            void WaitForFocus()
            {
                if (!sceneView.hasFocus)
                {
                    EditorApplication.delayCall += WaitForFocus;
                    return;
                }

                // Prepare the sceneView region the
                const int tabHeight      = 19; // Taken from DockArea, which is internal, and the value is also internal.
                var       cameraRect     = sceneView.cameraRect;
                var       offsetPosition = sceneView.position.position + cameraRect.position + new Vector2(0, tabHeight);
                var       region         = new Rect(offsetPosition, cameraRect.size);

                // Take the snapshot
                var texture = TakeScreenSnapshot(region);

                // Execute callback
                onTextureReadyCallback(texture);
            }

            EditorApplication.delayCall += WaitForFocus;
        }
Пример #2
0
        public static void TakeGameViewSnapshot([NotNull] EditorWindow gameView, OnTextureReady onTextureReadyCallback)
        {
            // Focus the game view (there is no need to wait for focus here
            // as the snapshot won't happen until there is a render
            gameView.Focus();

            var textureAssetPath = AssetDatabase.GenerateUniqueAssetPath("Assets/game-view-texture.png");

            ScreenCapture.CaptureScreenshot(textureAssetPath);

            void WaitForSnapshotReady()
            {
                // Wait if the file is not ready
                if (!File.Exists(textureAssetPath))
                {
                    EditorApplication.delayCall += WaitForSnapshotReady;
                    return;
                }

                // Import the texture a first time
                AssetDatabase.ImportAsset(textureAssetPath, ImportAssetOptions.ForceSynchronousImport);

                // Then get the importer for the texture
                var textureImporter = AssetImporter.GetAtPath(textureAssetPath) as TextureImporter;

                if (textureImporter == null)
                {
                    return;
                }

                // Set it readable
                var oldIsReadable = textureImporter.isReadable;

                textureImporter.isReadable = true;

                // Re-import it again, then load it
                AssetDatabase.ImportAsset(textureAssetPath, ImportAssetOptions.ForceSynchronousImport);
                var textureAsset = AssetDatabase.LoadAssetAtPath <Texture2D>(textureAssetPath);

                textureImporter.isReadable = oldIsReadable;
                if (!textureAsset)
                {
                    Debug.LogFormat(LogType.Warning, LogOption.NoStacktrace, null, "Texture asset unavailable.");
                    return;
                }

                // Copy the texture since we are going to delete the asset
                var textureCopy = new Texture2D(textureAsset.width, textureAsset.height);

                EditorUtility.CopySerialized(textureAsset, textureCopy);

                // Delete the original texture asset
                AssetDatabase.DeleteAsset(textureAssetPath);

                onTextureReadyCallback(textureCopy);
            }

            EditorApplication.delayCall += WaitForSnapshotReady;
        }
Пример #3
0
        public static void RequestUITexture(string textureName, OnTextureReady onTextureReady, bool persist = false)
        {
            if (string.IsNullOrEmpty(textureName))
            {
                return;
            }
            if (onTextureReady == null)
            {
                return;
            }

            Texture texture = null;

            if (persistTextureDict.TryGetValue(textureName, out texture))
            {
                if (texture == null)
                {
                    persistTextureDict.Remove(textureName);
                }
            }
            else if (nonPersistTextureDict.TryGetValue(textureName, out texture))
            {
                if (texture == null)
                {
                    nonPersistTextureDict.Remove(textureName);
                }
            }

            if (texture == null)
            {
                // TODO: manage the Load method later.
                texture = Resources.Load <Texture>(GetDefaultTexturePath(textureName));

                if (persist)
                {
                    persistTextureDict.Add(textureName, texture);
                }
                else
                {
                    nonPersistTextureDict.Add(textureName, texture);
                }
            }

            onTextureReady(textureName, texture);
        }
Пример #4
0
        public void UpdateWebVideoTexture()
        {
            if (isError)
            {
                return;
            }

            switch (WebVideoPlayerGetState(videoPlayerId))
            {
            case (int)VideoState.ERROR:
                Debug.LogError(WebVideoPlayerGetError(videoPlayerId));
                isError = true;
                break;

            case (int)VideoState.READY:
                if (!initialized)
                {
                    initialized      = true;
                    texture          = CreateTexture(WebVideoPlayerGetWidth(videoPlayerId), WebVideoPlayerGetHeight(videoPlayerId));
                    textureNativePtr = texture.GetNativeTexturePtr();
                    OnTextureReady?.Invoke(texture);
                }
                break;

            case (int)VideoState.PLAYING:
                if (shouldBePlaying && visible)
                {
                    int width  = WebVideoPlayerGetWidth(videoPlayerId);
                    int height = WebVideoPlayerGetHeight(videoPlayerId);
                    if (texture.width != width || texture.height != height)
                    {
                        if (texture.Resize(width, height))
                        {
                            texture.Apply();
                            textureNativePtr = texture.GetNativeTexturePtr();
                        }
                    }
                    if (texture.width > 0 && texture.height > 0)
                    {
                        WebVideoPlayerTextureUpdate(videoPlayerId, textureNativePtr, isWebGL1);
                    }
                }
                break;
            }
        }