Скрипт сохраняет изображение, потом загружает из папки.
В редакторе работает прекрасно. На андроиде вместо изображения, просто белая картинка. В чем проблема?
Resource.Load не канает, т.к. новые файлы не видит.
код сохранения
Синтаксис:
Используется csharp
void SaveSpriteToFile(Texture2D texture)
{
string fromPath = Path.Combine($"{Application.dataPath}/Resources", texture.name + ".png");
using (FileStream fs = new FileStream(fromPath, FileMode.Create))
{
using (BinaryWriter writer = new BinaryWriter(fs))
{
var bytes = texture.EncodeToPNG();
writer.Write(bytes);
writer.Close();
}
}
}
{
string fromPath = Path.Combine($"{Application.dataPath}/Resources", texture.name + ".png");
using (FileStream fs = new FileStream(fromPath, FileMode.Create))
{
using (BinaryWriter writer = new BinaryWriter(fs))
{
var bytes = texture.EncodeToPNG();
writer.Write(bytes);
writer.Close();
}
}
}
код загрузки
Синтаксис:
Используется csharp
Sprite LoadImage(string imageName, int width, int height)
{
byte[] imgData;
try
{
imgData = File.ReadAllBytes(Path.Combine($"{Application.dataPath}/Resources", imageName + ".png"));
}
catch (FileNotFoundException)
{
return null;
}
Texture2D texture = new Texture2D(width, height, TextureFormat.DXT5Crunched, true);
texture.Compress(true);
texture.name = $"{imageName}";
texture.filterMode = FilterMode.Trilinear;
texture.Apply();
texture.LoadImage(imgData);
Sprite sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f);
sprite.name = $"{imageName}";
return sprite;
}
{
byte[] imgData;
try
{
imgData = File.ReadAllBytes(Path.Combine($"{Application.dataPath}/Resources", imageName + ".png"));
}
catch (FileNotFoundException)
{
return null;
}
Texture2D texture = new Texture2D(width, height, TextureFormat.DXT5Crunched, true);
texture.Compress(true);
texture.name = $"{imageName}";
texture.filterMode = FilterMode.Trilinear;
texture.Apply();
texture.LoadImage(imgData);
Sprite sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f);
sprite.name = $"{imageName}";
return sprite;
}