private void CreateAnimatorOverrideController(ImportedAnimationInfo animations) { if (_baseController != null) { AnimatorOverrideController overrideController; // check if override controller already exists; use this to not loose any references to this in other assets string pathForOverrideController = animations.basePath + "/" + animations.name + ".overrideController"; overrideController = AssetDatabase.LoadAssetAtPath <AnimatorOverrideController>(pathForOverrideController); if (overrideController == null) { overrideController = new AnimatorOverrideController(); AssetDatabase.CreateAsset(overrideController, pathForOverrideController); } overrideController.runtimeAnimatorController = _baseController; // set override clips var clipPairs = overrideController.clips; for (int i = 0; i < clipPairs.Length; i++) { string animationName = clipPairs[i].originalClip.name; AnimationClip clip = animations.GetClipOrSimilar(animationName); clipPairs[i].overrideClip = clip; } overrideController.clips = clipPairs; EditorUtility.SetDirty(overrideController); } else { Debug.LogWarning("No Animator Controller found as a base for the Override Controller"); } }
private void CreateAnimationAssets(ImportedAnimationInfo animationInfo, string imageAssetFilename, string pathForAnimations) { string masterName = Path.GetFileNameWithoutExtension(imageAssetFilename); var assets = AssetDatabase.LoadAllAssetsAtPath(imageAssetFilename); List <Sprite> sprites = new List <Sprite>(); foreach (var item in assets) { if (item is Sprite) { sprites.Add(item as Sprite); } } // we order the sprites by name here because the LoadAllAssets above does not necessarily return the sprites in correct order // the OrderBy is fed with the last word of the name, which is an int from 0 upwards sprites = sprites.OrderBy(x => int.Parse(x.name.Substring(x.name.LastIndexOf(' ')).TrimStart())) .ToList(); foreach (var animation in animationInfo.animations) { animationInfo.CreateAnimation(animation, sprites, pathForAnimations, masterName, sharedData.targetObjectType); } }
private void CreateSprites(ImportedAnimationInfo animations, string imageFile) { TextureImporter importer = AssetImporter.GetAtPath(imageFile) as TextureImporter; // adjust values for pixel art importer.textureType = TextureImporterType.Sprite; importer.spriteImportMode = SpriteImportMode.Multiple; importer.spritePixelsPerUnit = _spritePixelsPerUnit; importer.mipmapEnabled = false; importer.filterMode = FilterMode.Point; importer.textureFormat = TextureImporterFormat.AutomaticTruecolor; importer.maxTextureSize = animations.maxTextureSize; // create sub sprites for this file according to the AsepriteAnimationInfo importer.spritesheet = animations.GetSpriteSheet(_spriteAlignment, _spriteAlignmentCustomX, _spriteAlignmentCustomY); EditorUtility.SetDirty(importer); try { importer.SaveAndReimport(); } catch (Exception e) { Debug.LogWarning("There was a potential problem with applying settings to the generated sprite file: " + e.ToString()); } AssetDatabase.ImportAsset(imageFile, ImportAssetOptions.ForceUpdate); }
private static bool GetSpritesFromJSON(JSONObject root, ImportedAnimationInfo importedInfos) { var list = root["frames"].Array; if (list == null) { Debug.LogWarning("No 'frames' array found in JSON created by Aseprite."); IssueVersionWarning(); return(false); } foreach (var item in list) { ImportedSpriteInfo frame = new ImportedSpriteInfo(); frame.name = item.Obj["filename"].Str.Replace(".ase", ""); var frameValues = item.Obj["frame"].Obj; frame.width = (int)frameValues["w"].Number; frame.height = (int)frameValues["h"].Number; frame.x = (int)frameValues["x"].Number; frame.y = importedInfos.height - (int)frameValues["y"].Number - frame.height; // unity has a different coord system frame.duration = (int)item.Obj["duration"].Number; importedInfos.frames.Add(frame); } return(true); }
private static void GetMetaInfosFromJSON(ImportedAnimationInfo importedInfos, JSONObject meta) { var size = meta["size"].Obj; importedInfos.width = (int)size["w"].Number; importedInfos.height = (int)size["h"].Number; }
// ================================================================================ // JSON IMPORT // -------------------------------------------------------------------------------- public static ImportedAnimationInfo GetAnimationInfo(JSONObject root) { if (root == null) { Debug.LogWarning("Error importing JSON animation info: JSONObject is NULL"); return(null); } ImportedAnimationInfo importedInfos = new ImportedAnimationInfo(); // import all informations from JSON if (!root.ContainsKey("meta")) { Debug.LogWarning("Error importing JSON animation info: no 'meta' object"); return(null); } var meta = root["meta"].Obj; GetMetaInfosFromJSON(importedInfos, meta); if (GetAnimationsFromJSON(importedInfos, meta) == false) { return(null); } if (GetSpritesFromJSON(root, importedInfos) == false) { return(null); } importedInfos.CalculateTimings(); return(importedInfos); }
private ImportedAnimationInfo ImportJSONAndCreateAnimations(string basePath, string name) { string imagePath = basePath; if (_saveSpritesToSubfolder) { imagePath += "/Sprites"; } string imageAssetFilename = imagePath + "/" + name + ".png"; string textAssetFilename = imagePath + "/" + name + ".json"; TextAsset textAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(textAssetFilename); if (textAsset != null) { // parse the JSON file JSONObject jsonObject = JSONObject.Parse(textAsset.ToString()); ImportedAnimationInfo animationInfo = AsepriteImporter.GetAnimationInfo(jsonObject); if (animationInfo == null) { return(null); } animationInfo.basePath = basePath; animationInfo.name = name; animationInfo.nonLoopingAnimations = _animationNamesThatDoNotLoop; // delete JSON file afterwards AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(textAsset)); CreateSprites(animationInfo, imageAssetFilename); CreateAnimations(animationInfo, imageAssetFilename); return(animationInfo); } else { Debug.LogWarning("Problem with JSON file: " + textAssetFilename); } return(null); }
private void CreateSprites(ImportedAnimationInfo animations, string imageFile) { TextureImporter importer = AssetImporter.GetAtPath(imageFile) as TextureImporter; // apply texture import settings if there are no previous ones if (!animations.hasPreviousTextureImportSettings) { importer.textureType = TextureImporterType.Sprite; importer.spritePixelsPerUnit = sharedData.spritePixelsPerUnit; importer.mipmapEnabled = false; importer.filterMode = FilterMode.Point; importer.textureFormat = TextureImporterFormat.AutomaticTruecolor; } // create sub sprites for this file according to the AsepriteAnimationInfo importer.spritesheet = animations.GetSpriteSheet( sharedData.spriteAlignment, sharedData.spriteAlignmentCustomX, sharedData.spriteAlignmentCustomY); // reapply old import settings (pivot settings for sprites) if (animations.hasPreviousTextureImportSettings) { animations.previousImportSettings.ApplyPreviousTextureImportSettings(importer); } // these values will be set in any case, not influenced by previous import settings importer.spriteImportMode = SpriteImportMode.Multiple; importer.maxTextureSize = animations.maxTextureSize; EditorUtility.SetDirty(importer); try { importer.SaveAndReimport(); } catch (Exception e) { Debug.LogWarning("There was a problem with applying settings to the generated sprite file: " + e.ToString()); } AssetDatabase.ImportAsset(imageFile, ImportAssetOptions.ForceUpdate); }
private void CreateAnimations(ImportedAnimationInfo animationInfo, string imageAssetFilename) { if (animationInfo.hasAnimations) { if (sharedData.saveAnimationsToSubfolder) { string path = animationInfo.basePath + "/Animations"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } CreateAnimationAssets(animationInfo, imageAssetFilename, path); } else { CreateAnimationAssets(animationInfo, imageAssetFilename, animationInfo.basePath); } } }
public AnimatorController CreateAnimatorController(ImportedAnimationInfo animations) { AnimatorController controller; // check if controller already exists; use this to not loose any references to this in other assets string pathForAnimatorController = animations.basePath + "/" + animations.name + ".controller"; controller = AssetDatabase.LoadAssetAtPath <AnimatorController>(pathForAnimatorController); if (controller == null) { // create a new controller and place every animation as a state on the first layer controller = AnimatorController.CreateAnimatorControllerAtPath(animations.basePath + "/" + animations.name + ".controller"); controller.AddLayer("Default"); foreach (var animation in animations.animations) { AnimatorState state = controller.layers[0].stateMachine.AddState(animation.name); state.motion = animation.animationClip; } } else { // look at all states on the first layer and replace clip if state has the same name var childStates = controller.layers[0].stateMachine.states; foreach (var childState in childStates) { AnimationClip clip = animations.GetClip(childState.state.name); if (clip != null) { childState.state.motion = clip; } } } EditorUtility.SetDirty(controller); AssetDatabase.SaveAssets(); return(controller); }
private ImportedAnimationInfo ImportJSONAndCreateAnimations(string basePath, string name, PreviousImportSettings previousImportSettings) { string imageAssetFilename = GetImageAssetFilename(basePath, name); string textAssetFilename = GetJSONAssetFilename(basePath, name); TextAsset textAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(textAssetFilename); if (textAsset != null) { // parse the JSON file JSONObject jsonObject = JSONObject.Parse(textAsset.ToString()); ImportedAnimationInfo animationInfo = AsepriteImporter.GetAnimationInfo(jsonObject); if (animationInfo == null) { return(null); } animationInfo.previousImportSettings = previousImportSettings; animationInfo.basePath = basePath; animationInfo.name = name; animationInfo.nonLoopingAnimations = sharedData.animationNamesThatDoNotLoop; // delete JSON file afterwards AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(textAsset)); CreateSprites(animationInfo, imageAssetFilename); CreateAnimations(animationInfo, imageAssetFilename); return(animationInfo); } else { Debug.LogWarning("Problem with JSON file: " + textAssetFilename); } return(null); }
private static bool GetAnimationsFromJSON(ImportedAnimationInfo importedInfos, JSONObject meta) { if (!meta.ContainsKey("frameTags")) { Debug.LogWarning("No 'frameTags' found in JSON created by Aseprite."); IssueVersionWarning(); return(false); } var frameTags = meta["frameTags"].Array; foreach (var item in frameTags) { JSONObject frameTag = item.Obj; ImportedSingleAnimationInfo anim = new ImportedSingleAnimationInfo(); anim.name = frameTag["name"].Str; anim.firstSpriteIndex = (int)(frameTag["from"].Number); anim.lastSpriteIndex = (int)(frameTag["to"].Number); importedInfos.animations.Add(anim); } return(true); }