using System; using System.Collections; using System.Collections.Generic; using UC; using UnityEditor; using UnityEngine; namespace Spout { public class SceneViewSpoutSender : EditorWindow { static SceneViewSpoutSender exampleWindow; [MenuItem("Window/UC/Scene View Spout Sender")] static void Open() { if (exampleWindow == null) { exampleWindow = CreateInstance(); } exampleWindow.Show(); } public string sharingName = "UnitySceneSender"; public Spout.TextureFormat textureFormat = Spout.TextureFormat.DXGI_FORMAT_R8G8B8A8_UNORM; [AutoUI] public bool yFlip = true; [SerializeField] [Header("Automatic")] RenderTexture output; private bool senderIsCreated; private const int _createAttempts = 5; private int _attempts = 0; private bool textureSizeHasChanged = false; [SerializeField] private Shader m_Shader; public Shader shader { get { if (m_Shader == null) m_Shader = Shader.Find("Unlit/AlphaPremultiplied"); return m_Shader; } } private Material m_Material; public Material material { get { if (m_Material == null) m_Material = ImageEffectHelper.CheckShaderAndCreateMaterial(shader); return m_Material; } } private void OnEnable() { SceneView.onSceneGUIDelegate += getTextureFromSceneView; } void getTextureFromSceneView(SceneView view) { RenderTexture s = view.camera.targetTexture; if (s == null) return; bool isValid = (output != null) && s.width == output.width && s.height == output.height; if (!isValid) { if (output != null) output.Release(); output = new RenderTexture(s.width, s.height, s.depth, RenderTextureFormat.ARGB32); output.Create(); } textureSizeHasChanged = !isValid; material.SetInt("yFlip", yFlip ? 1 : 0); GL.sRGBWrite = (QualitySettings.activeColorSpace == ColorSpace.Linear); Graphics.Blit(s, output, material); GL.sRGBWrite = false; } void OnGUI() { //エスケープを押した時に閉じる if (Event.current.keyCode == KeyCode.Escape) { exampleWindow.Close(); } //sharingName = EditorGUILayout.TextField(sharingName); if(Application.isPlaying == false) EditorGUILayout.HelpBox("Play the scene to enable spout", MessageType.Warning, true); } void OnDisable() { SceneView.onSceneGUIDelegate -= getTextureFromSceneView; _CloseSender(); if (m_Material != null) DestroyImmediate(m_Material); m_Material = null; } void Update() { if (Application.isPlaying == false) return; if (output == null) return; if (textureSizeHasChanged) _CloseSender(); if (senderIsCreated) Spout.instance.UpdateSender(sharingName, output); else _CreateSender(output); } private void _CreateSender(Texture texture) { if (texture == null) return; if (!Spout.instance.enabled) return; if (!senderIsCreated) { Debug.Log("Sender is not created, creating one"); senderIsCreated = Spout.instance.CreateSender(sharingName, texture, (int)textureFormat); } _attempts++; if (_attempts > _createAttempts) Debug.LogWarning(System.String.Format("There are problems with creating the sender {0}. Please check your settings or restart Unity.", sharingName)); Spout.instance.OnSenderStopped -= OnSenderStoppedDelegate; Spout.instance.OnSenderStopped += OnSenderStoppedDelegate; Spout.instance.OnAllSendersStopped -= OnAllSendersStoppedDelegate; Spout.instance.OnAllSendersStopped += OnAllSendersStoppedDelegate; Spout.instance.OnEnabled -= _OnSpoutEnabled; Spout.instance.OnEnabled += _OnSpoutEnabled; } private void _OnSpoutEnabled() { //Debug.Log("SpoutSender._OnSpoutEnabled"); //if (enabled) { //force a reconnection // enabled = !enabled; // enabled = !enabled; } } private void _CloseSender() { Debug.Log("SpoutSender._CloseSender:" + sharingName); if (senderIsCreated) Spout.CloseSender(sharingName); _CloseSenderCleanUpData(); } private void OnSenderStoppedDelegate(object sender, TextureShareEventArgs e) { //Debug.Log("SpoutSender.OnSenderStoppedDelegate:"+e.sharingName); if (e.sharingName == sharingName) { _CloseSenderCleanUpData(); } } private void OnAllSendersStoppedDelegate() { _CloseSenderCleanUpData(); } private void _CloseSenderCleanUpData() { senderIsCreated = false; } } }