Quick Bites 04 : Scene View FX

Quick Bites 04 : Scene View FX

Image effects now available in scene view with 1 simple script.

Simply add the script to any camera you would like to have the image effects on in your scene view and voila!

  • Enable / Disable the component to toggle the image effects in the scene view.
  • Change properties and see the results in scene view in real time
  • All image effects are supported
  • Disables in play mode for performance

 

Download SceneViewFX.cs

using System.Collections.Generic;
using System.Linq;
 
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
 
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class SceneViewFX : MonoBehaviour {
 
#if UNITY_EDITOR
 
	private static SceneViewFX _instance;
	private SceneView sceneView;
	private Camera sceneViewCamera;
	private Camera myCamera;
 
	private void OnEnable() {
		if (_instance != null && _instance != this) {
			Debug.LogError("Cannot add SceneViewFX. Already one active in this scene");
			DestroyImmediate(this);
			return;
		}
		_instance = this;
		sceneViewCamera = GetCamera();
		if (!Application.isPlaying) {
			UpdateComponents();
		}
	}
 
	private void OnDisable() {
		ClearCurrentEffects();
	}
 
 
	private Camera GetCamera() {
		myCamera = camera;
	        sceneView = EditorWindow.GetWindow();
	        return sceneView.camera;
	}
 
	// get components from main game camera.
	private Component[] GetComponents() {
		var result = myCamera.GetComponents();
	        if (result != null && result.Length > 1) {
			// exlude these components:
	        	List excludes = new List();
	        	excludes.Add(myCamera.transform);
	        	excludes.Add(myCamera);
			if (myCamera.GetComponent()) excludes.Add(myCamera.GetComponent());
			if (myCamera.GetComponent("FlareLayer")) excludes.Add(myCamera.GetComponent("FlareLayer"));
			if (myCamera.GetComponent()) excludes.Add(myCamera.GetComponent());
	        	result = result.Except(excludes).ToArray();
        	}
		return result;
	}
 
	public void Update() {
		if (Application.isPlaying) return;
		if (enabled) UpdateComponents();
	}
 
	// update scene view components
	public void UpdateComponents() {
        	if(sceneViewCamera == null) sceneViewCamera = GetCamera();
		if (sceneViewCamera == null) return;
	    	ClearCurrentEffects();
		var components = GetComponents();
        	if (components != null && components.Length > 1) {
		        var cameraGo = sceneViewCamera.gameObject;
			for (int i = 0; i < components.Length; i++) {
                		var c = components[i];
		                var cType = c.GetType();
                		var existing = cameraGo.AddComponent(cType);
		                EditorUtility.CopySerialized(c, existing);
			}
        	}
	    	sceneViewCamera.hdr = myCamera.hdr;
	}
 
	public void ClearCurrentEffects() {
		// clear sceneview camera of any previous components / fx.
		if (sceneViewCamera == null) sceneViewCamera = GetCamera();
	    	Component[] compsOnCam = sceneViewCamera.GetComponents();
		for (int i = compsOnCam.Length-1; i >= 0; i--) {
			// these components are default on the SceneView camera...
			if (sceneViewCamera.GetComponent("HaloLayer") == compsOnCam[i]) continue;
			if (sceneViewCamera.GetComponent("FlareLayer") == compsOnCam[i]) continue;
			if (compsOnCam[i] is Transform) continue;
			if (compsOnCam[i] is Camera) continue;
			DestroyImmediate(compsOnCam[i]);
		}
	}
#endif
}