当前位置:   article > 正文

Unity Texture和Base64互转_unity base64转texture

unity base64转texture


一、TextureToBase64

	/// <summary>
	/// TextureToBase64普通转换
	/// </summary>
	/// <param name="texture2D"></param>
	/// <returns></returns>
	public static string TextureToBase64(Texture2D texture2D)
	{
		byte[] imageData = texture2D.EncodeToJPG();
		string baser64 = Convert.ToBase64String(imageData);
		return baser64;
	}

    /// <summary>
    /// 图片文件普通转换
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public static string TextureFileBase64(string filePath)
    {
        FileStream filestream = new FileStream(filePath, FileMode.Open);
        byte[] arr = new byte[filestream.Length];
        filestream.Read(arr, 0, (int)filestream.Length);
        string baser64 = Convert.ToBase64String(arr);
        filestream.Close();
        return baser64;
    }

	/// <summary>
	/// TextureToBase64网页解析专用
	/// </summary>
	/// <param name="texture2D"></param>
	/// <returns></returns>
	public static string TextureToBase64_Prefixing(Texture2D texture2D)
	{
		return "data:image/jpeg;base64," + TextureToBase64(texture2D);
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

二、Base64ToTexture

    public static Texture2D Base64ToTexture(string imageData)
    {
        Texture2D pic = new Texture2D(190, 190, TextureFormat.RGBA32, false);
        byte[] data = System.Convert.FromBase64String(imageData);
        pic.LoadImage(data);
        return pic;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/118497
推荐阅读
相关标签
  

闽ICP备14008679号