/// <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)); }
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> /// Remove all Sprite Library override for a given category /// </summary> /// <param name="category">Category overrides to remove</param> public void RemoveOverride(string category) { var hash = new StringAndHash(SpriteLibraryAsset.GetStringHash(category)); m_Overrides.Remove(hash); RefreshSpriteResolvers(); }
/// <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 overridelabel = GetCategoryOverride(category, true); AddSpriteToOverride(overridelabel, new StringAndHash(label), sprite); }
void OnAddCallback(ReorderableList list) { var oldSize = m_Labels.arraySize; m_Labels.arraySize += 1; const string kNewCatName = "New Category"; string newCatName = kNewCatName; int catNameIncrement = 1; while (true) { if (IsNameInUsed(newCatName, m_Labels, "m_Name", 0)) { newCatName = string.Format("{0} {1}", kNewCatName, catNameIncrement++); } else { break; } } var sp = m_Labels.GetArrayElementAtIndex(oldSize); sp.FindPropertyRelative("m_Name").stringValue = newCatName; sp.FindPropertyRelative("m_Hash").intValue = SpriteLibraryAsset.GetStringHash(newCatName); }
/// <summary> /// Set the Category and label to use /// </summary> /// <param name="category">Category to use</param> /// <param name="label">Label to use</param> public void SetCategoryAndLabel(string category, string label) { categoryHashInt = SpriteLibraryAsset.GetStringHash(category); m_PreviousCategoryHash = categoryHashInt; labelHashInt = SpriteLibraryAsset.GetStringHash(label); m_PreviouslabelHash = categoryHashInt; ResolveSpriteToSpriteRenderer(); }
public void UpdateHash() { m_Hash = SpriteLibraryAsset.GetStringHash(m_Name); foreach (var s in m_CategoryList) { s.UpdateHash(); } }
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> /// Remove Sprite Library override for a given category and label /// </summary> /// <param name="category">Category to remove</param> /// <param name="label">Label to remove</param> public void RemoveOverride(string category, string label) { var catlabel = GetCategoryOverride(category, false); if (catlabel != null) { catlabel.Remove(new StringAndHash(SpriteLibraryAsset.GetStringHash(label))); RefreshSpriteResolvers(); } }
/// <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</param> public void AddCategoryLabel(Sprite sprite, string category, string label) { category = category.Trim(); label = label.Trim(); if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(label)) { Debug.LogError("Cannot add label with empty or null Category or label string"); } var catHash = SpriteLibraryAsset.GetStringHash(category); Categorylabel categorylabel = null; SpriteLibCategory libCategory = null; libCategory = m_Labels.FirstOrDefault(x => x.hash == catHash); if (libCategory != null) { 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 Categorylabel() { name = label, sprite = sprite }; libCategory.categoryList.Add(categorylabel); } } else { var slc = new SpriteLibCategory() { categoryList = new List <Categorylabel>() { new Categorylabel() { name = label, sprite = sprite } }, name = category }; m_Labels.Add(slc); } #if UNITY_EDITOR EditorUtility.SetDirty(this); #endif }
/// <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 label = GetCategoryOverride(category, true); for (int i = 0; i < cat.categoryList.Count; ++i) { AddSpriteToOverride(label, new StringAndHash(cat.categoryList[i].name), cat.categoryList[i].sprite); } } }
/// <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 } }
bool IsNameInUsed(string name, SerializedProperty property, string propertyField, int threshold) { int count = 0; var nameHash = SpriteLibraryAsset.GetStringHash(name); for (int i = 0; i < property.arraySize; ++i) { var sp = property.GetArrayElementAtIndex(i); var otherName = sp.FindPropertyRelative(propertyField).stringValue; var otherNameHash = SpriteLibraryAsset.GetStringHash(otherName); if (otherName == name || nameHash == otherNameHash) { count++; if (count > threshold) { return(true); } } } return(false); }
public void UpdateHash() { m_Hash = SpriteLibraryAsset.GetStringHash(m_Name); }
public StringAndHash(string name) { this.name = name; hash = SpriteLibraryAsset.GetStringHash(name); }