Skip to content

Instantly share code, notes, and snippets.

@Naphier
Created July 27, 2015 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Naphier/048d56f0ed139b47cc41 to your computer and use it in GitHub Desktop.
Save Naphier/048d56f0ed139b47cc41 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
public class MasterCam : ScriptableObject
{
[MenuItem("GameObject/MasterCam/Set all cameras")]
static void CopyToAllCameras()
{
bool go = EditorUtility.DisplayDialog("MasterCam", "***This operations cannot be undone!***\nThis will copy the current scene's camera settings to every camera in every scene included in Build Settings.", "OK", "Cancel");
if (go)
{
string startScenePath = EditorApplication.currentScene;
if (EditorBuildSettings.scenes.Length == 1 && EditorBuildSettings.scenes[0].path == startScenePath)
{
EditorUtility.DisplayDialog("MasterCam", "No scenes modified! Make sure you have at least one scene that is not the current scene in your build settings.", "OK");
return;
}
else if (EditorBuildSettings.scenes.Length <= 0)
{
EditorUtility.DisplayDialog("MasterCam", "No scenes modified! Make sure you have at least one scene in your build settings.", "OK");
return;
}
else
{
//grab a copy of the camera
GameObject[] masterCamGO = GetThisSceneCameraGO(true , false);
MasterCamera masterCamera = new MasterCamera();
masterCamera.SetValues(masterCamGO[0]);
EditorApplication.SaveScene(startScenePath);
if (masterCamGO.Length <= 0)
{
EditorUtility.DisplayDialog("MasterCam", "No scenes modified! Could not find a main camera to copy from. Please select the camera in the current scenet to copy from or ensure the scene has at least one camera tagged as MainCamera.", "OK");
return;
}
else
{
string dOut = "";
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
{
if (scene.path != startScenePath)
{
string name = scene.path.Substring(scene.path.LastIndexOf('/') + 1);
name = name.Substring(0, name.Length - 6);
EditorApplication.OpenScene(scene.path);
dOut += name + "\n";
GameObject[] thisSceneCams = GetThisSceneCameraGO(false, false);
foreach (GameObject camGo in thisSceneCams)
{
masterCamera.CopyValuesTo(camGo);
}
EditorApplication.SaveScene(scene.path);
}
}
string sceneName = startScenePath.Substring(startScenePath.LastIndexOf('/') + 1);
sceneName = sceneName.Substring(0, sceneName.Length - 6);
EditorApplication.OpenScene(startScenePath);
dOut = "Done!\nStart scene: " + sceneName + "\nModified Scenes:\n" + dOut;
EditorUtility.DisplayDialog("MasterCam", dOut , "Done");
}
}
}
}
static GameObject[] GetThisSceneCameraGO(bool isMasterCam , bool mainCameraOnly)
{
if (mainCameraOnly || isMasterCam)
{
GameObject[] cams = new GameObject[1];
if (Selection.activeGameObject != null)
{
if (Selection.activeGameObject.GetComponent<Camera>() != null)
{
cams[0] = Selection.activeGameObject;
return cams;
}
}
GameObject thisSceneCamera = GameObject.FindGameObjectWithTag("MainCamera");
if (thisSceneCamera != null)
{
cams[0] = thisSceneCamera;
return cams;
}
if (Camera.main != null)
{
cams[0] = Camera.main.gameObject;
return cams;
}
else
return new GameObject[0];
}
else
{
Camera[] allCams = GameObject.FindObjectsOfType<Camera>();
GameObject[] cams = new GameObject[allCams.Length];
for (int i = 0; i < allCams.Length; i++)
{
cams[i] = allCams[i].gameObject;
}
return cams;
}
}
}
public class MasterCamera
{
public GameObject cameraGo;
public CameraClearFlags clearFlags;
public Color background;
public int cullingMask;
public bool orthographic;
public float size;
public float farClipPlane;
public float nearClipPlane;
public Rect viewportRect;
public float depth;
public RenderingPath renderingPath;
public RenderTexture targetTexture;
public bool occlusionCulling;
public bool hdr;
public int layer;
public string tag;
public void SetValues(GameObject cameraGameObject)
{
this.cameraGo = cameraGameObject;
Camera cam = cameraGameObject.GetComponent<Camera>();
if (!cam)
{
Debug.LogError("Cannot find camera component on " + cameraGameObject.name);
return;
}
clearFlags = cam.clearFlags;
background = cam.backgroundColor;
cullingMask = cam.cullingMask;
orthographic = cam.orthographic;
if (orthographic)
size = cam.orthographicSize;
else
size = cam.fieldOfView;
farClipPlane = cam.farClipPlane;
nearClipPlane = cam.nearClipPlane;
viewportRect = cam.rect;
depth = cam.depth;
renderingPath = cam.renderingPath;
targetTexture = cam.targetTexture;
occlusionCulling = cam.useOcclusionCulling;
hdr = cam.hdr;
layer = cameraGameObject.layer;
tag = cameraGameObject.tag;
}
public void CopyValuesTo(GameObject cameraGoToCopyTo)
{
Camera cam = cameraGoToCopyTo.GetComponent<Camera>();
if (!cam)
{
Debug.LogError("Cannot find camera component on " + cameraGoToCopyTo.name);
return;
}
cam.clearFlags = clearFlags;
cam.backgroundColor = background;
cam.cullingMask = cullingMask;
cam.orthographic = orthographic;
if (orthographic)
cam.orthographicSize = size;
else
cam.fieldOfView = size;
cam.farClipPlane = farClipPlane;
cam.nearClipPlane = nearClipPlane;
cam.rect = viewportRect;
cam.depth = depth;
cam.renderingPath = renderingPath;
cam.targetTexture = targetTexture;
cam.useOcclusionCulling = occlusionCulling;
cam.hdr = hdr;
cameraGoToCopyTo.layer = layer;
cameraGoToCopyTo.tag = tag;
}
}
@Naphier
Copy link
Author

Naphier commented Jul 27, 2015

A simple editor script that takes the current scene's camera settings and applies it to all cameras in every scene.
I'm considering extended functionality such as copying all other components on the camera (scripts, effects, etc). Please comment your interest and ideas here and I'll extend it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment