devNotes 11-12-16 RT Create Button Save Settings Preset

 


// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor 
{
    public static void Main(string[] args) 
    {
        foreach(string path in args) 
        {
            if(File.Exists(path)) 
            {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) 
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else 
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }        
        }        
    }


    // Process all files in the directory passed in, recurse on any directories 
    // that are found, and process the files they contain.
    public static void ProcessDirectory(string targetDirectory) 
    {
        // Process the list of files found in the directory.
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory.
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach(string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }

    // Insert logic for processing found files here.
    public static void ProcessFile(string path) 
    {
        Console.WriteLine("Processed file '{0}'.", path);	    
    }
}

How to take a screenshot and apply it as a texture to a sprite?

So what I’m trying to do is take a render from the screen, turn it into a Texture2D and apply it to a sprite using Sprite.Create();


 // instance of the camera im getting on Awake()
 private Camera _camera;
 
 private Texture2D _screenShot;
 
     private IEnumerator TakeScreenShot()
         {
             yield return new WaitForEndOfFrame();
     
             RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
             _camera.targetTexture = rt;
             _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;
             Destroy(rt);
     
             string filename = ScreenShotName(resWidth, resHeight);
     
             //byte[] bytes = _screenShot.EncodeToPNG();
             //System.IO.File.WriteAllBytes(filename, bytes);
 
             Debug.Log(string.Format("Took screenshot to: {0}", filename));
     
             Sprite tempSprite = Sprite.Create(_screenShot,new Rect(0,0,resWidth,resHeight),new Vector2(0,0));
             GameObject.Find("SpriteObject").GetComponent<SpriteRenderer>().sprite = tempSprite;
         }

All it does is change the SpriteObject.sprite color to white. But when I try to save the content of the screenshot it turns out that there is actualy an image and not just white.

Answer by PAEvenson

Perhaps you are missing apply after you read the pixels?

_screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
 
 _screenShot.Apply(); //Add this?
 
 _camera.targetTexture = null;

DUUUUDE! You nailed it! Thank you so much!

http://answers.unity3d.com/questions/733240/how-to-take-a-screenshot-and-apply-it-as-a-texture.html

 

AstroLabe

7a6b41255

detailed-pen-drawings-kerby-rosanes-11

detailed-pen-drawings-kerby-rosanes-7