Created
June 7, 2022 09:20
-
-
Save davidjmallar/f41942abe57577c1c86576ff03d332df to your computer and use it in GitHub Desktop.
Unity AddNextSprite button with OdinInspector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Sirenix.OdinInspector; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using UnityEngine; | |
| using System; | |
| public class MyUnityObject : MonoBehaviour { | |
| [InlineButton("AddNextAnimationSprite", "Add Next")] | |
| [PreviewField(ObjectFieldAlignment.Left, Height = 50)] | |
| public List<Sprite> EffectSprites = new List<Sprite>(); | |
| public static void AddNextAnimationSprite(List<Sprite> sprites) | |
| { | |
| if (sprites == null || sprites.Count == 0) return; | |
| var lastSprite = sprites[^1]; | |
| var path = UnityEditor.AssetDatabase.GetAssetPath(lastSprite); | |
| var objects = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(path); | |
| sprites.Add(objects.Where(q => q is Sprite).Cast<Sprite>().First(s => s.name == lastSprite.name.IncrementNumberAfterUndescore())); | |
| } | |
| } | |
| public static class MiscExtensions | |
| { | |
| public static int NumberAfterUnderscore(this string text) | |
| { | |
| return int.Parse(text[(text.LastIndexOf('_') + 1)..]); | |
| } | |
| public static string IncrementNumberAfterUndescore(this string text) | |
| { | |
| var newNumber = text.NumberAfterUnderscore() + 1; | |
| text = text.Remove(text.LastIndexOf('_') + 1); | |
| return text + newNumber; | |
| } | |
| public static string SetNumberAfterUnderscore(this string text, int number) | |
| { | |
| text = text.Remove(text.LastIndexOf('_') + 1); | |
| return text + number; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment