Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save davidjmallar/f41942abe57577c1c86576ff03d332df to your computer and use it in GitHub Desktop.

Select an option

Save davidjmallar/f41942abe57577c1c86576ff03d332df to your computer and use it in GitHub Desktop.
Unity AddNextSprite button with OdinInspector
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