void CreateEmptyIconSettings(SpellIconPack pack, string path) { pack.icons = new SpellIconSettings[pack.iconCount]; for (int i = 0; i < pack.iconCount; i++) { pack.icons[i] = new SpellIconSettings() { index = i, }; } WriteMetadata(pack, path); }
bool TryLoadPackIcons(SpellIconPack pack, Texture2D atlas, string path = null) { // Check pack is valid if (pack == null || pack.rowCount == 0 || pack.iconCount == 0 || pack.icons == null) { string debugInfo = pack != null ? pack.displayName : (path ?? "<unknown>"); Debug.LogErrorFormat("Failed to import icon pack {0} because metadata is missing or invalid.", debugInfo); return(false); } // Might need to generate empty icon settings for first time if (pack.icons == null) { CreateEmptyIconSettings(pack, path); } // Derive dimension of each icon from atlas width int dim = atlas.width / pack.rowCount; // Check icons size if (atlas.width % dim != 0 || atlas.height % dim != 0) { Debug.LogErrorFormat("Failed to extract icons from {0} because atlas size is not multiple of icons size.", pack.displayName); return(false); } // Read icons to their own texture (remembering Unity textures are flipped vertically) int srcX = 0, srcY = atlas.height - dim; for (int i = 0; i < pack.iconCount; i++) { // Extract texture Texture2D iconTexture = new Texture2D(dim, dim, atlas.format, false); Graphics.CopyTexture(atlas, 0, 0, srcX, srcY, dim, dim, iconTexture, 0, 0, 0, 0); iconTexture.filterMode = pack.filterMode; pack.icons[i].texture = iconTexture; // Step to next source icon position and wrap to next row srcX += dim; if (srcX >= atlas.width) { srcX = 0; srcY -= dim; } } return(true); }
SpellIconPack CreateIconPackEmptyMetadata(Texture2D atlasTexture, string path) { // Create an empty metadata file at path SpellIconPack pack = new SpellIconPack() { displayName = Path.GetFileNameWithoutExtension(path), rowCount = 0, iconCount = 0, filterMode = FilterMode.Bilinear, }; WriteMetadata(pack, path); //Debug.LogFormat("Created empty spell icon metadata at '{0}'.", path); return(pack); }
/// <summary> /// Populates a dictionary of *.png spell icons. /// If no metadata is present for icon atlas then an empty metadata file will be created. /// Modder must fill out basic metadata information like row count and icon count before icons will be loaded. /// </summary> void LoadSpellIconPacks() { // Start with all the atlases in the spell icons streaming assets path string sourcePath = Path.Combine(Application.streamingAssetsPath, sourceFolderName); string[] atlasPaths = Directory.GetFiles(sourcePath, "*.png"); if (atlasPaths == null || atlasPaths.Length == 0) { return; } // Read each atlas found and its metadata foreach (string path in atlasPaths) { // Get source atlas Texture2D atlasTexture = LoadAtlasTextureFromPNG(path); if (atlasTexture == null) { Debug.LogWarningFormat("LoadSpellIconPacks(): Could not load spell icons atlas texture '{0}'", path); continue; } // Attempt to load metadata JSON file SpellIconPack pack = null; string packKey = Path.GetFileNameWithoutExtension(path); string metadataFilename = packKey + ".txt"; string metadataPath = Path.Combine(sourcePath, metadataFilename); if (File.Exists(metadataPath)) { // Try to load existing metadata and icons pack = ReadMetadata(metadataPath); LoadPackIcons(pack, atlasTexture, metadataPath); } else { // Create empty metadata file if none found pack = CreateIconPackEmptyMetadata(atlasTexture, metadataPath); return; } // If pack is populated then add to dictionary if (pack != null && pack.rowCount > 0 && pack.iconCount > 0 && pack.icons != null) { spellIconPacks.Add(packKey, pack); } } }
public Texture2D GetSpellIcon(SpellIcon icon) { // Fallback to classic icons if (string.IsNullOrEmpty(icon.key) || !HasPack(icon.key)) { return(GetSpellIcon(icon.index)); } // If pack and index seem valid then return texture from pack SpellIconPack pack = spellIconPacks[icon.key]; if (pack.iconCount == 0 || pack.icons == null || icon.index < 0 || icon.index >= pack.iconCount) { return(null); } else { return(pack.icons[icon.index].texture); } }
void LoadPackIcons(SpellIconPack pack, Texture2D atlas, string path) { if (pack == null) { return; } // Might need to generate empty icon settings for first time if (pack.icons == null) { CreateEmptyIconSettings(pack, path); } // Derive dimension of each icon from atlas width int dim = atlas.width / pack.rowCount; // Read icons to their own texture (remembering Unity textures are flipped vertically) int srcX = 0, srcY = atlas.height - dim; for (int i = 0; i < pack.iconCount; i++) { // Extract texture Texture2D iconTexture = new Texture2D(dim, dim, atlas.format, false); Graphics.CopyTexture(atlas, 0, 0, srcX, srcY, dim, dim, iconTexture, 0, 0, 0, 0); iconTexture.filterMode = pack.filterMode; pack.icons[i].texture = iconTexture; // Step to next source icon position and wrap to next row srcX += dim; if (srcX >= atlas.width) { srcX = 0; srcY -= dim; } } }
void WriteMetadata(SpellIconPack pack, string path) { string json = SaveLoadManager.Serialize(pack.GetType(), pack); File.WriteAllText(path, json); }
/// <summary> /// Populates a dictionary of *.png spell icons. /// If no metadata is present for icon atlas then an empty metadata file will be created. /// Modder must fill out basic metadata information like row count and icon count before icons will be loaded. /// </summary> void LoadSpellIconPacks() { // Start with all the atlases in the spell icons streaming assets path string sourcePath = Path.Combine(Application.streamingAssetsPath, sourceFolderName); string[] atlasPaths = Directory.GetFiles(sourcePath, "*.png"); if (atlasPaths != null && atlasPaths.Length != 0) { // Read each atlas found and its metadata foreach (string path in atlasPaths) { // Get source atlas Texture2D atlasTexture = LoadAtlasTextureFromPNG(path); if (atlasTexture == null) { Debug.LogWarningFormat("LoadSpellIconPacks(): Could not load spell icons atlas texture '{0}'", path); continue; } // Attempt to load metadata JSON file SpellIconPack pack = null; string packKey = Path.GetFileNameWithoutExtension(path); string metadataFilename = packKey + ".txt"; string metadataPath = Path.Combine(sourcePath, metadataFilename); if (File.Exists(metadataPath)) { // Try to load existing metadata and icons pack = ReadMetadata(metadataPath); if (TryLoadPackIcons(pack, atlasTexture, metadataPath)) { spellIconPacks.Add(packKey, pack); } } else { // Create empty metadata file if none found pack = CreateIconPackEmptyMetadata(atlasTexture, metadataPath); } } } // Import icon packs from mods with load order if (ModManager.Instance) { foreach (Mod mod in ModManager.Instance.GetAllModsWithContributes(x => x.SpellIcons != null)) { foreach (string packName in mod.ModInfo.Contributes.SpellIcons.Where(x => !spellIconPacks.ContainsKey(x))) { // Both atlas and metadata must be provided var atlas = mod.GetAsset <Texture2D>(packName); var metaData = mod.GetAsset <TextAsset>(packName + ".json"); if (!atlas || !metaData) { Debug.LogWarningFormat("Failed to retrieve assets for icon pack {0} from {1}.", packName, mod.Title); continue; } // Add pack to dictionary var pack = SaveLoadManager.Deserialize(typeof(SpellIconPack), metaData.ToString()) as SpellIconPack; if (TryLoadPackIcons(pack, atlas)) { spellIconPacks.Add(packName, pack); } } } } }