Last active
November 19, 2018 13:13
-
-
Save nicloay/0dbf2a5764496154495719c86ef48a7c to your computer and use it in GitHub Desktop.
Add this component to the same gameobject as paintcraft CanvasController
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 PaintCraft.Canvas.Configs; | |
using PaintCraft.Controllers; | |
using UnityEngine; | |
using UnityEngine.Assertions; | |
[RequireComponent(typeof(CanvasController))] | |
public class SecondCameraCtrl : MonoBehaviour | |
{ | |
[SerializeField] private Color _clearColor = Color.black; | |
private CanvasController _canvas; | |
private Camera _seconCamera; | |
void Awake() | |
{ | |
if (Display.displays.Length < 2) | |
{ | |
DestroyImmediate(this); | |
return; | |
} | |
if (!Display.displays[1].active) | |
{ | |
Display.displays[1].Activate(); | |
} | |
} | |
void Start(){ | |
_canvas = GetComponent<CanvasController>(); | |
Assert.IsNotNull(_canvas); | |
InitCameraParams(); | |
_canvas.OnPageChange += OnPageChange; | |
if (_canvas.PageConfig != null) | |
{ | |
OnPageChange(_canvas.PageConfig); | |
} | |
} | |
private void InitCameraParams() | |
{ | |
_seconCamera = GetComponent<Camera>(); | |
if (_seconCamera == null) | |
{ | |
_seconCamera = gameObject.AddComponent<Camera>(); | |
} | |
_seconCamera.targetDisplay = 1; | |
_seconCamera.targetTexture = null; | |
_seconCamera.orthographic = true; | |
_seconCamera.clearFlags = CameraClearFlags.Color; | |
_seconCamera.backgroundColor = _clearColor; | |
_seconCamera.cullingMask = int.MaxValue;// _canvas.BrushLayerId| _canvas.TempRenderLayerId | 8; //8 is a canvas | |
} | |
private void OnDestroy() | |
{ | |
if (_canvas != null) | |
{ | |
_canvas.OnPageChange -= OnPageChange; | |
} | |
} | |
private void OnPageChange(PageConfig pageConfig) | |
{ | |
_seconCamera.orthographicSize = pageConfig.GetSize().y / 2.0f; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment