Created
May 12, 2019 14:18
-
-
Save johans2/5d7c897adb64e4435e77dc0d297443bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
// http://framebunker.com/blog/viewing-image-effects-in-the-scene-view/ | |
public class SceneViewFilter : MonoBehaviour { | |
#if UNITY_EDITOR | |
bool hasChanged = false; | |
public virtual void OnValidate() { | |
hasChanged = true; | |
} | |
static SceneViewFilter() { | |
SceneView.onSceneGUIDelegate += CheckMe; | |
} | |
static void CheckMe(SceneView sv) { | |
if(Event.current.type != EventType.Layout) | |
return; | |
if(!Camera.main) | |
return; | |
// Get a list of everything on the main camera that should be synced. | |
SceneViewFilter[] cameraFilters = Camera.main.GetComponents<SceneViewFilter>(); | |
SceneViewFilter[] sceneFilters = sv.camera.GetComponents<SceneViewFilter>(); | |
// Let's see if the lists are different lengths or something like that. | |
// If so, we simply destroy all scene filters and recreate from maincame | |
if(cameraFilters.Length != sceneFilters.Length) { | |
Recreate(sv); | |
return; | |
} | |
for(int i = 0; i < cameraFilters.Length; i++) { | |
if(cameraFilters[i].GetType() != sceneFilters[i].GetType()) { | |
Recreate(sv); | |
return; | |
} | |
} | |
// Ok, WHICH filters, or their order hasn't changed. | |
// Let's copy all settings for any filter that has changed. | |
for(int i = 0; i < cameraFilters.Length; i++) | |
if(cameraFilters[i].hasChanged || sceneFilters[i].enabled != cameraFilters[i].enabled) { | |
EditorUtility.CopySerialized(cameraFilters[i], sceneFilters[i]); | |
cameraFilters[i].hasChanged = false; | |
} | |
} | |
static void Recreate(SceneView sv) { | |
SceneViewFilter filter; | |
while(filter = sv.camera.GetComponent<SceneViewFilter>()) | |
DestroyImmediate(filter); | |
foreach(SceneViewFilter f in Camera.main.GetComponents<SceneViewFilter>()) { | |
SceneViewFilter newFilter = sv.camera.gameObject.AddComponent(f.GetType()) as SceneViewFilter; | |
EditorUtility.CopySerialized(f, newFilter); | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment