internal static float ConvertCategoryLabelHashToSpriteKey(SpriteLibrary library, int categoryHash, int labelHash) { if (library != null) { foreach (var category in library.categoryNames) { if (categoryHash == SpriteLibraryAsset.GetStringHash(category)) { var entries = library.GetEntryNames(category); if (entries != null) { foreach (var entry in entries) { if (labelHash == SpriteLibraryAsset.GetStringHash(entry)) { var spriteKey = SpriteLibrary.GetHashForCategoryAndEntry(category, entry); return(ConvertIntToFloat(spriteKey)); } } } } } } return(0); }
internal static void RenameDuplicate(IEnumerable <INameHash> nameHashList, Action <string, string> onRename) { const int k_IncrementMax = 1000; for (int i = 0; i < nameHashList.Count(); ++i) { // Verify categories have no hash clash var category = nameHashList.ElementAt(i); var categoriesClash = nameHashList.Where(x => (x.hash == category.hash || x.name == category.name) && x != category); int increment = 0; for (int j = 0; j < categoriesClash.Count(); ++j) { var categoryClash = categoriesClash.ElementAt(j); while (increment < k_IncrementMax) { var name = categoryClash.name; name = string.Format("{0}_{1}", name, increment); var nameHash = SpriteLibraryAsset.GetStringHash(name); var exist = nameHashList.FirstOrDefault(x => (x.hash == nameHash || x.name == name) && x != categoryClash); if (exist == null) { onRename(categoryClash.name, name); categoryClash.name = name; break; } ++increment; } } } }
/// <summary> /// Returns the Sprite registered in the Asset given the Category and Label value /// </summary> /// <param name="category">Category string value</param> /// <param name="label">Label string value</param> /// <returns></returns> public Sprite GetSprite(string category, string label) { var categoryHash = SpriteLibraryAsset.GetStringHash(category); var labelHash = SpriteLibraryAsset.GetStringHash(label); return(GetSprite(categoryHash, labelHash)); }
public void UpdateHash() { m_Hash = SpriteLibraryAsset.GetStringHash(m_Name); foreach (var s in m_CategoryList) { s.UpdateHash(); } }
/// <summary> /// Add or replace an override when querying for the given Category and Label from a SpriteLibraryAsset /// </summary> /// <param name="spriteLib">Sprite Library Asset to query</param> /// <param name="category">Category name from the Sprite Library Asset to add override</param> /// <param name="label">Label name to add override</param> public void AddOverride(SpriteLibraryAsset spriteLib, string category, string label) { var sprite = spriteLib.GetSprite(category, label); var entries = GetEntries(category, true); var entry = GetEntry(entries, label, true); entry.sprite = sprite; CacheOverrides(); }
internal void ValidateLabels() { SpriteLibraryAsset.RenameDuplicate(m_CategoryList, (originalName, newName) => { Debug.LogWarning(string.Format("Label {0} renamed to {1} due to hash clash", originalName, newName)); }); }
/// <summary> /// Add or replace an override when querying for the given Category. All the categories in the Category will be added. /// </summary> /// <param name="spriteLib">Sprite Library Asset to query</param> /// <param name="category">Category name from the Sprite Library Asset to add override</param> public void AddOverride(SpriteLibraryAsset spriteLib, string category) { var categoryHash = SpriteLibraryAsset.GetStringHash(category); var cat = spriteLib.categories.FirstOrDefault(x => x.hash == categoryHash); if (cat != null) { var entries = GetEntries(category, true); for (var i = 0; i < cat.categoryList.Count; ++i) { var ent = cat.categoryList[i]; GetEntry(entries, ent.name, true).sprite = ent.sprite; } CacheOverrides(); } }
/// <summary> /// Remove a Label from a given Category /// </summary> /// <param name="category">Category to remove from</param> /// <param name="label">Label to remove</param> /// <param name="deleteCategory">Indicate to remove the Category if it is empty</param> public void RemoveCategoryLabel(string category, string label, bool deleteCategory) { var catHash = SpriteLibraryAsset.GetStringHash(category); SpriteLibCategory libCategory = null; libCategory = m_Labels.FirstOrDefault(x => x.hash == catHash); if (libCategory != null) { var labelHash = SpriteLibraryAsset.GetStringHash(label); libCategory.categoryList.RemoveAll(x => x.hash == labelHash); if (deleteCategory && libCategory.categoryList.Count == 0) { m_Labels.RemoveAll(x => x.hash == libCategory.hash); } #if UNITY_EDITOR EditorUtility.SetDirty(this); #endif } }
internal static int GetHashForCategoryAndEntry(string category, string entry) { return(SpriteLibraryAsset.GetStringHash($"{category}_{entry}")); }
static internal int GetHashForCategoryAndEntry(string category, string entry) { return(SpriteLibraryAsset.GetStringHash(string.Format("{0}_{1}", category, entry))); }
public void UpdateHash() { m_Hash = SpriteLibraryAsset.GetStringHash(m_Name); }
/// <summary> /// Add or replace and existing Sprite into the given Category and Label /// </summary> /// <param name="sprite">Sprite to add</param> /// <param name="category">Category to add the Sprite to</param> /// <param name="label">Label of the Category to add the Sprite to. If this parameter is null or an empty string, it will attempt to add a empty category</param> public void AddCategoryLabel(Sprite sprite, string category, string label) { category = category.Trim(); label = label?.Trim(); if (string.IsNullOrEmpty(category)) { throw new ArgumentException("Cannot add empty or null Category string"); } var catHash = SpriteLibraryAsset.GetStringHash(category); SpriteCategoryEntry categorylabel = null; SpriteLibCategory libCategory = null; libCategory = m_Labels.FirstOrDefault(x => x.hash == catHash); if (libCategory != null) { if (string.IsNullOrEmpty(label)) { throw new ArgumentException("Cannot add empty or null Label string"); } Assert.AreEqual(libCategory.name, category, "Category string hash clashes with another existing Category. Please use another string"); var labelHash = SpriteLibraryAsset.GetStringHash(label); categorylabel = libCategory.categoryList.FirstOrDefault(y => y.hash == labelHash); if (categorylabel != null) { Assert.AreEqual(categorylabel.name, label, "Label string hash clashes with another existing label. Please use another string"); categorylabel.sprite = sprite; } else { categorylabel = new SpriteCategoryEntry() { name = label, sprite = sprite }; libCategory.categoryList.Add(categorylabel); } } else { var slc = new SpriteLibCategory() { categoryList = new List <SpriteCategoryEntry>(), name = category }; if (!string.IsNullOrEmpty(label)) { slc.categoryList.Add(new SpriteCategoryEntry() { name = label, sprite = sprite }); } m_Labels.Add(slc); } #if UNITY_EDITOR EditorUtility.SetDirty(this); #endif }