need help (sorry for my english level).
Game have file generator (make levels files). I update asset, add functional of save jpg to file. After that game read file and make image from this.
My steps:
- settings.imageCode = coloredTexture.EncodeToJPG();
- Save to file:
if (settings.rastr)
{
bytes.AddRange(System.BitConverter.GetBytes(settings.imageCode.Length));
bytes.AddRange(settings.imageCode);
}
- Start game. Game read levels files (part about img):
private byte[] GetSprite() private byte[] GetSprite()
{
int length = GetNextInt();
ArraySegment<byte> byteImage = new ArraySegment<byte>(levelFileBytes, byteIndex, length);
byteIndex = length;
int count = byteImage.Count;
return byteImage.Array;
}
- Try create sprite and add it to GO:
private void CreateRastrImage(LevelFileData levelFileData)
{
GameObject imageObj = new GameObject("Rastr");
RectTransform containerRectT = imageObj.AddComponent<RectTransform>();
containerRectT.SetParent(transform, false);
// Expand to fill
containerRectT.anchoredPosition = Vector2.zero;
containerRectT.anchorMin = Vector2.zero;
containerRectT.anchorMax = Vector2.one;
containerRectT.offsetMax = Vector2.zero;
containerRectT.offsetMin = Vector2.zero;
Image sprite = imageObj.AddComponent<Image>();
Texture2D spriteColor = new Texture2D(levelFileData.imageWidth, levelFileData.imageHeight);
//ImageConversion.LoadImage(spriteColor, levelFileData.rastrBytes);
spriteColor.LoadImage(levelFileData.rastrBytes);
Sprite fileSptire = Sprite.Create(spriteColor, new Rect(0f, 0f, spriteColor.width, spriteColor.height), new Vector2(0f, 0f));
sprite.sprite = fileSptire;
}
- Result:red question mark
About result, source img its this jpg file: picture Use jpg (RGB24), also try another TextureFormat. I think problem with reading (img dont taken full), but I check length with stop poing in VS. Help guys and thanks you!
CodePudding user response:
Problem was in ArraySegment. When I try return byteImage.Array - its return full array (levelFileBytes). My solution now:
private byte[] GetSprite()
{
int length = GetNextInt();
byte[] imageBytes = new byte[length];
for(int i = 0; i < length; i )
{
imageBytes[i] = levelFileBytes[byteIndex i];
}
byteIndex = length;
return imageBytes;
}