unity 怎样把图片转为base64
时间: 2023-07-01 20:19:04 浏览: 89
在Unity中,可以使用以下C#代码将图片转换为Base64字符串:
```csharp
// 读取图片文件
byte[] imageBytes = File.ReadAllBytes("your_image_path.png");
// 将图片字节数据转换为Base64字符串
string base64String = Convert.ToBase64String(imageBytes);
```
其中,`File.ReadAllBytes`方法用于读取指定路径下的图片文件,并将其转换为字节数据。`Convert.ToBase64String`方法将字节数据转换为Base64字符串。
需要注意的是,由于Base64字符串长度较长,因此可能会增加内存和网络传输开销。在使用时需要根据实际情况进行考虑。
相关问题
使用unity将base64转为图片
在 Unity 中将 base64 编码的字符串转换为图片可以使用以下代码:
```csharp
using System;
using System.IO;
using UnityEngine;
public static class Base64Utils
{
public static Texture2D Base64ToTexture2D(string base64)
{
string[] base64Parts = base64.Split(',');
if (base64Parts.Length != 2)
{
Debug.LogError("Invalid base64 input string!");
return null;
}
string imageType = base64Parts[0].Replace("data:", "").Replace(";base64", "");
if (imageType != "image/png" && imageType != "image/jpeg")
{
Debug.LogError("Unsupported image type: " + imageType);
return null;
}
Texture2D texture = new Texture2D(1, 1);
byte[] imageData = Convert.FromBase64String(base64Parts[1]);
if (texture.LoadImage(imageData))
{
return texture;
}
Debug.LogError("Failed to load image data!");
return null;
}
}
```
这段代码定义了一个 `Base64Utils` 静态类,其中有一个 `Base64ToTexture2D` 方法可以将 base64 编码的字符串转换为 `Texture2D` 对象。这个方法首先分离出 base64 字符串中的图片类型和编码数据,然后根据图片类型创建一个新的 `Texture2D` 对象,并使用 `Convert.FromBase64String` 方法将 base64 编码的字符串解码为字节数组,最后使用 `texture.LoadImage` 方法将字节数组加载到 `Texture2D` 对象中。
你可以在你的代码中使用该静态方法,例如:
```csharp
string base64Str = "data:image/png;base64,iVBORw0KG...QmCC";
Texture2D texture = Base64Utils.Base64ToTexture2D(base64Str);
if (texture != null)
{
// 将 texture 作为图片显示在 UI 或 3D 对象上
}
```
unity 如何将base64转为图片
要将Base64编码的字符串转换为图片,在Unity中可以使用以下代码:
```csharp
using System;
using UnityEngine;
using System.IO;
public static class ImageUtils
{
public static Texture2D Base64ToTexture2D(string base64)
{
byte[] imageBytes = Convert.FromBase64String(base64);
Texture2D texture = new Texture2D(1, 1);
texture.LoadImage(imageBytes);
return texture;
}
public static void SaveTextureToFile(Texture2D texture, string filePath)
{
byte[] bytes = texture.EncodeToPNG();
File.WriteAllBytes(filePath, bytes);
}
}
```
上面的代码中,`Base64ToTexture2D`方法将Base64编码的字符串转换为Texture2D对象,`SaveTextureToFile`方法将Texture2D对象保存为PNG文件。使用时,您可以将Base64字符串传递给`Base64ToTexture2D`方法,然后使用`SaveTextureToFile`方法将其保存为文件。
以下是使用示例:
```csharp
string base64 = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABlBMVEUAAAD/" +
"////fwD///8p5hYAAAAAXRSTlMAQObYZgAAAAFiS0dEAIgFHUgAAAAJcEhZ" +
"cwAADsMAAA7DAcdvqGQAAAAadEVYdFNvZnR3YXJlAFBhaW50Lk5FVCB2My4w" +
"OyBodHRwOi8vd3d3LnBhaW50Lm5ldC9zdmcvOFYzL2JpbmFyeS54bWwA/wAA" +
"AAD//z8L8AAAAJcEhZcwAADsIAAA7CARUoSoAAAAHdElNRQfiBRsJAxv3nYu" +
"AAABgklEQVQoz6WQPUvDUBDFf6XcXRy6R8JZGk2pW4jA6NqoXuA8F4j7NcXw" +
"SugdI2L9ApLq4KJ0i7Qd4q9e5/7gHcxd7Mn3nO+c3W3O5UtJ+LRf2hWTQlE" +
"ZatFxWInx6Yw8GZpkz1KsS2E1e4rVJG4cRgQxltbNvIu+2wFPhW8z0vWZkR" +
"vZVJL0QZ9L5A8aEc5xZg6zj2dXjB7L9mK3HW+0vZlB1m3EY1WzgJlRlj6cD" +
"9zjX5l9L+8hJZmK8eJ8wzESU8vL7+7Iy4tTz8HqQ4eNf7o5zCQ2vCvFvV3R" +
"e0b8HsJTAxIeesb4JYwhk0XHwAAAABJRU5ErkJggg==";
Texture2D texture = ImageUtils.Base64ToTexture2D(base64);
ImageUtils.SaveTextureToFile(texture, Application.dataPath + "/image.png");
```
上面的代码将Base64字符串转换为Texture2D对象,并将其保存为`image.png`文件。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)