internal Sprite GetSprite(int categoryHash, int labelHash, out bool validEntry) { SpriteLibCategory category = null; for (int i = 0; i < m_Labels.Count; ++i) { if (m_Labels[i].hash == categoryHash) { category = m_Labels[i]; break; } } if (category != null) { Categorylabel spritelabel = null; for (int i = 0; i < category.categoryList.Count; ++i) { if (category.categoryList[i].hash == labelHash) { spritelabel = category.categoryList[i]; break; } } if (spritelabel != null) { validEntry = true; return(spritelabel.sprite); } } validEntry = false; return(null); }
/// <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> /// 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 } }