示例#1
0
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.T))
            {
#if !UNITY_EDITOR
                if (transparent)
                {
                    Camera.main.backgroundColor = Color.green;
                    WindowApi.Opacify();
                }
                else
                {
                    Camera.main.clearFlags      = CameraClearFlags.Color;
                    Camera.main.backgroundColor = Color.clear;
                    WindowApi.Transparentize();
                }
#endif

                transparent = !transparent;
            }
            else if (Input.GetKeyDown(KeyCode.E))
            {
                if (monitoring)
                {
                    MouseApi.Release();
                    KeyApi.Release();
                }
                else
                {
                    MouseApi.ObserveLeftMouseDown(() => Debug.Log("left mouse down"));
                    MouseApi.ObserveLeftMouseUp(() => Debug.Log("left mouse up"));
                    MouseApi.ObserveRightMouseDown(() => Debug.Log("right mouse down"));
                    MouseApi.ObserveRightMouseUp(() => Debug.Log("right mouse up"));

                    // accessibility permission is required
                    KeyApi.ObserveKeyDown(code => Debug.Log($"key down: {code}"));
                    KeyApi.ObserveKeyUp(code => Debug.Log($"key up: {code}"));
                }

                monitoring = !monitoring;
            }
        }
示例#2
0
        private void Start()
        {
            _receivedMessageHandler.Commands.Subscribe(message =>
            {
                switch (message.Command)
                {
                case MessageCommandNames.ChangeBackground:
                    {
                        if (message.ToBoolean())
                        {
                            if (canvasObject != null)
                            {
                                canvasObject.SetActive(false);
                            }

                            UnityEngine.Camera.main.backgroundColor = Color.clear;
                            shouldSetTransparent = true;
                        }
                        else
                        {
                            WindowApi.Opacify();
                            shouldSetTransparent = false;
                            if (canvasObject != null)
                            {
                                canvasObject.SetActive(true);
                            }

                            UnityEngine.Camera.main.backgroundColor = Color.green;
                        }

                        break;
                    }

                case MessageCommandNames.ChangeBackgroundImageDirection:
                    {
                        widthAll = message.ToBoolean();
                        break;
                    }
                }
            });

            try
            {
                var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/background.jpg";
                if (File.Exists(path))
                {
                    var data = File.ReadAllBytes(path);

                    canvasObject = new GameObject("Background");
                    var imageObject = new GameObject("Image");
                    imageObject.transform.SetParent(canvasObject.transform);

                    var canvas = canvasObject.AddComponent <Canvas>();

                    canvas.renderMode  = RenderMode.ScreenSpaceCamera;
                    canvas.worldCamera = UnityEngine.Camera.main;
                    canvasObject.AddComponent <CanvasScaler>();
                    rawImage = imageObject.AddComponent <RawImage>();

                    //画像読み込み
                    var tex = new Texture2D(1, 1);
                    tex.LoadImage(data);

                    //ImageObjectのサイズを画像サイズに合わせる(Canvasをはみ出た分はトリム=中央クロップになる)
                    imageSize   = rawImage.GetComponent <RectTransform>();
                    aspectRatio = (float)tex.height / tex.width;
                    if (widthAll)
                    {
                        imageSize.sizeDelta = new Vector2(Screen.width, Screen.width * aspectRatio);
                    }
                    else
                    {
                        imageSize.sizeDelta = new Vector2(Screen.height / aspectRatio, Screen.height);
                    }

                    rawImage.texture = tex;
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }
        }