|
| 1 | +// Checks for a .txt file with the same name as an imported .fbx file (in Assets/Models/ folder), containing a list of animation clips to add to the ModelImporter. |
| 2 | +// .txt file should be tab-delimited with the following columns: "title", "start frame", "end frame" (and optional description, not used). |
| 3 | +// example: |
| 4 | +// Take0 10 40 asdf |
| 5 | +// Take1 50 80 wasdf.. |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.IO; |
| 9 | +using System.Collections.Generic; |
| 10 | +using UnityEngine; |
| 11 | +using UnityEditor; |
| 12 | + |
| 13 | +namespace UnityLibrary.Importers |
| 14 | +{ |
| 15 | + public class AnimationClipListImporter : AssetPostprocessor |
| 16 | + { |
| 17 | + void OnPreprocessModel() |
| 18 | + { |
| 19 | + ModelImporter modelImporter = assetImporter as ModelImporter; |
| 20 | + if (modelImporter == null) return; |
| 21 | + |
| 22 | + string assetPath = assetImporter.assetPath; |
| 23 | + if (!assetPath.StartsWith("Assets/Models") || !assetPath.EndsWith(".fbx", StringComparison.OrdinalIgnoreCase)) return; |
| 24 | + |
| 25 | + string txtPath = Path.ChangeExtension(assetPath, ".txt"); |
| 26 | + if (!File.Exists(txtPath)) return; |
| 27 | + |
| 28 | + try |
| 29 | + { |
| 30 | + List<ModelImporterClipAnimation> clips = new List<ModelImporterClipAnimation>(); |
| 31 | + string[] lines = File.ReadAllLines(txtPath); |
| 32 | + |
| 33 | + foreach (string line in lines) |
| 34 | + { |
| 35 | + string[] parts = line.Split('\t'); |
| 36 | + if (parts.Length < 3) continue; // Ensure we have at least "title, start, end" |
| 37 | + |
| 38 | + string title = parts[0].Trim(); |
| 39 | + if (!int.TryParse(parts[1], out int startFrame) || !int.TryParse(parts[2], out int endFrame)) |
| 40 | + continue; |
| 41 | + |
| 42 | + ModelImporterClipAnimation clip = new ModelImporterClipAnimation |
| 43 | + { |
| 44 | + name = title, |
| 45 | + firstFrame = startFrame, |
| 46 | + lastFrame = endFrame, |
| 47 | + loopTime = false |
| 48 | + }; |
| 49 | + |
| 50 | + clips.Add(clip); |
| 51 | + } |
| 52 | + |
| 53 | + if (clips.Count > 0) |
| 54 | + { |
| 55 | + modelImporter.clipAnimations = clips.ToArray(); |
| 56 | + Debug.Log($"Added {clips.Count} animation clips to {assetPath}"); |
| 57 | + } |
| 58 | + } |
| 59 | + catch (Exception ex) |
| 60 | + { |
| 61 | + Debug.LogError($"Failed to process animation data for {assetPath}: {ex.Message}"); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments