public Texture2D LoadTexture(string FilePath) { // Load a PNG or JPG file from disk to a Texture2D // Returns null if load fails Texture2D Tex2D; byte[] FileData; if (FilePath.EndsWith(".dxt")) { Tex2D = LoadTextureDXT.Load(FilePath, false); return(Tex2D); } if (File.Exists(FilePath)) { #if (!UNITY_WSA) || UNITY_EDITOR FileData = File.ReadAllBytes(FilePath); #else FileData = UnityEngine.Windows.File.ReadAllBytes(FilePath); #endif Tex2D = new Texture2D(2, 2); // Create new "empty" texture if (Tex2D.LoadImage(FileData)) // Load the imagedata into the texture (size is set automatically) { return(Tex2D); // If data = readable -> return texture } } return(null); // Return null if load failed }
public Texture2D LoadTexture(string FilePath) { // Load a PNG or JPG file from disk to a Texture2D // Returns null if load fails // PB : LoadImage would be inefficient??? // Compressed all images to compressed format and use LoadRawTextureData? Or load progressively rather than all at once. Texture2D Tex2D; byte[] FileData; if (FilePath.EndsWith(".dxt")) { Tex2D = LoadTextureDXT.Load(FilePath, false); return(Tex2D); } if (File.Exists(FilePath)) { #if (!UNITY_WSA) || UNITY_EDITOR FileData = File.ReadAllBytes(FilePath); #else FileData = UnityEngine.Windows.File.ReadAllBytes(FilePath); #endif Tex2D = new Texture2D(2, 2); // Create new "empty" texture if (Tex2D.LoadImage(FileData)) // Load the imagedata into the texture (size is set automatically) { return(Tex2D); // If data = readable -> return texture } } return(null); // Return null if load failed }
public void SwapImage(int imageId) { #if USE_DXT_IMAGES string imageUrl = MJAData.archive.images[imageId] != null ? MJAData.archive.images[imageId].mediaSources[1].url : "invalid"; imageUrl = MJAData.archive.jsonPath + "/DXT5_s320" + imageUrl.Substring(6) + ".dxt"; // string imageUrl = MJAData.archive.images[imageId] != null ? MJAData.archive.images[imageId].mediaSources[3].url : "invalid"; // imageUrl = MJAData.archive.jsonPath + "/DXT5_l2048" + imageUrl.Substring(6) + ".dxt"; mainImageTex = LoadTextureDXT.Load(imageUrl, true); if (mainImageTex == null || mainImageTex.width < 10) { Debug.Log("Error loading image: " + imageUrl); if (MJAData.defaultImageData != null) { mainImageTex.LoadImage(MJAData.defaultImageData); } } mainImage.GetComponent <MeshRenderer>().material.SetTexture("_MainTex", mainImageTex); #else string imageUrl = MJAData.archive.images[imageId] != null ? MJAData.archive.images[imageId].mediaSources[1].url : "invalid"; if (imageUrl.StartsWith("file://")) { imageUrl = MJAData.archive.jsonPath + "/images" + imageUrl.Substring(6); } if (File.Exists(imageUrl)) { // mainImageTex = new Texture2D(128, 128, TextureFormat.RGB24, false); if (!mainImageTex.LoadImage(File.ReadAllBytes(imageUrl)) || mainImageTex.width < 10) { Debug.Log("Error loading image: " + imageUrl); if (MJAData.defaultImageData != null) { mainImageTex.LoadImage(MJAData.defaultImageData); } } else { // Debug.Log("Success loading image: " + imageUrl); } mainImage.GetComponent <MeshRenderer>().material.SetTexture("_MainTex", mainImageTex); } else if (MJAData.defaultImageData != null) { mainImageTex.LoadImage(MJAData.defaultImageData); } #endif }
void UpdateImageDXT1() { string path = archive.jsonPath + "/DXT1_8192x4096/" + archive.images[archive.records.items[recordIndex].files[imageIndex]].localFile + ".dxt"; Debug.Log("loading new compressed image: " + path); Texture2D Tex2D = LoadTextureDXT.Load(path, true); if (Tex2D != null) { infoBlock.GetComponent <TextMesh>().text = "Record " + (recordIndex + 1) + "/" + archive.records.items.Length + ": " + archive.records.items[recordIndex].title + "\nImage " + (imageIndex + 1) + "/" + archive.records.items[recordIndex].files.Length + ": " + archive.images[archive.records.items[recordIndex].files[imageIndex]].full_name; gameObject.GetComponent <MeshRenderer>().material.SetTexture("_MainTex", Tex2D); } else { infoBlock.GetComponent <TextMesh>().text = "Record " + (recordIndex + 1) + "/" + archive.records.items.Length + ": " + archive.records.items[recordIndex].title + "\nImage " + (imageIndex + 1) + "/" + archive.records.items[recordIndex].files.Length + ": [Could not be loaded]"; } }