BakingCamera.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using UnityEngine;
  2. namespace Coffee.UIParticleExtensions
  3. {
  4. [AddComponentMenu("")]
  5. internal class BakingCamera : MonoBehaviour
  6. {
  7. static BakingCamera s_Instance;
  8. private static readonly Vector3 s_OrthoPosition = new Vector3(0, 0, -1000);
  9. private static readonly Quaternion s_OrthoRotation = Quaternion.identity;
  10. #if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
  11. // static BakingCamera s_InstanceForPrefab;
  12. //
  13. // private static BakingCamera InstanceForPrefab
  14. // {
  15. // get
  16. // {
  17. // // If current scene is prefab mode, create OverlayCamera for editor.
  18. // var prefabStage = UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
  19. // if (prefabStage == null || !prefabStage.scene.isLoaded) return null;
  20. // if (s_InstanceForPrefab) return s_InstanceForPrefab;
  21. //
  22. // s_InstanceForPrefab = Create();
  23. // s_InstanceForPrefab.name += " (For Prefab Stage)";
  24. // UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(s_InstanceForPrefab.gameObject, prefabStage.scene);
  25. //
  26. // return s_InstanceForPrefab;
  27. // }
  28. // }
  29. #endif
  30. private static BakingCamera Instance
  31. {
  32. get
  33. {
  34. // #if UNITY_2018_3_OR_NEWER && UNITY_EDITOR
  35. // var inst = InstanceForPrefab;
  36. // if (inst) return inst;
  37. // #endif
  38. // Find instance in scene, or create new one.
  39. return s_Instance
  40. ? s_Instance
  41. : (s_Instance = Create());
  42. }
  43. }
  44. private Camera _camera;
  45. private static BakingCamera Create()
  46. {
  47. var gameObject = new GameObject(typeof(BakingCamera).Name);
  48. // This camera object is just for internal use
  49. gameObject.hideFlags = HideFlags.HideAndDontSave;
  50. var inst = gameObject.AddComponent<BakingCamera>();
  51. inst._camera = gameObject.AddComponent<Camera>();
  52. inst._camera.enabled = false;
  53. inst._camera.orthographic = true;
  54. // Turn camera off because particle mesh baker will use only camera matrix
  55. gameObject.SetActive(false);
  56. return inst;
  57. }
  58. private void Awake()
  59. {
  60. if (this == s_Instance)
  61. DontDestroyOnLoad(gameObject);
  62. }
  63. public static Camera GetCamera(Canvas canvas)
  64. {
  65. if (!canvas) return Camera.main;
  66. canvas = canvas.rootCanvas;
  67. // Adjust camera orthographic size to canvas size
  68. // for canvas-based coordinates of particles' size and speed.
  69. var size = ((RectTransform) canvas.transform).rect.size;
  70. Instance._camera.orthographicSize = Mathf.Max(size.x, size.y) * canvas.scaleFactor;
  71. var camera = canvas.worldCamera;
  72. var transform = Instance.transform;
  73. var rotation = canvas.renderMode != RenderMode.ScreenSpaceOverlay && camera
  74. ? camera.transform.rotation
  75. : s_OrthoRotation;
  76. transform.SetPositionAndRotation(s_OrthoPosition, rotation);
  77. Instance._camera.orthographic = true;
  78. Instance._camera.farClipPlane = 2000f;
  79. return Instance._camera;
  80. }
  81. }
  82. }