Version: 2019.2
LanguageEnglish
  • C#

ImageConversion.LoadImage

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable);

Parameters

dataThe byte array containing the image data to load.
markNonReadableSet to false by default, pass true to optionally mark the texture as non-readable.
texThe texture to load the image into.

Returns

bool Returns true if the data can be loaded, false otherwise.

Description

Loads PNG/JPG image byte array into a texture.

This function replaces texture contents with new image data. After LoadImage, texture size and format might change. JPG files are loaded into RGB24 format, PNG files are loaded into RGBA32 format. If texture format before calling LoadImage is DXT1 or DXT5, then the loaded image will be DXT-compressed (into DXT1 for JPG images and DXT5 for PNG images). If the platform is not able to perform runtime compression then false will be returned.

Texture will be uploaded to the GPU automatically; there's no need to call Apply.

using UnityEngine;

public class ExampleClass : MonoBehaviour { // Load a .jpg or .png file by adding .bytes extensions to the file // and dragging it on the imageAsset variable. public TextAsset imageAsset; public void Start() { // Create a texture. Texture size does not matter, since // LoadImage will replace with with incoming image size. Texture2D tex = new Texture2D(2, 2); tex.LoadImage(imageAsset.bytes); GetComponent<Renderer>().material.mainTexture = tex; } }
using UnityEngine;

public class ExampleScript : MonoBehaviour { public void Start() { // Create a texture. Texture size does not matter, since // LoadImage will replace with with incoming image size. Texture2D tex = new Texture2D(2, 2); // A small 64x64 Unity logo encoded into a PNG. byte[] pngBytes = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x8F, 0x02, 0x2E, 0x02, 0x00, 0x00, 0x01, 0x57, 0x49, 0x44, 0x41,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F, 0x82, 0x89, 0xB4, 0x1A, 0x7F, 0xE5, 0xA3, 0x5F, 0x46, 0xAC, 0xC6, 0x0F, 0xBA, 0x96, 0x1C, 0xB1, 0x12, 0x7F, 0xE5, 0x33, 0x26, 0xD2, 0x4A, 0xFC, 0x41, 0x07, 0xB3, 0x09, 0x56, 0xE1, 0xE3, 0xA1, 0xB8, 0xCE, 0x3C, 0x5A, 0x81, 0xBF, 0xDA, 0x43, 0x73, 0x75, 0xA6, 0x71, 0xDB, 0x7F, 0x0F, 0x29, 0x24, 0x82, 0x95, 0x08, 0xAF, 0x21, 0xC9, 0x9E, 0xBD, 0x50, 0xE6, 0x47, 0x12, 0x38, 0xEF, 0x03, 0x78, 0x11, 0x2B, 0x61, 0xB4, 0xA5, 0x0B, 0xE8, 0x21, 0xE8, 0x26, 0xEA, 0x69, 0xAC, 0x17, 0x12, 0x0F, 0x73, 0x21, 0x29, 0xA5, 0x2C, 0x37, 0x93, 0xDE, 0xCE, 0xFA, 0x85, 0xA2, 0x5F, 0x69, 0xFA, 0xA5, 0xAA, 0x5F, 0xEB, 0xFA, 0xC3, 0xA2, 0x3F, 0x6D, 0xFA, 0xE3, 0xAA, 0x3F, 0xEF, 0xFA, 0x80, 0xA1, 0x8F, 0x38, 0x04, 0xE2, 0x8B, 0xD7, 0x43, 0x96, 0x3E, 0xE6, 0xE9, 0x83, 0x26, 0xE1, 0xC2, 0xA8, 0x2B, 0x0C, 0xDB, 0xC2, 0xB8, 0x2F, 0x2C, 0x1C, 0xC2, 0xCA, 0x23, 0x2D, 0x5D, 0xFA, 0xDA, 0xA7, 0x2F, 0x9E, 0xFA, 0xEA, 0xAB, 0x2F, 0xDF, 0xF2, 0xFA, 0xFF, 0x01, 0x1A, 0x18, 0x53, 0x83, 0xC1, 0x4E, 0x14, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82, }; // Load data into the texture. tex.LoadImage(pngBytes); // Assign texture to renderer's material. GetComponent<Renderer>().material.mainTexture = tex; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961