public static int get_n_frames( this SpriteLibrary in_library ) { SpriteLibraryAsset asset = in_library.spriteLibraryAsset; String category = asset.GetCategoryNames().First(); return(asset.GetCategoryLabelNames(category).Count()); }
public static void UpdateLibraryWithNewMainLibrary(SpriteLibraryAsset spriteLib, SerializedProperty library) { var emptyStringArray = new string[0]; var categories = spriteLib != null?spriteLib.GetCategoryNames() : emptyStringArray; // populate new primary int newCatgoryIndex = 0; foreach (var newCategory in categories) { SerializedProperty existingCategory = null; if (library.arraySize > 0) { var cat = library.GetArrayElementAtIndex(0); for (int i = 0; i < library.arraySize; ++i) { if (cat.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue == newCategory) { existingCategory = cat; if (i != newCatgoryIndex) { library.MoveArrayElement(i, newCatgoryIndex); } break; } cat.Next(false); } } if (existingCategory != null) { if (!existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.fromMain).boolValue) { existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.fromMain).boolValue = true; } } else { library.InsertArrayElementAtIndex(newCatgoryIndex); existingCategory = library.GetArrayElementAtIndex(newCatgoryIndex); SetPropertyName(existingCategory, newCategory); existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.fromMain).boolValue = true; existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntryCount).intValue = 0; existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntries).arraySize = 0; } newCatgoryIndex++; var newEntries = spriteLib.GetCategoryLabelNames(newCategory); var entries = existingCategory.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntries); int newEntryIndex = 0; foreach (var newEntry in newEntries) { SerializedProperty cacheEntry = null; if (entries.arraySize > 0) { var ent = entries.GetArrayElementAtIndex(0); for (int j = 0; j < entries.arraySize; ++j) { if (ent.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue == newEntry) { cacheEntry = ent; if (j != newEntryIndex) { entries.MoveArrayElement(j, newEntryIndex); } break; } ent.Next(false); } } var mainSprite = spriteLib.GetSprite(newCategory, newEntry); if (cacheEntry == null) { entries.InsertArrayElementAtIndex(newEntryIndex); cacheEntry = entries.GetArrayElementAtIndex(newEntryIndex); SetPropertyName(cacheEntry, newEntry); cacheEntry.FindPropertyRelative(SpriteLibraryPropertyString.spriteOverride) .objectReferenceValue = mainSprite; } ++newEntryIndex; if (!cacheEntry.FindPropertyRelative(SpriteLibraryPropertyString.fromMain).boolValue) { cacheEntry.FindPropertyRelative(SpriteLibraryPropertyString.fromMain).boolValue = true; } if (cacheEntry.FindPropertyRelative(SpriteLibraryPropertyString.sprite).objectReferenceValue != mainSprite) { cacheEntry.FindPropertyRelative(SpriteLibraryPropertyString.sprite).objectReferenceValue = mainSprite; } } } // Remove any library or entry that is not in primary and not overridden for (int i = 0; i < library.arraySize; ++i) { var categoryProperty = library.GetArrayElementAtIndex(i); var categoryEntriesProperty = categoryProperty.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntries); var categoryFromMainProperty = categoryProperty.FindPropertyRelative(SpriteLibraryPropertyString.fromMain); var categoryName = categoryProperty.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue; var categoryInPrimary = categories.Contains(categoryName); var entriesInPrimary = categoryInPrimary ? spriteLib.GetCategoryLabelNames(categoryName) : emptyStringArray; var categoryOverride = 0; for (int j = 0; j < categoryEntriesProperty.arraySize; ++j) { var entry = categoryEntriesProperty.GetArrayElementAtIndex(j); var entryName = entry.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue; var entryInPrimary = entriesInPrimary.Contains(entryName); var entryFromMainProperty = entry.FindPropertyRelative(SpriteLibraryPropertyString.fromMain); var overrideSpriteProperty = entry.FindPropertyRelative(SpriteLibraryPropertyString.spriteOverride); var spriteProperty = entry.FindPropertyRelative(SpriteLibraryPropertyString.sprite); if (!entryInPrimary) { // Entry no longer in new primary. // Check for override and set it to us if (entryFromMainProperty.boolValue) { if (overrideSpriteProperty.objectReferenceValue == spriteProperty.objectReferenceValue) { categoryEntriesProperty.DeleteArrayElementAtIndex(j); --j; continue; } } if (entryFromMainProperty.boolValue) { entryFromMainProperty.boolValue = false; } if (spriteProperty.objectReferenceValue != overrideSpriteProperty.objectReferenceValue) { spriteProperty.objectReferenceValue = overrideSpriteProperty.objectReferenceValue; } ++categoryOverride; } else { // Check if sprite has been override if (spriteProperty.objectReferenceValue != overrideSpriteProperty.objectReferenceValue) { ++categoryOverride; } } } if (!categoryInPrimary && categoryEntriesProperty.arraySize == 0 && categoryFromMainProperty.boolValue) { library.DeleteArrayElementAtIndex(i); --i; continue; } // since there is override, and we removed the main. This category now // belows to the library if (!categoryInPrimary) { if (categoryFromMainProperty.boolValue) { categoryFromMainProperty.boolValue = false; } } else { if (categoryProperty.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntryCount).intValue != categoryOverride) { categoryProperty.FindPropertyRelative(SpriteLibraryPropertyString.overrideEntryCount).intValue = categoryOverride; } } } }