-
Notifications
You must be signed in to change notification settings - Fork 5
/
CameraPreviewWindow.cs
110 lines (91 loc) · 2.48 KB
/
CameraPreviewWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
#if UNITY_EDITOR
public class CameraPreviewWindow : UnityEditor.EditorWindow
{
public Camera mCamera;
public RenderTexture mRenderTexture;
// Add menu named "My Window" to the Window menu
[MenuItem ("NewChromantics/Camera Preview Window/Create Camera Preview Window")]
static void Init ()
{
// create a new window
var NewWindow = EditorWindow.CreateInstance<CameraPreviewWindow>();
NewWindow.ShowUtility();
}
void UpdateRenderTexture(Rect WindowSize)
{
// render camera
if (mCamera == null) {
mRenderTexture = null;
} else {
if (mRenderTexture != null && (mRenderTexture.width != WindowSize.width || mRenderTexture.height != WindowSize.height)) {
mRenderTexture = null;
}
if (mRenderTexture == null) {
int CameraRenderTextureDepth = (mCamera.targetTexture != null) ? mCamera.targetTexture.depth : 24;
mRenderTexture = new RenderTexture ( (int)WindowSize.width, (int)WindowSize.height, CameraRenderTextureDepth);
}
var OldTarget = mCamera.targetTexture;
mCamera.targetTexture = mRenderTexture;
mCamera.Render ();
mCamera.targetTexture = OldTarget;
}
}
void SnapWindowToDisplay()
{
if ( mCamera == null )
return;
// get disp
}
void OnGUI()
{
var Rect = this.position;
Rect.x = 0;
Rect.y = 0;
// draw camera selection
{
int SelectedCamera = 0;
List<string> CameraNames = new List<string> ();
CameraNames.Add ("null");
// add the other cameras in the scene
foreach (Camera Cam in Camera.allCameras) {
CameraNames.Add (Cam.name);
if (mCamera == Cam)
SelectedCamera = CameraNames.Count - 1;
}
int NewSelectedCamera = EditorGUILayout.Popup ( SelectedCamera, CameraNames.ToArray(), GUILayout.ExpandWidth (true));
// changed camera
if (NewSelectedCamera != SelectedCamera) {
if (NewSelectedCamera == 0) {
mCamera = null;
} else {
mCamera = Camera.allCameras [NewSelectedCamera -1];
}
}
}
{
if (GUILayout.Button ("Snap to display")) {
SnapWindowToDisplay ();
}
}
// draw the content!
{
// move the rect to go past all the previous controls
var LastBottom = GUILayoutUtility.GetLastRect ().yMax;
Rect.y += LastBottom;
Rect.height -= LastBottom;
UpdateRenderTexture (Rect);
if (mRenderTexture == null) {
GUI.Box (Rect, "No camera");
} else {
GUI.DrawTexture (Rect, mRenderTexture);
}
}
}
}
#endif