Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question File.WriteAllBytes?

Discussion in 'Visual Scripting' started by Welfarecheck, Feb 14, 2023.

  1. Welfarecheck

    Welfarecheck

    Joined:
    Jun 14, 2020
    Posts:
    117
    Is it possible to access WriteAllBytes from VS? I need to write a Texture2D to a png every 60 seconds. Or, it is possible to invoke a script from VS to accomplish the same goal?
     
  2. Welfarecheck

    Welfarecheck

    Joined:
    Jun 14, 2020
    Posts:
    117
    Got it figured out.

    Can invoke the script from VS.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Texture2DSaveSystem : MonoBehaviour {
    6.      public Texture2D Tex2D;
    7.  
    8.      public string ScreenShotName() {
    9.          return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png",
    10.                               Application.dataPath,
    11.                               Tex2D.width, Tex2D.height,
    12.                               System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
    13.      }
    14.      public void TakeHiResShot() {
    15.  
    16.              byte[] bytes = Tex2D.EncodeToPNG();
    17.              string filename = ScreenShotName();
    18.              System.IO.File.WriteAllBytes(filename, bytes);
    19.          
    20.      }
    21.  
    22. }
    upload_2023-2-14_10-53-22.png