/// <summary> /// Gets the contract icon for the given id and seed (color). /// </summary> /// <param name="url">URL of the icon</param> /// <param name="seed">Seed to use for generating the color</param> /// <returns>The texture</returns> public static Texture2D GetContractIcon(string url, int seed) { // Check cache for texture Texture2D texture; Color color = SystemUtilities.RandomColor(seed, 1.0f, 1.0f, 1.0f); if (!contractIcons.ContainsKey(url)) { contractIcons[url] = new Dictionary <Color, Texture2D>(); } if (!contractIcons[url].ContainsKey(color)) { Texture2D baseTexture = ContractDefs.sprites[url].texture; try { Texture2D loadedTexture = null; string path = (url.Contains('/') ? "GameData/" : "GameData/Squad/Contracts/Icons/") + url; // PNG loading if (File.Exists(path + ".png")) { path += ".png"; loadedTexture = new Texture2D(baseTexture.width, baseTexture.height, TextureFormat.RGBA32, false); loadedTexture.LoadImage(File.ReadAllBytes(path.Replace('/', Path.DirectorySeparatorChar))); } // DDS loading else if (File.Exists(path + ".dds")) { path += ".dds"; BinaryReader br = new BinaryReader(new MemoryStream(File.ReadAllBytes(path))); if (br.ReadUInt32() != DDSValues.uintMagic) { throw new Exception("Format issue with DDS texture '" + path + "'!"); } DDSHeader ddsHeader = new DDSHeader(br); if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDX10) { DDSHeaderDX10 ddsHeaderDx10 = new DDSHeaderDX10(br); } TextureFormat texFormat; if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT1) { texFormat = UnityEngine.TextureFormat.DXT1; } else if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT3) { texFormat = UnityEngine.TextureFormat.DXT1 | UnityEngine.TextureFormat.Alpha8; } else if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT5) { texFormat = UnityEngine.TextureFormat.DXT5; } else { throw new Exception("Unhandled DDS format!"); } loadedTexture = new Texture2D((int)ddsHeader.dwWidth, (int)ddsHeader.dwHeight, texFormat, false); loadedTexture.LoadRawTextureData(br.ReadBytes((int)(br.BaseStream.Length - br.BaseStream.Position))); } else { throw new Exception("Couldn't find file for icon '" + url + "'"); } Color[] pixels = loadedTexture.GetPixels(); for (int i = 0; i < pixels.Length; i++) { pixels[i] *= color; } texture = new Texture2D(baseTexture.width, baseTexture.height, TextureFormat.RGBA32, false); texture.SetPixels(pixels); texture.Apply(false, false); contractIcons[url][color] = texture; UnityEngine.Object.Destroy(loadedTexture); } catch (Exception e) { Debug.LogError("WaypointManager: Couldn't create texture for '" + url + "'!"); Debug.LogException(e); texture = contractIcons[url][color] = baseTexture; } } else { texture = contractIcons[url][color]; } return(texture); }
/// <summary> /// Gets the contract icon for the given id and seed (color). /// </summary> /// <param name="url">URL of the icon</param> /// <param name="seed">Seed to use for generating the color</param> /// <returns>The texture</returns> public static Texture2D GetContractIcon(string url, int seed) { // Check cache for texture Texture2D texture; Color color = SystemUtilities.RandomColor(seed, 1.0f, 1.0f, 1.0f); if (!contractIcons.ContainsKey(url)) { contractIcons[url] = new Dictionary<Color, Texture2D>(); } if (!contractIcons[url].ContainsKey(color)) { Texture2D baseTexture = ContractDefs.sprites[url].texture; try { Texture2D loadedTexture = null; string path = (url.Contains('/') ? "GameData/" : "GameData/Squad/Contracts/Icons/") + url; // PNG loading if (File.Exists(path + ".png")) { path += ".png"; loadedTexture = new Texture2D(baseTexture.width, baseTexture.height, TextureFormat.RGBA32, false); loadedTexture.LoadImage(File.ReadAllBytes(path.Replace('/', Path.DirectorySeparatorChar))); } // DDS loading else if (File.Exists(path + ".dds")) { path += ".dds"; BinaryReader br = new BinaryReader(new MemoryStream(File.ReadAllBytes(path))); if (br.ReadUInt32() != DDSValues.uintMagic) { throw new Exception("Format issue with DDS texture '" + path + "'!"); } DDSHeader ddsHeader = new DDSHeader(br); if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDX10) { DDSHeaderDX10 ddsHeaderDx10 = new DDSHeaderDX10(br); } TextureFormat texFormat; if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT1) { texFormat = UnityEngine.TextureFormat.DXT1; } else if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT3) { texFormat = UnityEngine.TextureFormat.DXT1 | UnityEngine.TextureFormat.Alpha8; } else if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT5) { texFormat = UnityEngine.TextureFormat.DXT5; } else { throw new Exception("Unhandled DDS format!"); } loadedTexture = new Texture2D((int)ddsHeader.dwWidth, (int)ddsHeader.dwHeight, texFormat, false); loadedTexture.LoadRawTextureData(br.ReadBytes((int)(br.BaseStream.Length - br.BaseStream.Position))); } else { throw new Exception("Couldn't find file for icon '" + url + "'"); } Color[] pixels = loadedTexture.GetPixels(); for (int i = 0; i < pixels.Length; i++) { pixels[i] *= color; } texture = new Texture2D(baseTexture.width, baseTexture.height, TextureFormat.RGBA32, false); texture.SetPixels(pixels); texture.Apply(false, false); contractIcons[url][color] = texture; UnityEngine.Object.Destroy(loadedTexture); } catch (Exception e) { Debug.LogError("WaypointManager: Couldn't create texture for '" + url + "'!"); Debug.LogException(e); texture = contractIcons[url][color] = baseTexture; } } else { texture = contractIcons[url][color]; } return texture; }
public static Texture2D LoadTexture(string url) { // Check cache for texture Texture2D texture; try { string path = "GameData/" + url; // PNG loading if (File.Exists(path) && path.Contains(".png")) { texture = new Texture2D(2, 2, TextureFormat.RGBA32, false); texture.LoadImage(File.ReadAllBytes(path.Replace('/', Path.DirectorySeparatorChar))); } // DDS loading else if (File.Exists(path) && path.Contains(".dds")) { BinaryReader br = new BinaryReader(new MemoryStream(File.ReadAllBytes(path))); if (br.ReadUInt32() != DDSValues.uintMagic) { throw new Exception("Format issue with DDS texture '" + path + "'!"); } DDSHeader ddsHeader = new DDSHeader(br); if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDX10) { DDSHeaderDX10 ddsHeaderDx10 = new DDSHeaderDX10(br); } TextureFormat texFormat; if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT1) { texFormat = UnityEngine.TextureFormat.DXT1; } else if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT3) { texFormat = UnityEngine.TextureFormat.DXT1 | UnityEngine.TextureFormat.Alpha8; } else if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT5) { texFormat = UnityEngine.TextureFormat.DXT5; } else { throw new Exception("Unhandled DDS format!"); } texture = new Texture2D((int)ddsHeader.dwWidth, (int)ddsHeader.dwHeight, texFormat, false); texture.LoadRawTextureData(br.ReadBytes((int)(br.BaseStream.Length - br.BaseStream.Position))); texture.Apply(false, true); } else { throw new Exception("Couldn't find file for image '" + url + "'"); } } catch (Exception e) { LoggingUtil.LogError(typeof(TextureUtil), "Couldn't create texture for '" + url + "'!"); Debug.LogException(e); texture = null; } return texture; }
public static Texture2D LoadTexture(string url) { // Check cache for texture Texture2D texture; try { // Auto-detect file type string path = "GameData/" + url; if (!File.Exists(path)) { if (File.Exists(path + ".png")) { path += ".png"; } else if (File.Exists(path + ".dds")) { path += ".dds"; } } // PNG loading if (File.Exists(path) && path.EndsWith(".png")) { texture = new Texture2D(2, 2, TextureFormat.RGBA32, false); texture.LoadImage(File.ReadAllBytes(path.Replace('/', Path.DirectorySeparatorChar))); } // DDS loading else if (File.Exists(path) && path.EndsWith(".dds")) { BinaryReader br = new BinaryReader(new MemoryStream(File.ReadAllBytes(path))); if (br.ReadUInt32() != DDSValues.uintMagic) { throw new Exception("Format issue with DDS texture '" + path + "'!"); } DDSHeader ddsHeader = new DDSHeader(br); if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDX10) { DDSHeaderDX10 ddsHeaderDx10 = new DDSHeaderDX10(br); } TextureFormat texFormat; if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT1) { texFormat = UnityEngine.TextureFormat.DXT1; } else if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT3) { texFormat = UnityEngine.TextureFormat.DXT1 | UnityEngine.TextureFormat.Alpha8; } else if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT5) { texFormat = UnityEngine.TextureFormat.DXT5; } else { throw new Exception("Unhandled DDS format!"); } texture = new Texture2D((int)ddsHeader.dwWidth, (int)ddsHeader.dwHeight, texFormat, false); texture.LoadRawTextureData(br.ReadBytes((int)(br.BaseStream.Length - br.BaseStream.Position))); texture.Apply(false, true); } else { throw new Exception(StringBuilderCache.Format("Couldn't find file for image '{0}'", url)); } } catch (Exception e) { LoggingUtil.LogError(typeof(TextureUtil), "Couldn't create texture for '{0}'!", url); LoggingUtil.LogException(e); texture = null; } return(texture); }
/// <summary> /// Gets the contract icon for the given id and seed (color). /// </summary> /// <param name="url">URL of the icon</param> /// <param name="seed">Seed to use for generating the color</param> /// <returns>The texture</returns> /// public static Texture2D GetContractIcon(string url, int seed) { string key = url; // Check cache for texture Texture2D texture; Color color = SystemUtilities.RandomColor(seed, 1.0f, 1.0f, 1.0f); if (!contractIcons.ContainsKey(key)) { contractIcons[key] = new Dictionary <Color, Texture2D>(); } if (!contractIcons[key].ContainsKey(color)) { if (url.EndsWith(".png")) { url = url.Substring(0, url.Length - 4); } if (url.StartsWith("GameData")) { url = url.Substring(9); } Texture2D baseTexture = ContractDefs.sprites[url].texture; url = url.Replace('\\', '/'); string url2 = url; string tmp = url; if (!tmp.Contains("PluginData")) { if (!url.StartsWith("GameData")) { tmp = "/" + url; } for (int i = 0; i < GameDatabase.Instance.databaseTexture.Count; i++) { if (GameDatabase.Instance.databaseTexture[i].file != null) { if (GameDatabase.Instance.databaseTexture[i].name.EndsWith(tmp) || GameDatabase.Instance.databaseTexture[i].name == url) { url2 = GameDatabase.Instance.databaseTexture[i].file.fullPath; } } } } else { if (!url2.StartsWith("GameData")) { url2 = KSPUtil.ApplicationRootPath + "GameData/" + url2; } } string path = url2; if (path.EndsWith(".png") || path.EndsWith(".dds")) { path = path.Substring(0, path.Length - 4); } try { Texture2D loadedTexture = null; // PNG loading if (File.Exists(path + ".png")) { path += ".png"; loadedTexture = new Texture2D(baseTexture.width, baseTexture.height, TextureFormat.RGBA32, false); loadedTexture.LoadImage(File.ReadAllBytes(path.Replace('/', Path.DirectorySeparatorChar))); } // DDS loading else { if (File.Exists(path + ".dds")) { path += ".dds"; BinaryReader br = new BinaryReader(new MemoryStream(File.ReadAllBytes(path))); if (br.ReadUInt32() != DDSValues.uintMagic) { throw new Exception("Format issue with DDS texture '" + path + "'!"); } DDSHeader ddsHeader = new DDSHeader(br); if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDX10) { DDSHeaderDX10 ddsHeaderDx10 = new DDSHeaderDX10(br); } TextureFormat texFormat; if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT1) { texFormat = UnityEngine.TextureFormat.DXT1; } #if false // Not using DXT3 anymore else if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT3) { texFormat = UnityEngine.TextureFormat.DXT1 | UnityEngine.TextureFormat.Alpha8; } #endif else if (ddsHeader.ddspf.dwFourCC == DDSValues.uintDXT5) { texFormat = UnityEngine.TextureFormat.DXT5; } else { throw new Exception("Unhandled DDS format!"); } loadedTexture = new Texture2D((int)ddsHeader.dwWidth, (int)ddsHeader.dwHeight, texFormat, false); loadedTexture.LoadRawTextureData(br.ReadBytes((int)(br.BaseStream.Length - br.BaseStream.Position))); } else { throw new Exception("Couldn't find file for icon '" + url + "'"); } } Color[] pixels = loadedTexture.GetPixels(); for (int i = 0; i < pixels.Length; i++) { pixels[i] *= color; } //texture = new Texture2D(baseTexture.width, baseTexture.height, TextureFormat.RGBA32, false); texture = new Texture2D(loadedTexture.width, loadedTexture.height, TextureFormat.RGBA32, false); texture.SetPixels(pixels); texture.Apply(false, false); contractIcons[key][color] = texture; UnityEngine.Object.Destroy(loadedTexture); } catch (Exception e) { Log.Error("WaypointManager: Couldn't create texture for '" + url + "'!"); Log.Error("key: " + key); Log.Error("path: " + path); Debug.LogException(e); texture = contractIcons[key][color] = baseTexture; } } else { texture = contractIcons[key][color]; } return(texture); }