public static string ImportSprite(DetailedTextureInfo data, string dstPath, bool overrideSprite, bool[] selectedAnimations) { if (string.IsNullOrEmpty(Path.GetExtension(dstPath))) { dstPath = Path.Combine(dstPath, Path.GetFileName(data.SpritePath)); } File.Copy(data.SpritePath, dstPath, overrideSprite); var assetFilePath = GetProjectPathFromFullPath(dstPath); AssetDatabase.ImportAsset(assetFilePath); var importer = AssetImporter.GetAtPath(assetFilePath) as TextureImporter; importer.textureType = TextureImporterType.Sprite; importer.spriteImportMode = SpriteImportMode.Multiple; var totalHeight = 0; int dummy = 0; GetImageSize(importer, out dummy, out totalHeight); var sprites = new List <SpriteMetaData>(); int j = 0; foreach (var dataAnimation in data.Animations) { if (selectedAnimations[j++] == false) { continue; } for (int i = 0; i < dataAnimation.Sprites.Count; i++) { var orig = data.Original.SubTexture[i]; var s = new SpriteMetaData(); var actualY = totalHeight - orig.y - orig.height; orig.SpriteName = s.name = string.Format("{0}_{1}_{2}", data.FileName, dataAnimation.NewName, i); s.pivot = new Vector2(0.5f, 0.5f); s.rect = new Rect(orig.x, actualY, orig.width, orig.height); sprites.Add(s); } } importer.spritesheet = sprites.ToArray(); importer.SaveAndReimport(); return(assetFilePath); }
public static DetailedTextureInfo Load(string path) { var raw = LoadRaw(path); var details = new DetailedTextureInfo(); details.Animations = ExtractAnimations(raw.SubTexture); details.Original = raw; details.SpritePath = Path.Combine(Path.GetDirectoryName(path), raw.imagePath); details.FileName = Path.GetFileName(details.SpritePath).Replace(Path.GetExtension(details.SpritePath), string.Empty); return(details); }
public static List <AnimationClip> GenerateAnimations(string animationClipsDestination, string assetPath, bool[] selectedAnimations, DetailedTextureInfo data, int framerate) { var allSprites = AssetDatabase.LoadAllAssetsAtPath(assetPath); var ret = new List <AnimationClip>(); for (int i = 0; i < data.Animations.Count; i++) { if (selectedAnimations[i] == false) { continue; } var animSprites = allSprites.Where(s => data.Animations[i].Sprites.Any(s2 => s2.SpriteName == s.name)) .OrderBy(s => s.name).ToList(); var animClip = CreateAnimationClip(data.Animations[i], animSprites, animationClipsDestination, framerate, data); ret.Add(animClip); } return(ret); }
void OnGUI() { LoadEditorPrefs(); if (GUILayout.Button("Load Dragon Bones file")) { //GenerateAnimations( // @"D:/FAC/Programing/Projects/DragonBonesTextureUnpacker/DragonBonesTextureUnpacker/Assets/Resources/Animations", // @"Assets/Resources/Sprites/Armature_tex.png", // new bool[] { true, true }, // DataLoader.Load(@"C:\Users\DVDMSI\Desktop\New folder (3)\Armature_tex.json") //); //return; var filePath = EditorUtility.OpenFilePanel("Select a dragon bones json file", _lastOpenLocation, "json"); _data = DataLoader.Load(filePath); SetupAnimations(); _lastOpenLocation = Path.GetDirectoryName(filePath); SavePrefs(); _assetPath = null; } else if (_data == null) { return; } DrawAnimationSelector(); if (GUILayout.Button("Spritesheet Destination")) { _spriteFolderPath = _lastSaveSpriteLocation = EditorUtility.SaveFolderPanel("Pick Spritesheet Destination", _lastSaveSpriteLocation, ""); SavePrefs(); } if (string.IsNullOrEmpty(_spriteFolderPath)) { return; } DrawSpriteOptions(); /* * EditorGUILayout.LabelField(" "); * EditorGUILayout.LabelField("Type an URL or URI that you which to debug."); * EditorGUILayout.LabelField(" "); */ //const string SpritePath = //@"D:\FAC\Programing\Projects\DragonBonesTextureUnpacker\DragonBonesTextureUnpacker\Assets\Resources\Sprites"; //const string AnimationDestinationPath = // @"D:\FAC\Programing\Projects\DragonBonesTextureUnpacker\DragonBonesTextureUnpacker\Assets\Resources\Animation"; bool overrideSprite = true; bool overrideAnimation = true; if (GUILayout.Button("Import")) { _assetPath = DataLoader.ImportSprite(_data, _spriteFolderPath, _overrideSprite, _selectedAnimations); Selection.activeObject = AssetDatabase.LoadAssetAtPath <Sprite>(_assetPath); } if (string.IsNullOrEmpty(_assetPath)) { return; } DrawAnimationOptions(); }
public static AnimatorController GenerateAnimationController(string assetPath, DetailedTextureInfo data, string animationClipsDestination, List <AnimationClip> clips) { var controllerName = string.Format("{0}/{1}{2}", GetProjectPathFromFullPath(animationClipsDestination), data.FileName, ".controller"); var controller = AssetDatabase.LoadAssetAtPath <AnimatorController>(controllerName); if (controller != null) { Debug.LogError("AnimatorController Already exists, skipping its creation\n" + controllerName); return(controller); } controller = AnimatorController.CreateAnimatorControllerAtPath(controllerName); var rootStateMachine = controller.layers[0].stateMachine; AnimatorState entry = null; foreach (var animationClip in clips) { entry = rootStateMachine.AddState(animationClip.name); entry.motion = animationClip; } return(controller); }
public static AnimationClip CreateAnimationClip(DBAnimation dataAnimation, List <Object> animSprites, string animationClipsDestination, int framerate, DetailedTextureInfo data) { var animationClipName = string.Format("{0}/{1}_{2}{3}", DataLoader.GetProjectPathFromFullPath(animationClipsDestination), data.FileName, dataAnimation.NewName, ".anim"); var origClip = AssetDatabase.LoadAssetAtPath <AnimationClip>(animationClipName); var animationClip = origClip ?? new AnimationClip(); animationClip.name = dataAnimation.NewName; animationClip.frameRate = framerate; EditorCurveBinding curveBinding = new EditorCurveBinding { // I want to change the sprites of the sprite renderer, so I put the typeof(SpriteRenderer) as the binding type. type = typeof(SpriteRenderer), // Regular path to the gameobject that will be changed (empty string means root) path = "", // This is the property name to change the sprite of a sprite renderer propertyName = "m_Sprite" }; var keyFrames = new List <ObjectReferenceKeyframe>(); for (int j = 0; j < animSprites.Count; j++) { var frame = new ObjectReferenceKeyframe { value = animSprites[j], time = j / animationClip.frameRate }; keyFrames.Add(frame); } AnimationUtility.SetAnimationClipSettings(animationClip, new AnimationClipSettings { loopTime = true }); AnimationUtility.SetObjectReferenceCurve(animationClip, curveBinding, keyFrames.ToArray()); if (origClip == null) { AssetDatabase.CreateAsset(animationClip, animationClipName); } AssetDatabase.SaveAssets(); return(animationClip); }