static void UpdateAnim() { string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject); HashSet <string> guids = new HashSet <string>(AssetDatabase.FindAssets("", new string[] { assetPath })); Dictionary <string, Sprite> sprites = new Dictionary <string, Sprite>(); foreach (string guid in guids) { Object[] objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GUIDToAssetPath(guid)); foreach (Object o in objs) { if (o is Sprite) { sprites.Add(o.name, o as Sprite); } } } Dictionary <string, Cartoon> Cartoons = new Dictionary <string, Cartoon>(); List <Sprite> tempss = new List <Sprite>(); for (int i = 0; i < 1000; ++i) { string animName = string.Format("anim_{0}_", i); for (int j = 0; j < 100; ++j) { string frameName = animName + j; Sprite s = null; if (sprites.TryGetValue(frameName, out s)) { tempss.Add(s); } } if (tempss.Count != 0) { Cartoon c = new Cartoon(); c.name = i.ToString(); c.fps = 5f; c.sprites = tempss.ToArray(); c.width = (int)c.sprites[0].rect.width; c.height = (int)c.sprites[0].rect.height; Cartoons.Add(i.ToString(), c); tempss.Clear(); } } SymbolTextInit sti = Resources.Load <SymbolTextInit>("SymbolTextInit"); FieldInfo info = typeof(SymbolTextInit).GetField("cartoons", BindingFlags.Instance | BindingFlags.NonPublic); List <Cartoon> cartoons = new List <Cartoon>(Cartoons.Values); cartoons.Sort((Cartoon x, Cartoon y) => { return(int.Parse(x.name).CompareTo(int.Parse(y.name))); }); info.SetValue(sti, cartoons.ToArray()); EditorUtility.SetDirty(sti); }
static void UpdateAnim() { Dictionary <string, List <Frame> > anims = new Dictionary <string, List <Frame> >(); { HashSet <string> guids = new HashSet <string>(AssetDatabase.FindAssets("", new string[] { "Assets" })); foreach (string guid in guids) { string assetPath = AssetDatabase.GUIDToAssetPath(guid); Object[] objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath); foreach (Object o in objs) { if (!(o is Sprite) || !assetPath.Contains("/anim_")) { continue; } string name; int frame; int time; { string fileName = System.IO.Path.GetFileNameWithoutExtension(assetPath); int sp = fileName.LastIndexOf('_'); name = fileName.Substring(5, sp - 5); int sp1 = fileName.LastIndexOf('#'); if (sp1 == -1) { frame = int.Parse(fileName.Substring(sp + 1)); time = (int)(1f / 6f * 1000); } else { frame = int.Parse(fileName.Substring(sp + 1, sp1 - sp - 1)); time = int.Parse(fileName.Substring(sp1 + 1)); } } if (!anims.TryGetValue(name, out var f)) { anims.Add(name, f = new List <Frame>()); } f.Add(new Frame { index = frame, frame = new Cartoon.Frame { sprite = new DSprite(o as Sprite), delay = time * 0.001f, }, }); } } } Dictionary <string, Cartoon> Cartoons = new Dictionary <string, Cartoon>(); { foreach (var ator in anims) { Cartoon cartoon = new Cartoon(); Cartoons.Add(ator.Key, cartoon); cartoon.name = ator.Key; var frames = ator.Value; int count = frames.Count; cartoon.frames = new Cartoon.Frame[count]; for (int i = 0; i < count; ++i) { var f = frames[i]; cartoon.frames[f.index - 1] = f.frame; } var fs = cartoon.frames[0].sprite; cartoon.width = fs.width; cartoon.height = fs.height; } } SymbolTextInit sti = Resources.Load <SymbolTextInit>("SymbolTextInit"); FieldInfo info = typeof(SymbolTextInit).GetField("cartoons", BindingFlags.Instance | BindingFlags.NonPublic); List <Cartoon> cartoons = new List <Cartoon>(Cartoons.Values); cartoons.Sort(Sorted); info.SetValue(sti, cartoons.ToArray()); EditorUtility.SetDirty(sti); }