Unity3D下用脚本存储Texture2D到硬盘jpg文件

   public void writeCaptureDataToFile(Texture2D texture, string dataPath, string filename)
    {
        string path_full = dataPath + filename + ".jpg";

        // 存入jpg文件
        StartCoroutine(saveTexture2DtoFile(texture, path_full));
    }

    // 保存图片到指定目录
    private IEnumerator saveTexture2DtoFile(Texture2D texture, string path, Action callback=null)
    {
        //等待渲染线程结束  
        yield return new WaitForEndOfFrame();

        byte[] textureData = texture.EncodeToJPG();
        System.IO.File.WriteAllBytes(path, textureData);

        callback?.Invoke(null);
        Debug.Log("图片文件写入完毕:" + path);
    } 
  

 

你可能感兴趣的:(Unity3D)