Advertisement
napland

MasterCam - copy camera to all scenes

Jul 27th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.98 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class MasterCam : ScriptableObject
  5. {
  6.     [MenuItem("GameObject/MasterCam/Set all cameras")]
  7.     static void CopyToAllCameras()
  8.     {
  9.         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");
  10.         if (go)
  11.         {
  12.             string startScenePath = EditorApplication.currentScene;
  13.             if (EditorBuildSettings.scenes.Length == 1 && EditorBuildSettings.scenes[0].path == startScenePath)
  14.             {
  15.                 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");
  16.                 return;
  17.             }
  18.             else if (EditorBuildSettings.scenes.Length <= 0)
  19.             {
  20.                 EditorUtility.DisplayDialog("MasterCam", "No scenes modified! Make sure you have at least one scene in your build settings.", "OK");
  21.                 return;
  22.             }
  23.             else
  24.             {
  25.                 //grab a copy of the camera
  26.                 GameObject[] masterCamGO = GetThisSceneCameraGO(true , false);
  27.                 MasterCamera masterCamera = new MasterCamera();
  28.                 masterCamera.SetValues(masterCamGO[0]);
  29.                 EditorApplication.SaveScene(startScenePath);
  30.  
  31.                 if (masterCamGO.Length <= 0)
  32.                 {
  33.                     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");
  34.                     return;
  35.                 }
  36.                 else
  37.                 {
  38.                    
  39.                     string dOut = "";
  40.                     foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
  41.                     {
  42.                         if (scene.path != startScenePath)
  43.                         {
  44.                             string name = scene.path.Substring(scene.path.LastIndexOf('/') + 1);
  45.                             name = name.Substring(0, name.Length - 6);
  46.                             EditorApplication.OpenScene(scene.path);
  47.                             dOut += name + "\n";
  48.  
  49.                             GameObject[] thisSceneCams = GetThisSceneCameraGO(false, false);
  50.                             foreach (GameObject camGo in thisSceneCams)
  51.                             {
  52.                                 masterCamera.CopyValuesTo(camGo);
  53.                             }
  54.                             EditorApplication.SaveScene(scene.path);
  55.                         }
  56.                     }
  57.  
  58.                     string sceneName = startScenePath.Substring(startScenePath.LastIndexOf('/') + 1);
  59.                     sceneName = sceneName.Substring(0, sceneName.Length - 6);
  60.                     EditorApplication.OpenScene(startScenePath);
  61.                     dOut = "Done!\nStart scene: " + sceneName + "\nModified Scenes:\n" + dOut;
  62.                     EditorUtility.DisplayDialog("MasterCam", dOut , "Done");
  63.                 }
  64.             }
  65.         }
  66.     }
  67.  
  68.  
  69.     static GameObject[] GetThisSceneCameraGO(bool isMasterCam , bool mainCameraOnly)
  70.     {
  71.         if (mainCameraOnly || isMasterCam)
  72.         {
  73.             GameObject[] cams = new GameObject[1];
  74.             if (Selection.activeGameObject != null)
  75.             {
  76.                 if (Selection.activeGameObject.GetComponent<Camera>() != null)
  77.                 {
  78.                     cams[0] = Selection.activeGameObject;
  79.                     return cams;
  80.                 }
  81.             }
  82.  
  83.             GameObject thisSceneCamera = GameObject.FindGameObjectWithTag("MainCamera");
  84.             if (thisSceneCamera != null)
  85.             {
  86.                 cams[0] = thisSceneCamera;
  87.                 return cams;
  88.             }
  89.  
  90.             if (Camera.main != null)
  91.             {
  92.                 cams[0] = Camera.main.gameObject;
  93.                 return cams;
  94.             }
  95.             else
  96.                 return new GameObject[0];
  97.         }
  98.         else
  99.         {
  100.             Camera[] allCams = GameObject.FindObjectsOfType<Camera>();
  101.             GameObject[] cams = new GameObject[allCams.Length];
  102.             for (int i = 0; i < allCams.Length; i++)
  103.             {
  104.                 cams[i] = allCams[i].gameObject;
  105.             }
  106.             return cams;
  107.         }
  108.  
  109.     }
  110. }
  111.  
  112. public class MasterCamera
  113. {
  114.     public GameObject cameraGo;
  115.     public CameraClearFlags clearFlags;
  116.     public Color background;
  117.     public int cullingMask;
  118.     public bool orthographic;
  119.     public float size;
  120.     public float farClipPlane;
  121.     public float nearClipPlane;
  122.     public Rect viewportRect;
  123.     public float depth;
  124.     public RenderingPath renderingPath;
  125.     public RenderTexture targetTexture;
  126.     public bool occlusionCulling;
  127.     public bool hdr;
  128.     public int layer;
  129.     public string tag;
  130.  
  131.     public void SetValues(GameObject cameraGameObject)
  132.     {
  133.         this.cameraGo = cameraGameObject;
  134.         Camera cam = cameraGameObject.GetComponent<Camera>();
  135.         if (!cam)
  136.         {
  137.             Debug.LogError("Cannot find camera component on " + cameraGameObject.name);
  138.             return;
  139.         }
  140.  
  141.         clearFlags = cam.clearFlags;
  142.         background = cam.backgroundColor;
  143.         cullingMask = cam.cullingMask;
  144.         orthographic = cam.orthographic;
  145.         if (orthographic)
  146.             size = cam.orthographicSize;
  147.         else
  148.             size = cam.fieldOfView;
  149.  
  150.         farClipPlane = cam.farClipPlane;
  151.         nearClipPlane = cam.nearClipPlane;
  152.         viewportRect = cam.rect;
  153.         depth = cam.depth;
  154.         renderingPath = cam.renderingPath;
  155.         targetTexture = cam.targetTexture;
  156.         occlusionCulling = cam.useOcclusionCulling;
  157.         hdr = cam.hdr;
  158.  
  159.         layer = cameraGameObject.layer;
  160.         tag = cameraGameObject.tag;
  161.     }
  162.  
  163.     public void CopyValuesTo(GameObject cameraGoToCopyTo)
  164.     {
  165.         Camera cam = cameraGoToCopyTo.GetComponent<Camera>();
  166.         if (!cam)
  167.         {
  168.             Debug.LogError("Cannot find camera component on " + cameraGoToCopyTo.name);
  169.             return;
  170.         }
  171.  
  172.         cam.clearFlags = clearFlags;
  173.         cam.backgroundColor = background;
  174.         cam.cullingMask = cullingMask;
  175.         cam.orthographic = orthographic;
  176.         if (orthographic)
  177.             cam.orthographicSize = size;
  178.         else
  179.             cam.fieldOfView = size;
  180.  
  181.         cam.farClipPlane = farClipPlane;
  182.         cam.nearClipPlane = nearClipPlane;
  183.         cam.rect = viewportRect;
  184.         cam.depth = depth;
  185.         cam.renderingPath = renderingPath;
  186.         cam.targetTexture = targetTexture;
  187.         cam.useOcclusionCulling = occlusionCulling;
  188.         cam.hdr = hdr;
  189.  
  190.         cameraGoToCopyTo.layer = layer;
  191.         cameraGoToCopyTo.tag = tag;
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement