Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Kalu · Jan 22, 2012 at 12:43 AM · errorscreenshot

System.IO.File error, take a screenShot

I try to take a screen shot when the user presses' k. My worry now is that I get an error about using WriteAllBytes.

Assets / Scripts / Utility Scripts / HiResScreenShots.cs (37.40): error CS0117: System.IO.File 'does not Contain a definition for WriteAllBytes'

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 public class HiResScreenShots : MonoBehaviour {
     public int resWidth = 4096;//1600;//2550; 
     public int resHeight = 2232;//990;//3300;
 
     private bool takeHiResShot = false;
 
     public static string ScreenShotName(int width, int height) {
         return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png", 
                              Application.dataPath, 
                              width, height, 
                              System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
     }
 
     public void TakeHiResShot() {
         takeHiResShot = true;
     }
 
     void LateUpdate() {
         takeHiResShot |= Input.GetKeyDown("k");
         if (takeHiResShot) {
             RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
             camera.targetTexture = rt;
             Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
             camera.Render();
             RenderTexture.active = rt;
             screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
             camera.targetTexture = null;
             RenderTexture.active = null; // JC: added to avoid errors
             Destroy(rt);
             byte[] bytes = screenShot.EncodeToPNG();
             string filename = ScreenShotName(resWidth, resHeight);
             //System.IO.File.WriteAllBytes(filename, bytes);
             System.IO.File.WriteAllBytes(Application.dataPath + "/screenshots/screen" + System.DateTime.Now.ToString("dd-MM-yyyy_HH-mm-ss") + ".png", bytes);
             Debug.Log(string.Format("Took screenshot to: {0}", filename));
             takeHiResShot = false;
         }
     }
 }




Need help :s

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jan 22, 2012 at 02:01 AM 0
Share

What version of Unity are you using?

avatar image Kalu · Jan 22, 2012 at 02:36 AM 0
Share

3.3.0f4, i have some problem to download the last one on HotSpot connexion :s

avatar image Eric5h5 · Jan 22, 2012 at 02:47 AM 0
Share

That has WriteAllBytes. Try setting the .net compatibility to 2.0 full.

avatar image Kalu · Jan 22, 2012 at 04:05 PM 0
Share

I'm not sure to understand what setting you are talking about. :s

avatar image Eric5h5 · Jan 22, 2012 at 04:40 PM 0
Share

In the player settings, you have API compatibility, with the choices being .NET 2.0 or .NET 2.0 subset. However I don't think that's it, because I tried it with Unity 3.3 using subset, and it works fine here.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Kalu · Feb 24, 2012 at 02:46 PM

Fixed with :

Unity:

 public IEnumerator take_screen_shot () { 
     
         // We should only read the screen after all rendering is complete yield WaitForEndOfFrame();
         // Create a texture the size of the screen, RGB24 format
         int width = Screen.width;
         int height = Screen.height;
         Texture2D tex = new Texture2D( width-210, height, TextureFormat.RGB24, false );
         // Read screen contents into the texture
         tex.ReadPixels( new Rect(0, 0, width-210, height), 0, 0 );
         tex.Apply();
     
         // Encode texture into PNG
         byte[] bytes = tex.EncodeToPNG();
         Destroy( tex );
     
         // Create a Web Form
         WWWForm form = new WWWForm();
         if(idmeuble == 0){
             int nb1 = Random.Range(9, 99);
             int nb2 = Random.Range(0, 9);
             int nb3 = Random.Range(100, 999);
             string s_idmeuble = nb1.ToString()+nb2.ToString()+nb3.ToString();
             idmeuble = int.Parse(s_idmeuble);
         }
         string s_name = idmeuble + "_-_" + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
         s_name = s_name+".png";
         ref_link_screenshot = s_name;
         form.AddField("frameCount", Time.frameCount.ToString());
         form.AddBinaryData("file", bytes, s_name, "image/png");
     
         // Upload to a cgi script    
         WWW w = new WWW(screenShotURL, form);
         yield return w;
         if (w.error != null){
             Debug.Log(w.error);
             mssg_debug += (w.error);
         }  
         else{
             mssg_debug += "   upload ok     ";
             Main otherScript = GetComponent<Main>();
             otherScript.cur_screenshot_statu = otherScript.screen_shot_done;
             otherScript.b_capture_ok = true;
         }
         if(w.text != null){ mssg_debug += w.text; }
     }

PHP:

 <?php 
     $message = "Error";
     echo $_FILES["file"]["type"];
     if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000000000)) { 
         if ($_FILES["file"]["error"] > 0) { 
             $message = "Error";
             //echo "Return Code: " . $_FILES["file"]["error"] . "";
         } else { 
             //echo "Upload: " . $_FILES["file"]["name"] . ""; 
             //echo "Type: " . $_FILES["file"]["type"] . ""; 
             //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb"; 
             //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "";
 
             if (file_exists("dir_screen_shot/" . $_FILES["file"]["name"]))
             {
                 $message = "Error";
             }
             else
             {
                 move_uploaded_file($_FILES["file"]["tmp_name"], "dir_screen_shot/" . $_FILES["file"]["name"]);
                 $message = "Done";
             }
         }
     } else { $message = "Error"; } //invalide File
     echo $message;
 ?>
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
7

Answer by luizgpa · Jan 22, 2012 at 08:26 PM

Probably the current platform of your project is "Web player", witch doesn't have this method. To change it go to "File-> Build Settings", select "PC and Mac Standalone" and click "Switch Platform".

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kalu · Jan 23, 2012 at 07:26 PM 0
Share

it's a web appli .. :s

avatar image Eric5h5 · Jan 23, 2012 at 07:42 PM 1
Share

@$$anonymous$$alu: There is no local file access in web apps for security reasons.

avatar image Kalu · Jan 24, 2012 at 07:17 AM 0
Share

So how can i take a screen shot? Sorry i already had this problem to editing xml ..

avatar image luizgpa · Jan 24, 2012 at 12:36 PM 2
Share

I don't think Unity can do that directly, but you could use Flash to save the screenshot. The steps would be:

1- encode your byte[] to a string in Base64

2- pass the string to the Flash player via Javascript (browser's javascript, not UnityScript)

3- get the string and decode it to a ByteArray

4- use FileReference.save to ask the user where to save the file

avatar image Kalu · Jan 24, 2012 at 02:02 PM 0
Share

It looks good. I'll try as soon as I have time Thx! I'll tell you if it worked

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SreenShot, Color become a mess 1 Answer

Animation Scripting C# Error 1 Answer

Capture Screenshot not working in WebGL 1 Answer

the problem of touch screen logic 1 Answer

Capturing screenshot with transparent background in 4.3, 2d project 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges