/// <summary>
        /// Implementation of ScriptedImporter.OnImportAsset
        /// </summary>
        /// <param name="ctx">
        /// This argument contains all the contextual information needed to process the import
        /// event and is also used by the custom importer to store the resulting Unity Asset.
        /// </param>
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var spriteLib = ScriptableObject.CreateInstance <SpriteLibraryAsset>();

            spriteLib.name = System.IO.Path.GetFileNameWithoutExtension(assetPath);
            var sourceAsset = UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget(assetPath);

            if (sourceAsset?.Length > 0)
            {
                var sourceLibraryAsset = sourceAsset[0] as SpriteLibrarySourceAsset;
                if (sourceLibraryAsset != null)
                {
                    foreach (var cat in sourceLibraryAsset.library)
                    {
                        spriteLib.AddCategoryLabel(null, cat.name, null);
                        foreach (var entry in cat.overrideEntries)
                        {
                            spriteLib.AddCategoryLabel(entry.spriteOverride, cat.name, entry.name);
                        }
                    }
                }
                if (!string.IsNullOrEmpty(sourceLibraryAsset.primaryLibraryID))
                {
                    var primaryAssetPath = AssetDatabase.GUIDToAssetPath(sourceLibraryAsset.primaryLibraryID);
                    if (primaryAssetPath != assetPath)
                    {
                        ctx.DependsOnArtifact(AssetDatabase.GUIDToAssetPath(sourceLibraryAsset.primaryLibraryID));
                        m_PrimaryLibrary = AssetDatabase.LoadAssetAtPath <SpriteLibraryAsset>(primaryAssetPath);
                    }
                }
            }

            ctx.AddObjectToAsset("SpriteLib", spriteLib, IconUtility.LoadIconResource("Sprite Library", "Icons/Light", "Icons/Dark"));
        }
        bool IsEntryNameUsed(string name, SerializedProperty spriteList, int duplicateAllow)
        {
            if (spriteList.arraySize == 0)
            {
                return(false);
            }
            var sp       = spriteList.GetArrayElementAtIndex(0);
            var nameHash = SpriteLibraryAsset.GetStringHash(name);
            int count    = 0;

            for (int i = 0; i < spriteList.arraySize; ++i, sp.Next(false))
            {
                var stringValue = sp.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue;
                var hash        = SpriteLibraryAsset.GetStringHash(stringValue);
                if (stringValue == name || hash == nameHash)
                {
                    ++count;
                    if (count > duplicateAllow)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        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);
        }
        public void Initialize()
        {
            Debug.Log("Assets initialize");

            assetHolder.Initialize();
            foreach (var taggedAsset in assetHolder.GetItems <TaggedAsset>())
            {
                taggedAsset.Initialize();
            }

            _spriteLibraryAsset = ScriptableObject.CreateInstance <SpriteLibraryAsset>();
            SpriteLibraryAssetCreator.CompleteCharacters(ref _spriteLibraryAsset,
                                                         GetAsset(AssetTag.Characters).GetItems <Character>(), emptySprite, deadFaceSprite);
            SpriteLibraryAssetCreator.CompleteAccessories(ref _spriteLibraryAsset,
                                                          GetAsset(AssetTag.Accessories).GetItems <Accessory>(), emptySprite);

            //Purpose of icon backgrounds for achievements
            var length           = GetAsset(AssetTag.Achievements).Length;
            var spriteCount      = achievementIconBgs.Length;
            var achievementArray = GetAsset(AssetTag.Achievements).GetItems <Achievement>();

            for (var i = 0; i < length; i++)
            {
                var index = i % spriteCount;
                achievementArray[i].IconBg = achievementIconBgs[index];
            }
        }
Beispiel #5
0
    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());
    }
Beispiel #6
0
        public static void CompleteAccessories(ref SpriteLibraryAsset asset, IEnumerable <Accessory> accessories, Sprite empty)
        {
            foreach (var category in typeof(AccessoryCategory).GetEnumValues())
            {
                asset.AddCategoryLabel(empty, category.ToString(), PlayerLook.NoneLabel);
            }

            foreach (var accessory in accessories)
            {
                asset.AddCategoryLabel(accessory.Icon, accessory.Category.ToString(), accessory.TaggedName.ToString());
            }
        }
Beispiel #7
0
 public override void OnEnable()
 {
     base.OnEnable();
     m_PrimaryLibraryGUID = extraDataSerializedObject.FindProperty("m_PrimaryLibraryGUID");
     if (!m_PrimaryLibraryGUID.hasMultipleDifferentValues && !string.IsNullOrEmpty(m_PrimaryLibraryGUID.stringValue))
     {
         var assetPath = AssetDatabase.GUIDToAssetPath(m_PrimaryLibraryGUID.stringValue);
         m_MainSpriteLibraryAsset = AssetDatabase.LoadAssetAtPath <SpriteLibraryAsset>(assetPath);
     }
     m_Library = extraDataSerializedObject.FindProperty("m_Library");
     m_SpriteLibraryDataInspector = new SpriteLibraryDataInspector(extraDataSerializedObject, m_Library);
 }
Beispiel #8
0
 void DoMainAssetGUI()
 {
     EditorGUI.BeginChangeCheck();
     if (m_PrimaryLibraryGUID.hasMultipleDifferentValues)
         EditorGUI.showMixedValue = true;
     m_MainSpriteLibraryAsset = AssetDatabase.LoadAssetAtPath<SpriteLibraryAsset>(AssetDatabase.GUIDToAssetPath(m_PrimaryLibraryGUID.stringValue));
     m_MainSpriteLibraryAsset = EditorGUILayout.ObjectField(Style.mainAssetLabel, m_MainSpriteLibraryAsset, typeof(SpriteLibraryAsset), false) as SpriteLibraryAsset;
     if (EditorGUI.EndChangeCheck())
     {
         m_PrimaryLibraryGUID.stringValue = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(m_MainSpriteLibraryAsset));
         SpriteLibraryDataInspector.UpdateLibraryWithNewMainLibrary(m_MainSpriteLibraryAsset, m_Library);
         serializedObject.ApplyModifiedProperties();
     }
     EditorGUI.showMixedValue = false;
 }
        public void AddSpriteToCategory(string category, SpriteCategoryLabel label)
        {
            if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(label.name))
            {
                // Remove sprite from name
                RemoveSpriteFromCategory(label.spriteId);
            }
            else
            {
                //find cateogry
                var categoryIndex = categories.FindIndex(x => x.name == category);
                if (categoryIndex == -1)
                {
                    // check if the hash might clash
                    var hash = SpriteLibraryAsset.GetStringHash(category);
                    if (categories.FindIndex(x => x.name != category && SpriteLibraryAsset.GetStringHash(x.name) == hash) != -1)
                    {
                        Debug.LogError("Unable to add Sprite to new Category due to name hash clash");
                        return;
                    }
                }
                var insertCategory = categoryIndex != -1 ? categories[categoryIndex] : new SpriteCategory()
                {
                    name = category, labels = new List <SpriteCategoryLabel>()
                };
                if (insertCategory.labels.FindIndex(x => x.spriteId == label.spriteId) == -1)
                {
                    insertCategory.labels.Add(label);
                }

                // now remove everything that has this sprite
                foreach (var cat in categories)
                {
                    if (cat.name != category)
                    {
                        cat.labels.RemoveAll(x => x.spriteId == label.spriteId);
                    }
                }
                if (categoryIndex == -1)
                {
                    categories.Add(insertCategory);
                }
                else
                {
                    categories[categoryIndex] = insertCategory;
                }
            }
        }
Beispiel #10
0
        public static void CompleteCharacters(ref SpriteLibraryAsset asset, IEnumerable <Character> characters, Sprite empty, Sprite deadFace)
        {
            foreach (var category in Enum.GetValues(typeof(BodyPartTag)))
            {
                asset.AddCategoryLabel(empty, category.ToString(), PlayerLook.NoneLabel);
            }

            asset.AddCategoryLabel(deadFace, BodyPartTag.Face.ToString(), PlayerLook.DeadFaceLabel);

            foreach (var character in characters)
            {
                foreach (var part in character.BodyParts)
                {
                    asset.AddCategoryLabel(part.Sprite, part.Tag.ToString(), character.TaggedName.ToString());
                }
            }
        }
        private void Awake()
        {
            _asset = Assets.SpriteLibraryAsset;
            GetComponent <SpriteLibrary>().spriteLibraryAsset = _asset;
            _face      = transform.Find(BodyPartTag.Face.ToString()).GetComponent <SpriteResolver>();
            _bodyParts = new List <SpriteResolver>();

            foreach (BodyPartTag bodyPartTag in typeof(BodyPartTag).GetEnumValues())
            {
                _bodyParts.Add(transform.Find(bodyPartTag.ToString()).GetComponent <SpriteResolver>());
            }

            _accessories = new List <SpriteResolver>();

            foreach (AccessoryCategory category in typeof(AccessoryCategory).GetEnumValues())
            {
                _accessories.Add(transform.Find(category.ToString()).GetComponent <SpriteResolver>());
            }
        }
 void OnPreprocessAsset()
 {
     if (assetImporter is SpriteLibrarySourceAssetImporter)
     {
         var obj = SpriteLibrarySourceAssetImporter.LoadSpriteLibrarySourceAsset(assetPath);
         if (obj != null)
         {
             SpriteLibraryAsset mainLibraryAsset = null;
             var mainLibraryAssetAssetPath       = AssetDatabase.GUIDToAssetPath(obj.primaryLibraryID);
             mainLibraryAsset = AssetDatabase.LoadAssetAtPath <SpriteLibraryAsset>(mainLibraryAssetAssetPath);
             var so      = new SerializedObject(obj);
             var library = so.FindProperty("m_Library");
             SpriteLibraryDataInspector.UpdateLibraryWithNewMainLibrary(mainLibraryAsset, library);
             if (so.hasModifiedProperties)
             {
                 so.ApplyModifiedPropertiesWithoutUndo();
                 SpriteLibrarySourceAssetImporter.SaveSpriteLibrarySourceAsset(obj, assetPath);
             }
         }
     }
 }
        SerializedProperty IsCategoryNameInUsed(string name)
        {
            if (m_Library.arraySize == 0)
            {
                return(null);
            }
            var nameHash = SpriteLibraryAsset.GetStringHash(name);
            var sp       = m_Library.GetArrayElementAtIndex(0);

            for (int i = 0; i < m_Library.arraySize; ++i)
            {
                var nameProperty = sp.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue;
                var hash         = SpriteLibraryAsset.GetStringHash(nameProperty);
                if (nameProperty == name || nameHash == hash)
                {
                    return(sp);
                }
                sp.Next(false);
            }

            return(null);
        }
        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);
        }
Beispiel #15
0
 private void UpdateMasterLibraryObject()
 {
     m_MasterLibraryObject = (SpriteLibraryAsset)m_MasterLibraryProperty.objectReferenceValue;
 }
Beispiel #16
0
 public void SwapSprites(SpriteLibraryAsset spriteLibraryAsset)
 {
     spriteLibrary.spriteLibraryAsset = spriteLibraryAsset;
 }
        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;
                    }
                }
            }
        }
 static void SetPropertyName(SerializedProperty sp, string newName)
 {
     sp.FindPropertyRelative(SpriteLibraryPropertyString.name).stringValue = newName;
     sp.FindPropertyRelative(SpriteLibraryPropertyString.hash).intValue    = SpriteLibraryAsset.GetStringHash(newName);
 }
        void UpdateSpriteLibrary()
        {
            m_SpriteLibSelection.Clear();
            int categoryHash = SpriteResolver.ConvertFloatToInt(m_SpriteCategoryHash.floatValue);
            int labelHash    = SpriteResolver.ConvertFloatToInt(m_SpritelabelHash.floatValue);
            var spriteLib    = spriteResolver.GetComponentInParent <SpriteLibrary>();

            if (spriteLib != null)
            {
                foreach (var labels in spriteLib.labels)
                {
                    if (!m_SpriteLibSelection.ContainsKey(labels.hash))
                    {
                        var nameHash = labels.categoryList.Select(x => x.hash).Distinct().ToArray();
                        if (nameHash.Length > 0)
                        {
                            var selectionList = new SpriteCategorySelectionList()
                            {
                                names = nameHash.Select(x =>
                                {
                                    var v = labels.categoryList.FirstOrDefault(y => y.hash == x);
                                    return(v.name);
                                }).ToArray(),
                                nameHash = nameHash,
                                sprites  = nameHash.Select(x =>
                                {
                                    var v = labels.categoryList.FirstOrDefault(y => y.hash == x);
                                    return(v.sprite);
                                }).ToArray(),
                                categoryName     = labels.name,
                                categoryNameHash = labels.hash
                            };

                            m_SpriteLibSelection.Add(labels.hash, selectionList);
                        }
                    }
                }
            }
            m_CategorySelection        = new string[1 + m_SpriteLibSelection.Keys.Count];
            m_CategorySelection[0]     = TextContent.none;
            m_CategorySelectionHash    = new int[1 + m_SpriteLibSelection.Keys.Count];
            m_CategorySelectionHash[0] = SpriteLibraryAsset.GetStringHash(TextContent.none);
            for (int i = 0; i < m_SpriteLibSelection.Keys.Count; ++i)
            {
                var selection = m_SpriteLibSelection[m_SpriteLibSelection.Keys.ElementAt(i)];
                m_CategorySelection[i + 1]     = selection.categoryName;
                m_CategorySelectionHash[i + 1] = selection.categoryNameHash;
                if (selection.categoryNameHash == categoryHash)
                {
                    m_CategorySelectionIndex = i + 1;
                }
            }
            ValidateCategorySelectionIndexValue();
            if (m_CategorySelectionIndex > 0)
            {
                m_SpriteSelectorWidget.UpdateContents(m_SpriteLibSelection[m_CategorySelectionHash[m_CategorySelectionIndex]].sprites);
                if (m_SpriteLibSelection.ContainsKey(categoryHash))
                {
                    m_labelSelectionIndex = Array.FindIndex(m_SpriteLibSelection[categoryHash].nameHash, x => x == labelHash);
                }
            }
            spriteResolver.spriteLibChanged = false;
        }
        public bool IsLabelDuplicate(SpriteCategory category, string labelName)
        {
            var hash = SpriteLibraryAsset.GetStringHash(labelName);

            return(category.labels.Count(x => x.name == labelName || hash == SpriteLibraryAsset.GetStringHash(x.name)) > 1);
        }
Beispiel #21
0
 public void Replace(SpriteLibraryAsset libraryAsset)
 {
     spriteLibrary.spriteLibraryAsset = libraryAsset;
 }