导入fbx人物动画read-only解决方案

原文链接: http://answers.unity3d.com/questions/8172/how-to-add-new-curves-or-animation-events-to-an-im.html

unity4.3版本

using UnityEditor;
using UnityEngine;

using System.IO;
using System.Collections;

public class MultipleCurvesTransferer
{
	const string duplicatePostfix = "Edit";
	const string animationFolder = "Animations";

	static void CopyClip(string importedPath,string copyPath)
	{
		AnimationClip src = AssetDatabase.LoadAssetAtPath(importedPath,typeof(AnimationClip)) as AnimationClip;
		AnimationClip newClip = new AnimationClip();
		newClip.name = src.name + duplicatePostfix;
		AssetDatabase.CreateAsset(newClip,copyPath);
		AssetDatabase.Refresh();
	}

	[MenuItem("PATOOL/Transfer Multiple Clips Curves to Copy")]
	static void CopyCurvesToDuplicate()
	{
		// Get selected AnimationClip
		Object[] imported = Selection.GetFiltered(typeof(AnimationClip),SelectionMode.Unfiltered);
		if(imported.Length == 0)
		{
			Debug.LogWarning("Either no objects were selected or the objects selected were not AnimationClips.");
			return;
		}

		//If necessary, create the animations folder.
		if(Directory.Exists("Assets/" + animationFolder) == false)
		{
			AssetDatabase.CreateFolder("Assets",animationFolder);
		}

		foreach(AnimationClip clip in imported)
		{
			string importedPath = AssetDatabase.GetAssetPath(clip);

			//If the animation came from an FBX, then use the FBX name as a subfolder to contain the animations.
			string copyPath;
			if(importedPath.Contains(".fbx") || importedPath.Contains(".FBX"))
			{
				//With subfolder.
				string folder = importedPath.Substring(importedPath.LastIndexOf("/") + 1,importedPath.LastIndexOf(".") - importedPath.LastIndexOf("/") - 1);
				if(!Directory.Exists("Assets/Animations/" + folder))
				{
					AssetDatabase.CreateFolder("Assets/Animations",folder);
				}
				copyPath = "Assets/Animations/" + folder + "/" + clip.name + duplicatePostfix + ".anim";
			}
			else
			{
				//No Subfolder
				copyPath = "Assets/Animations/" + clip.name + duplicatePostfix + ".anim";
			}

			Debug.Log("CopyPath: " + copyPath);

			CopyClip(importedPath,copyPath);

			AnimationClip copy = AssetDatabase.LoadAssetAtPath(copyPath,typeof(AnimationClip)) as AnimationClip;
			if(copy == null)
			{
				Debug.Log("No copy found at " + copyPath);
				return;
			}
			// Copy curves from imported to copy
			AnimationClipCurveData[] curveDatas = AnimationUtility.GetAllCurves(clip);
			EditorCurveBinding[] binds = AnimationUtility.GetObjectReferenceCurveBindings(clip);
			binds = AnimationUtility.GetCurveBindings(clip);
			AnimationCurve curve = null;
			foreach(EditorCurveBinding item in binds)
			{
				curve = AnimationUtility.GetEditorCurve(clip,item);
				AnimationUtility.SetEditorCurve(copy,item,curve);
			}
			Debug.Log("Copying curves into " + copy.name + " is done");
		}
	}
}

方法1:

You can use an editor script that copies over the curves from the original imported Animation Clip into the duplicated Animation Clip.

Here is such an editor script.

  • Place it in a folder called Editor, located somewhere inside the Assets folder.
  • The script assumes that you have already made a duplicate of the imported clip and called it the same name but with a *copy postfix. For example, if you have an imported clip called MyAnimation, it will search for *MyAnimation_copy*.
  • Select the original imported Animation Clip in the Project View.
  • You can now use the menu Assets -> Transfer Clip Curves to Copy

And the script:


 
 
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class CurvesTransferer {
  6.  
  7. const string duplicatePostfix = "_copy";
  8.  
  9. [MenuItem ("Assets/Transfer Clip Curves to Copy")]
  10. static void CopyCurvesToDuplicate () {
  11. // Get selected AnimationClip
  12. AnimationClip imported = Selection.activeObject as AnimationClip;
  13. if (imported == null) {
  14. Debug.Log("Selected object is not an AnimationClip");
  15. return;
  16. }
  17.  
  18. // Find path of copy
  19. string importedPath = AssetDatabase.GetAssetPath(imported);
  20. string copyPath = importedPath.Substring(0, importedPath.LastIndexOf("/"));
  21. copyPath += "/" + imported.name + duplicatePostfix + ".anim";
  22.  
  23. // Get copy AnimationClip
  24. AnimationClip copy = AssetDatabase.LoadAssetAtPath(copyPath, typeof(AnimationClip)) as AnimationClip;
  25. if (copy == null) {
  26. Debug.Log("No copy found at "+copyPath);
  27. return;
  28. }
  29.  
  30. // Copy curves from imported to copy
  31. AnimationClipCurveData[] curveDatas = AnimationUtility.GetAllCurves(imported, true);
  32. for (int i=0; i<curveDatas.Length; i++) {
  33. AnimationUtility.SetEditorCurve(
  34. copy,
  35. curveDatas[i].path,
  36. curveDatas[i].type,
  37. curveDatas[i].propertyName,
  38. curveDatas[i].curve
  39. );
  40. }
  41.  
  42. Debug.Log("Copying curves into "+copy.name+" is done");
  43. }
  44. }

方法2:

There is more simple variant. This script creates a new animation file itself and makes all copying operations.


 
 
  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. public class CurvesTransferer
  5. {
  6. const string duplicatePostfix = "_copy";
  7.  
  8. static void CopyClip(string importedPath, string copyPath)
  9. {
  10. AnimationClip src = AssetDatabase.LoadAssetAtPath(importedPath, typeof(AnimationClip)) as AnimationClip;
  11. AnimationClip newClip = new AnimationClip();
  12. newClip.name = src.name + duplicatePostfix;
  13. AssetDatabase.CreateAsset(newClip, copyPath);
  14. AssetDatabase.Refresh();
  15. }
  16.  
  17. [MenuItem("Assets/Transfer Clip Curves to Copy")]
  18. static void CopyCurvesToDuplicate()
  19. {
  20. // Get selected AnimationClip
  21. AnimationClip imported = Selection.activeObject as AnimationClip;
  22. if (imported == null)
  23. {
  24. Debug.Log("Selected object is not an AnimationClip");
  25. return;
  26. }
  27.  
  28. // Find path of copy
  29. string importedPath = AssetDatabase.GetAssetPath(imported);
  30. string copyPath = importedPath.Substring(0, importedPath.LastIndexOf("/"));
  31. copyPath += "/" + imported.name + duplicatePostfix + ".anim";
  32.  
  33. CopyClip(importedPath, copyPath);
  34.  
  35. AnimationClip copy = AssetDatabase.LoadAssetAtPath(copyPath, typeof(AnimationClip)) as AnimationClip;
  36. if (copy == null)
  37. {
  38. Debug.Log("No copy found at " + copyPath);
  39. return;
  40. }
  41. // Copy curves from imported to copy
  42. AnimationClipCurveData[] curveDatas = AnimationUtility.GetAllCurves(imported, true);
  43. for (int i = 0; i < curveDatas.Length; i++)
  44. {
  45. AnimationUtility.SetEditorCurve(
  46. copy,
  47. curveDatas[i].path,
  48. curveDatas[i].type,
  49. curveDatas[i].propertyName,
  50. curveDatas[i].curve
  51. );
  52. }
  53.  
  54. Debug.Log("Copying curves into " + copy.name + " is done");
  55. }
  56.  
  57. }

方法3:

Guys, I loved this script so much, I went ahead and added a couple of features.

Here is a modified version of MaDDoX's edit that includes logic for automatically placing the animations into folders.

It first uses an animations folder to contain them all and then if the animation came from an FBX file, it will use the FBX's name to create a subfolder for those animations. Hopefully, this helps y'all keep things organized.



 
 
  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. using System.IO;
  5. using System.Collections;
  6.  
  7. public class MultipleCurvesTransferer {
  8. const string duplicatePostfix = "Edit";
  9. const string animationFolder = "Animations";
  10.  
  11. static void CopyClip(string importedPath, string copyPath) {
  12. AnimationClip src = AssetDatabase.LoadAssetAtPath(importedPath, typeof(AnimationClip)) as AnimationClip;
  13. AnimationClip newClip = new AnimationClip();
  14. newClip.name = src.name + duplicatePostfix;
  15. AssetDatabase.CreateAsset(newClip, copyPath);
  16. AssetDatabase.Refresh();
  17. }
  18.  
  19. [MenuItem("Assets/Transfer Multiple Clips Curves to Copy")]
  20. static void CopyCurvesToDuplicate()
  21. {
  22. // Get selected AnimationClip
  23. Object[] imported = Selection.GetFiltered(typeof(AnimationClip), SelectionMode.Unfiltered);
  24. if (imported.Length == 0)
  25. {
  26. Debug.LogWarning("Either no objects were selected or the objects selected were not AnimationClips.");
  27. return;
  28. }
  29.  
  30. //If necessary, create the animations folder.
  31. if (Directory.Exists("Assets/" + animationFolder) == false) {
  32. AssetDatabase.CreateFolder("Assets", animationFolder);
  33. }
  34.  
  35. foreach (AnimationClip clip in imported) {
  36.  
  37.  
  38.  
  39. string importedPath = AssetDatabase.GetAssetPath(clip);
  40.  
  41. //If the animation came from an FBX, then use the FBX name as a subfolder to contain the animations.
  42. string copyPath;
  43. if (importedPath.Contains(".fbx")) {
  44. //With subfolder.
  45. string folder = importedPath.Substring(importedPath.LastIndexOf("/") + 1, importedPath.LastIndexOf(".") - importedPath.LastIndexOf("/") - 1);
  46. if (!Directory.Exists("Assets/Animations/" + folder)) {
  47. AssetDatabase.CreateFolder("Assets/Animations", folder);
  48. }
  49. copyPath = "Assets/Animations/" + folder + "/" + clip.name + duplicatePostfix + ".anim";
  50. } else {
  51. //No Subfolder
  52. copyPath = "Assets/Animations/" + clip.name + duplicatePostfix + ".anim";
  53. }
  54.  
  55. Debug.Log("CopyPath: " + copyPath);
  56.  
  57. CopyClip(importedPath, copyPath);
  58.  
  59. AnimationClip copy = AssetDatabase.LoadAssetAtPath(copyPath, typeof(AnimationClip)) as AnimationClip;
  60. if (copy == null)
  61. {
  62. Debug.Log("No copy found at " + copyPath);
  63. return;
  64. }
  65. // Copy curves from imported to copy
  66. AnimationClipCurveData[] curveDatas = AnimationUtility.GetAllCurves(clip, true);
  67. for (int i = 0; i < curveDatas.Length; i++)
  68. {
  69. AnimationUtility.SetEditorCurve(
  70. copy,
  71. curveDatas[i].path,
  72. curveDatas[i].type,
  73. curveDatas[i].propertyName,
  74. curveDatas[i].curve
  75. );
  76. }
  77.  
  78. Debug.Log("Copying curves into " + copy.name + " is done");
  79. }
  80. }
  81. }

....


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值