protected override void OnInit() { AddObject(SpriteToolsConfig.GetInstance()); AddProperty("defaultShader", "默认Shader"); AddProperty("defaultFrameRate", "默认动画帧率"); AddProperty("defaultStateList", "默认动作列表"); AddProperty("loopStateList", "循环动作列表"); }
public static AnimationClip MakeAnimationClip(Object [] keyframes, float frameRate = 12.0f, string savePath = null) { //动画曲线 EditorCurveBinding curveBinding = new EditorCurveBinding(); curveBinding.type = typeof(SpriteRenderer); curveBinding.path = ""; curveBinding.propertyName = "m_Sprite"; float frameTime = 1 / frameRate; int index = 0; List <ObjectReferenceKeyframe> keyFrames = new List <ObjectReferenceKeyframe>(); foreach (var keyframe in keyframes) { ObjectReferenceKeyframe keyFrame = new ObjectReferenceKeyframe(); keyFrame.time = (float)(frameTime * index); keyFrame.value = keyframe; keyFrames.Add(keyFrame); index++; } AnimationClip clip = new AnimationClip(); clip.frameRate = SpriteToolsConfig.GetInstance().defaultFrameRate;//动画帧率,30比较合适 #if !UNITY_5 AnimationUtility.SetAnimationType(clip, ModelImporterAnimationType.Generic); #endif AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyFrames.ToArray()); if (!string.IsNullOrEmpty(savePath)) { //保存动画Clip AssetDatabase.CreateAsset(clip, savePath); AssetDatabase.SaveAssets(); } return(clip); }
public static Material GenerateMaterialFromAnimationControllerFile(string assetPath, string savePath = "") { string assetFileNonExtName = Path.GetFileNameWithoutExtension(assetPath); string assetRootPath = Path.GetDirectoryName(assetPath); if (savePath == "") { string assetRootPathName = Path.GetFileNameWithoutExtension(assetRootPath); savePath = Path.Combine(assetRootPath, string.Format("{0}.mat", assetRootPathName)); } Material mat = new Material(SpriteToolsConfig.GetInstance().defaultShader); if (XFileTools.Exists(savePath)) { mat = AssetDatabase.LoadAssetAtPath <Material>(savePath); } else { AssetDatabase.CreateAsset(mat, savePath); } AssetDatabase.Refresh(); return(mat); }
/// <summary> /// 生成动画及控制器 /// sprite子项命名规则:组_动作名_编号,没有编号的一律不生成Anima /// </summary> /// <param name="assetPath"></param> public static Dictionary <string, Dictionary <string, AnimationClip> > GenerateAnimationClipFromTextureFile(string assetPath, string saveRootPath = "", System.Action <AnimationClip> callback = null) { string assetFileNonExtName = Path.GetFileNameWithoutExtension(assetPath); string assetRootPath = Path.GetDirectoryName(assetPath); TextureImporter importer = LoadImporterFromTextureFile(assetPath); if (importer) { //判断是否是精灵图集 if (!(importer.textureType == TextureImporterType.Sprite && importer.spriteImportMode == SpriteImportMode.Multiple)) { return(null); } //获取所有精灵帧 Object[] sheetObjs = AssetDatabase.LoadAllAssetsAtPath(assetPath); var sheetDict = new Dictionary <string, Dictionary <string, SortedList <string, SpriteSheetFrameData> > >(); foreach (var obj in sheetObjs) { SpriteSheetFrameData metadata = SpriteSheetFrameData.TryCreate(obj as Sprite); if (metadata != null) { string groupName = metadata.groupName; Dictionary <string, SortedList <string, SpriteSheetFrameData> > actionMaps; if (sheetDict.ContainsKey(groupName)) { actionMaps = sheetDict[groupName]; } else { actionMaps = new Dictionary <string, SortedList <string, SpriteSheetFrameData> >(); sheetDict.Add(groupName, actionMaps); } // string actionName = metadata.actionName; SortedList <string, SpriteSheetFrameData> frameList; if (actionMaps.ContainsKey(actionName)) { frameList = actionMaps[actionName]; } else { frameList = new SortedList <string, SpriteSheetFrameData>(); actionMaps.Add(actionName, frameList); } string idName = metadata.idName; if (frameList.ContainsKey(idName)) { Debug.LogWarning(string.Format("{0}_{1}重复ID:{2}", groupName, actionName, idName)); metadata.idName = string.Format("{0}_{1}", metadata.idName, frameList.Count); idName = metadata.idName; } frameList.Add(idName, metadata); } } if (saveRootPath == "") { saveRootPath = assetRootPath; } Dictionary <string, Dictionary <string, AnimationClip> > outMap = new Dictionary <string, Dictionary <string, AnimationClip> >(); foreach (var groupPair in sheetDict) { //保存资源 string groupName = groupPair.Key; string[] subFolders = groupName.Split(new char[] { '@' }); string saveOutRootPath = saveRootPath; for (int i = 0; i < subFolders.Length; i++) { saveOutRootPath = XPathTools.Combine(saveOutRootPath, subFolders[i]); if (!XFolderTools.Exists(saveOutRootPath)) { XFolderTools.CreateDirectory(saveOutRootPath); } } Dictionary <string, AnimationClip> outActionMap; if (outMap.ContainsKey(groupName)) { outActionMap = outMap[groupName]; } else { outActionMap = new Dictionary <string, AnimationClip>(); outMap.Add(groupName, outActionMap); } foreach (var actionPair in groupPair.Value) { //保存动画Clip string outName = actionPair.Key; string saveName = string.Format("{0}.anim", outName); string savePath = Path.Combine(saveOutRootPath, saveName); List <Sprite> spriteList = new List <Sprite>(); foreach (var listPair in actionPair.Value) { spriteList.Add(listPair.Value.sprite); } AnimationClip clip = MakeAnimationClip(spriteList.ToArray(), SpriteToolsConfig.GetInstance().defaultFrameRate, savePath); outActionMap.Add(actionPair.Key, clip); if (callback != null) { callback(clip); } } } return(outMap); } return(null); }
public static void GenAnimaAndCtrler(string assetPath, List <string> exportPathList = null) { string selectRootPath = Path.GetDirectoryName(assetPath); string selectFileName = Path.GetFileNameWithoutExtension(assetPath); //处理逻辑 var ctrlMap = SpriteEditorTools.GenerateAnimationClipFromTextureFile(assetPath, "", (clip) => { bool isLoop = SpriteToolsConfig.GetInstance().IsNeedLoop(clip.name); if (isLoop) { SpriteEditorTools.SetupAnimationClipLoop(clip, isLoop); } }); foreach (var groupPair in ctrlMap) { foreach (var clipPair in groupPair.Value) { var clip = clipPair.Value; string clipFilePath = AssetDatabase.GetAssetPath(clip); string clipRootPath = Path.GetDirectoryName(clipFilePath); bool isDefault = SpriteToolsConfig.GetInstance().IsDefaultState(clip.name); //上层目录检查 //如果上层有公共的,直接用公共的 //如果上层有模板,生成继承控制器 string prevRootPath = XPathTools.GetParentPath(clipRootPath); string parentCtrl = XPathTools.Combine(prevRootPath, SpriteEditorTools.controllerName); string parentCtrlTmpl = XPathTools.Combine(prevRootPath, SpriteEditorTools.controllerTmplName); if (XFileTools.Exists(parentCtrl)) { var ctrl = AssetDatabase.LoadAssetAtPath <AnimatorController>(parentCtrl); SpriteEditorTools.SetupAnimationState(ctrl, clip, isDefault); } else if (XFileTools.Exists(parentCtrlTmpl)) { string overrideCtrlSavePath = XPathTools.Combine(clipRootPath, SpriteEditorTools.overrideControllerName); var overrideCtrl = SpriteEditorTools.GenerateAnimationOverrideControllerFromAnimationClipFile("", parentCtrlTmpl, overrideCtrlSavePath); SpriteEditorTools.SetupOverrideMotion(overrideCtrl, clip); } else { string ctrlSavePath = XPathTools.Combine(clipRootPath, SpriteEditorTools.controllerName); var ctrl = SpriteEditorTools.GenerateAnimationControllerFromAnimationClipFile("", ctrlSavePath); SpriteEditorTools.SetupAnimationState(ctrl, clip, isDefault); } } if (exportPathList != null) { string groupPath = SpriteEditorTools.GroupName2Path(groupPair.Key); string exportRootPath = XPathTools.Combine(selectRootPath, groupPath); exportPathList.Add(exportRootPath); } } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }