I’m trying to create a button on Unity, that will give the command to create screenshot of my screen.
I think I have the file directory,
private/var/root/Media/DCIM/100APPLE (or ~/Media/DCIM/100APPLE)
Correct me if I’m wrong please. Doing my research I have the following code but if it is the right code I’m still unsure where to put the file directory. All help is appreciated.
// Saves screenshot as PNG file.
import System.IO ;
private var gui :GUITexture;
function Start(){
gui=GetComponent(GUITexture);
}
function Update(){
var count = Input.touchCount;
for (var i: int =0; i<count; i++){
var touch :Touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began gui.HitTest(touch.position))
{
////////////////////
UploadPNG ();
//////////////////
//change color on tap
guiTexture.color = Color(.1,.3,.4);
}
if (touch.phase == TouchPhase.Ended)
{
guiTexture.color = Color.grey;
}
}
}
// Take a shot immediately
function UploadPNG () {
*// We should only read the screen bufferafter rendering is complete
*yield WaitForEndOfFrame();
*// Create a texture the size of the screen, RGB24 format
*var width = Screen.width;
*var height = Screen.height;
*var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
*// Read screen contents into the texture
*tex.ReadPixels (Rect(0, 0, width, height), 0, 0);
*tex.Apply ();
*// Encode texture into PNG
*var bytes = tex.EncodeToPNG();
*Destroy (tex);
*// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + “private/var/root/Media/DCIM/100APPLE (or ~/Media/DCIM/100APPLE)/Screenshot.png”, bytes);
}
3 of the top 5 iOS threads are about the same thing, why did you feel the need to make a new thread?
File.WriteAllBytes("private/var/root/Media/DCIM/100APPLE/" + "[The name I want the screenshot to be named].png", bytes);
Don’t put Application.dataPath, as that’s a different path entirely than the path to photos, and attaching the path to your app onto the path to photos just creates a really mangled path.
anon_67940662:
3 of the top 5 iOS threads are about the same thing, why did you feel the need to make a new thread?
File.WriteAllBytes("private/var/root/Media/DCIM/100APPLE/" + "[The name I want the screenshot to be named].png", bytes);
Don’t put Application.dataPath, as that’s a different path entirely than the path to photos, and attaching the path to your app onto the path to photos just creates a really mangled path.
I have tried what you said replacing the bottom text, and still I’m having errors that Unity wont build to iOS
You’ll need to explain what these errors are before anyone can assist in solving these errors.
What steps do you take?
What errors are reported?
If clicking on them takes you to a specific line in a script, can you provide at least a pertinent segment of the related script?
// Saves screenshot as PNG file.
import System.IO ;
private var gui :GUITexture;
function Start(){
gui=GetComponent(GUITexture);
}
function Update(){
var count = Input.touchCount;
for (var i: int =0; i<count; i++){
var touch :Touch = Input.GetTouch(i);
if (touch.phase == TouchPhase.Began gui.HitTest(touch.position))
{
////////////////////
UploadPNG ();
//////////////////
//change color on tap
guiTexture.color = Color(.1,.3,.4);
}
if (touch.phase == TouchPhase.Ended)
{
guiTexture.color = Color.grey;
}
}
}
// Take a shot immediately
function UploadPNG () {
*// We should only read the screen bufferafter rendering is complete
*yield WaitForEndOfFrame();
*// Create a texture the size of the screen, RGB24 format
*var width = Screen.width;
*var height = Screen.height;
*var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
*// Read screen contents into the texture
*tex.ReadPixels (Rect(0, 0, width, height), 0, 0);
*tex.Apply ();
*// Encode texture into PNG
*var bytes = tex.EncodeToPNG();
*Destroy (tex);
*// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(“private/var/root/Media/DCIM/100APPLE/” + “[Hashkey Threads Pixels].png”, bytes);
is what I have in the GUI Button script and these are the errors,
Exception: Error building Player because scripts had compiler errors
UnityEditor.BuildPlayerWindow.BuildPlayerWithDefaultSettings (Boolean askForBuildLocation, BuildOptions forceOptions)
UnityEditor.BuildPlayerWindow.GUIBuildButtons (Boolean enableBuildButton, Boolean enableBuildAndRunButton, Boolean canInstallInBuildFolder)
UnityEditor.BuildPlayerWindow.ShowBuildTargetSettings ()
UnityEditor.BuildPlayerWindow.OnGUI ()
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[ ] parameters, System.Globalization.CultureInfo culture)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[ ] parameters)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj)
UnityEditor.HostView.Invoke (System.String methodName)
UnityEditor.HostView.OnGUI ()
I am reasonably new to Unity/Javascript but have an understand about code, but I’ve been check in the deep end!
I appreciate your help.
That errors is because there are other errors in the list.
Go to Window-> Console and list ALL the errors. The one you listed merely points out there are others.
If that’s the script as it is, * * is not valid syntax.
We removed the * * when putting it into the script, My other machine as the Unity Pro License so I’m unable to test it till tomorrow,
I shall continue this post tomorrow and would appreciate your input Ntero.
Many thanks.