t1 / TFDContents / Assets / KinectDemos / OverlayDemo / Scripts / PhotoShooter.cs @ 3
이력 | 보기 | 이력해설 | 다운로드 (4.9 KB)
1 |
using UnityEngine; |
---|---|
2 |
using System.Collections; |
3 |
using System.IO; |
4 |
using System; |
5 |
|
6 |
|
7 |
public class PhotoShooter : MonoBehaviour |
8 |
{ |
9 |
[Tooltip("Camera that will be used to render the background.")] |
10 |
public Camera backroundCamera; |
11 |
|
12 |
[Tooltip("Camera that will be used to overlay the 3D-objects over the background.")] |
13 |
public Camera foreroundCamera; |
14 |
|
15 |
[Tooltip("Array of sprite transforms that will be used for displaying the countdown until image shot.")] |
16 |
public Transform[] countdown; |
17 |
|
18 |
[Tooltip("GUI-Text used to display information messages.")] |
19 |
public GUIText infoText; |
20 |
|
21 |
|
22 |
/// <summary> |
23 |
/// Counts down (from 3 for instance), then takes a picture and opens it |
24 |
/// </summary> |
25 |
public void CountdownAndMakePhoto() |
26 |
{ |
27 |
StartCoroutine(CoCountdownAndMakePhoto()); |
28 |
} |
29 |
|
30 |
|
31 |
// counts down (from 3 for instance), then takes a picture and opens it |
32 |
private IEnumerator CoCountdownAndMakePhoto() |
33 |
{ |
34 |
if (countdown != null && countdown.Length > 0) |
35 |
{ |
36 |
for(int i = 0; i < countdown.Length; i++) |
37 |
{ |
38 |
if (countdown [i]) |
39 |
countdown [i].gameObject.SetActive(true); |
40 |
|
41 |
yield return new WaitForSeconds(1.0f); |
42 |
|
43 |
if (countdown [i]) |
44 |
countdown [i].gameObject.SetActive(false); |
45 |
} |
46 |
} |
47 |
|
48 |
MakePhoto(); |
49 |
yield return null; |
50 |
} |
51 |
|
52 |
|
53 |
/// <summary> |
54 |
/// Saves the screen image as png picture, and then opens the saved file. |
55 |
/// </summary> |
56 |
public void MakePhoto() |
57 |
{ |
58 |
MakePhoto(true); |
59 |
} |
60 |
|
61 |
/// <summary> |
62 |
/// Saves the screen image as png picture, and optionally opens the saved file. |
63 |
/// </summary> |
64 |
/// <returns>The file name.</returns> |
65 |
/// <param name="openIt">If set to <c>true</c> opens the saved file.</param> |
66 |
public string MakePhoto(bool openIt) |
67 |
{ |
68 |
int resWidth = Screen.width; |
69 |
int resHeight = Screen.height; |
70 |
|
71 |
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture |
72 |
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24); |
73 |
|
74 |
// hide the info-text, if any |
75 |
if (infoText) |
76 |
{ |
77 |
infoText.text = string.Empty; |
78 |
} |
79 |
|
80 |
// render background and foreground cameras |
81 |
if (backroundCamera && backroundCamera.enabled) |
82 |
{ |
83 |
backroundCamera.targetTexture = rt; |
84 |
backroundCamera.Render(); |
85 |
backroundCamera.targetTexture = null; |
86 |
} |
87 |
|
88 |
if (foreroundCamera && foreroundCamera.enabled) |
89 |
{ |
90 |
foreroundCamera.targetTexture = rt; |
91 |
foreroundCamera.Render(); |
92 |
foreroundCamera.targetTexture = null; |
93 |
} |
94 |
|
95 |
// get the screenshot |
96 |
RenderTexture prevActiveTex = RenderTexture.active; |
97 |
RenderTexture.active = rt; |
98 |
|
99 |
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); |
100 |
|
101 |
// clean-up |
102 |
RenderTexture.active = prevActiveTex; |
103 |
Destroy(rt); |
104 |
|
105 |
byte[] btScreenShot = screenShot.EncodeToJPG(); |
106 |
Destroy(screenShot); |
107 |
|
108 |
#if !UNITY_WSA |
109 |
// save the screenshot as jpeg file |
110 |
string sDirName = Application.persistentDataPath + "/Screenshots"; |
111 |
if (!Directory.Exists(sDirName)) |
112 |
Directory.CreateDirectory (sDirName); |
113 |
|
114 |
string sFileName = sDirName + "/" + string.Format ("{0:F0}", Time.realtimeSinceStartup * 10f) + ".jpg"; |
115 |
File.WriteAllBytes(sFileName, btScreenShot); |
116 |
|
117 |
Debug.Log("Photo saved to: " + sFileName); |
118 |
if (infoText) |
119 |
{ |
120 |
infoText.text = "Saved to: " + sFileName; |
121 |
} |
122 |
|
123 |
// open file |
124 |
if(openIt) |
125 |
{ |
126 |
System.Diagnostics.Process.Start(sFileName); |
127 |
} |
128 |
|
129 |
return sFileName; |
130 |
#elif NETFX_CORE |
131 |
System.Threading.Tasks.Task<string> task = null; |
132 |
|
133 |
string sFileName = string.Format("{0:F0}", Time.realtimeSinceStartup * 10f) + ".jpg"; |
134 |
string sFileUrl = string.Empty; // "ms-appdata:///local/" + sFileName; |
135 |
|
136 |
// UnityEngine.WSA.Application.InvokeOnUIThread(() => |
137 |
// { |
138 |
task = SaveImageFileAsync(sFileName, btScreenShot, openIt); |
139 |
// }, true); |
140 |
|
141 |
while (task != null && !task.IsCompleted && !task.IsFaulted) |
142 |
{ |
143 |
task.Wait(100); |
144 |
} |
145 |
|
146 |
if (task != null) |
147 |
{ |
148 |
if (task == null) |
149 |
throw new Exception("Could not create task for SaveImageFileAsync()"); |
150 |
else if (task.IsFaulted) |
151 |
throw task.Exception; |
152 |
|
153 |
sFileUrl = task.Result; |
154 |
Debug.Log(sFileUrl); |
155 |
} |
156 |
|
157 |
return sFileUrl; |
158 |
#else |
159 |
return string.Empty; |
160 |
#endif |
161 |
} |
162 |
|
163 |
#if NETFX_CORE |
164 |
private async System.Threading.Tasks.Task<string> SaveImageFileAsync(string imageFileName, byte[] btImageContent, bool openIt) |
165 |
{ |
166 |
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder; |
167 |
Windows.Storage.StorageFile imageFile = await storageFolder.CreateFileAsync(imageFileName, |
168 |
Windows.Storage.CreationCollisionOption.ReplaceExisting); |
169 |
|
170 |
await Windows.Storage.FileIO.WriteBytesAsync(imageFile, btImageContent); |
171 |
|
172 |
if(openIt) |
173 |
{ |
174 |
await Windows.System.Launcher.LaunchFileAsync(imageFile); |
175 |
} |
176 |
|
177 |
return imageFile.Path; |
178 |
} |
179 |
#endif |
180 |
|
181 |
} |