Skip to content

Instantly share code, notes, and snippets.

@MichalBerlinger
Created January 23, 2018 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichalBerlinger/b99a796bd52d7e1d334d525dbcbc4375 to your computer and use it in GitHub Desktop.
Save MichalBerlinger/b99a796bd52d7e1d334d525dbcbc4375 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sirenix.OdinInspector;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[Serializable]
public class SpineCleanerAnimation
{
public string animationName;
public string slotName;
}
[CreateAssetMenu]
public class SpineCleanerAsset : ScriptableObject
{
[SerializeField] private TextAsset jsonFile;
[SerializeField] private List<SpineCleanerAnimation> animationsToClean = new List<SpineCleanerAnimation>();
#if UNITY_EDITOR
[Button]
public void Clean()
{
JObject skeletonData = JObject.Parse(jsonFile.text);
foreach (var animationToClean in animationsToClean)
{
var slotToken = skeletonData.SelectToken(string.Format("animations.{0}.{1}.{2}", animationToClean.animationName, "slots", animationToClean.slotName));
if (slotToken == null)
{
Debug.Log(string.Format("There is no slot {0} in animation {1}", animationToClean.slotName, animationToClean.animationName));
continue;
}
Debug.Log(string.Format("Removing keys for slot {0} from animation {1}", animationToClean.slotName, animationToClean.animationName));
skeletonData.SelectToken(string.Format("animations.{0}.{1}.{2}", animationToClean.animationName, "slots", animationToClean.slotName)).Parent.Remove();
}
var newJson = JsonConvert.SerializeObject(skeletonData);
var path = Path.Combine(Application.dataPath, AssetDatabase.GetAssetPath(jsonFile).Substring(7));
StreamWriter writer = new StreamWriter(path, false);
writer.Write(newJson);
writer.Close();
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(jsonFile));
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment