Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How capture png texture from a gameobject

Discussion in 'General Graphics' started by Fabbbh, Jul 28, 2016.

  1. Fabbbh

    Fabbbh

    Joined:
    Apr 18, 2016
    Posts:
    2
    Hi,

    Because i'm using so many "complex" gameobject on my project, i have an overhead glitch.
    When i reduced the prefab complexity, overhead disappear.

    so, i'd like to capture the graphic result of my gameobjects to a png and generate a gameobject without childs.

    I read a lot of documents about creating prefab and export texture but i don't understand how capture a root gameobject texture which is the graphics result of my gameobject assembly.

    With this code, i can generate a png file but it doesn't contain my gameobject texture (perhaps a camera glitch on my code). I'd like write the exact gameobject png (with transparency) which is showed on my game windows.

    Code (CSharp):
    1.         GameObject selection = GameObject.Find ("Items");
    2.         int width = 128;
    3.         int height = 128;
    4.         Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
    5.         Rect sel = new Rect ();
    6.         sel.width = width;
    7.         sel.height = height;
    8.         sel.x = selection.transform.position.x;
    9.         sel.y = selection.transform.position.y;
    10.  
    11.         tex.ReadPixels (sel, 0, 0);
    12.  
    13.         byte[] bytes = tex.EncodeToPNG ();
    14.         File.WriteAllBytes ("Assets/Test/picture.png", bytes);
     
  2. namtm84

    namtm84

    Joined:
    Jul 28, 2016
    Posts:
    4
    the position of transform is world position , not the postition in screen.
    i think you can use Camera.WorldToScreenPoint instead
     
    PuzzlElite likes this.
  3. Fabbbh

    Fabbbh

    Joined:
    Apr 18, 2016
    Posts:
    2
    Hi namtm84,

    Thanks for your answer, i have the same problem with camera position, i have the entire picture (with background).
    Is there a method to catch the GameObject texture element (only the element and transparency) ?

    ReadPixels method works fine.
     
    Last edited: Jul 28, 2016