unity3d 屏幕截图 核心代码

//其实关键代码就是这三句,先取到目标的贴图,然后转成字节,最后字节输出成图片:
Texture2D tex = (Texture2D)target.renderer.material.mainTexture;
byte[] byt = tex.EncodeToPNG ();
System.IO.File.WriteAllBytes ("Assets/" + textureName + ".png", byt); 

private void SaveMenuWindow (int windowID)
 {
  if (!saved) {
   GUILayout.Label ("Name:");
   textureName = GUILayout.TextField (textureName);
   if (GUILayout.Button ("Save")) {
    Texture2D tex = (Texture2D)target.renderer.material.mainTexture;
    byte[] byt = tex.EncodeToPNG ();
    
    System.IO.File.WriteAllBytes ("Assets/" + textureName + ".png", byt);
    saved = true;
   }
  } else {
   GUILayout.Label (Application.dataPath + "/" + textureName + ".png");
   if (GUILayout.Button ("Close")) {
    objectMenuIndex = 0;
    showSaveMenu = false;
    saved = false;
   }
  }
 }

你可能感兴趣的:(unity3d 屏幕截图 核心代码)