如何在运行时存储或读取动画剪辑数据?



我正在开发一个小程序,可以在运行时修改动画(例如,当您运行得更快时,动画不仅播放得更快,而且运动也更大(。所以我需要获取现有的动画,更改其值,然后将其发回。

我发现有趣的是,我可以为动画设置新的曲线,但我无法访问我已经拥有的东西。所以我要么写一个文件来存储我的动画曲线(例如作为文本文件(,要么找到某种方法来读取启动时的动画。

我尝试使用

AnimationUtility.GetCurveBindings(AnimationCurve);

它在我的测试中有效,但在某些页面中它说这是一个"编辑器代码",如果我将项目构建为独立程序,它将不再工作。这是真的吗?如果是这样,有没有办法在运行时获取曲线?

感谢本杰明·扎克的澄清和TehMightyPotato的建议 我想保留在运行时修改动画的想法。因为它可以适应更多情况。

我现在的想法是编写一段编辑器代码,可以在编辑器中读取曲线,并将有关曲线(关键帧(的所有必要信息输出到文本文件中。然后在运行时读取该文件并创建新曲线以覆盖现有曲线。我将把这个问题搁置几天,看看是否有人对它有更好的想法。

如前所述AnimationUtility属于UnityEditor命名空间。整个命名空间在构建中被完全剥离,其中的任何内容在最终应用中都不可用,而仅在 Unity 编辑器中可用。


将动画曲线存储到文件

为了将所有需要的信息存储到文件中,您可以使用一个脚本,用于在构建之前在编辑器中序列化特定的动画曲线,例如BinaryFormatter.Serialize.然后稍后在运行时,您可以使用BinaryFormatter.Deserialize再次返回信息列表。

如果你希望它更具可编辑性,你也可以使用例如JSON或XML。

更新:一般来说停止使用BinaryFormatter

在最新的Unity版本中,Newtonsoft Json.NET 包已经预装,因此只需使用JSON。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.Plastic.Newtonsoft.Json;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
public class AnimationCurveManager : MonoBehaviour
{
[Serializable]
public sealed class ClipInfo
{
public int ClipInstanceID;
public List<CurveInfo> CurveInfos = new List<CurveInfo>();
// default constructor is sometimes required for (de)serialization
public ClipInfo() { }
public ClipInfo(Object clip, List<CurveInfo> curveInfos)
{
ClipInstanceID = clip.GetInstanceID();
CurveInfos = curveInfos;
}
}
[Serializable]
public sealed class CurveInfo
{
public string PathKey;
public List<KeyFrameInfo> Keys = new List<KeyFrameInfo>();
public WrapMode PreWrapMode;
public WrapMode PostWrapMode;
// default constructor is sometimes required for (de)serialization
public CurveInfo() { }
public CurveInfo(string pathKey, AnimationCurve curve)
{
PathKey = pathKey;
foreach (var keyframe in curve.keys)
{
Keys.Add(new KeyFrameInfo(keyframe));
}
PreWrapMode = curve.preWrapMode;
PostWrapMode = curve.postWrapMode;
}
}
[Serializable]
public sealed class KeyFrameInfo
{
public float Value;
public float InTangent;
public float InWeight;
public float OutTangent;
public float OutWeight;
public float Time;
public WeightedMode WeightedMode;
// default constructor is sometimes required for (de)serialization
public KeyFrameInfo() { }
public KeyFrameInfo(Keyframe keyframe)
{
Value = keyframe.value;
InTangent = keyframe.inTangent;
InWeight = keyframe.inWeight;
OutTangent = keyframe.outTangent;
OutWeight = keyframe.outWeight;
Time = keyframe.time;
WeightedMode = keyframe.weightedMode;
}
}
// I know ... singleton .. but what choices do we have? ;)
private static AnimationCurveManager _instance;
public static AnimationCurveManager Instance
{
get
{
// lazy initialization/instantiation
if (_instance) return _instance;
_instance = FindObjectOfType<AnimationCurveManager>();
if (_instance) return _instance;
_instance = new GameObject("AnimationCurveManager").AddComponent<AnimationCurveManager>();
return _instance;
}
}
// Clips to manage e.g. reference these via the Inspector
public List<AnimationClip> clips = new List<AnimationClip>();
// every animation curve belongs to a specific clip and 
// a specific property of a specific component on a specific object
// for making this easier lets simply use a combined string as key
private string CurveKey(string pathToObject, Type type, string propertyName)
{
return $"{pathToObject}:{type.FullName}:{propertyName}";
}
public List<ClipInfo> ClipCurves = new List<ClipInfo>();
private string filePath = Path.Combine(Application.streamingAssetsPath, "AnimationCurves.dat");
private void Awake()
{
if (_instance && _instance != this)
{
Debug.LogWarning("Multiple Instances of AnimationCurveManager! Will ignore this one!", this);
return;
}
_instance = this;
DontDestroyOnLoad(gameObject);
// load infos on runtime
LoadClipCurves();
}
#if UNITY_EDITOR
// Call this from the ContextMenu (or later via editor script)
[ContextMenu("Save Animation Curves")]
private void SaveAnimationCurves()
{
ClipCurves.Clear();
foreach (var clip in clips)
{
var curveInfos = new List<CurveInfo>();
ClipCurves.Add(new ClipInfo(clip, curveInfos));
foreach (var binding in AnimationUtility.GetCurveBindings(clip))
{
var key = CurveKey(binding.path, binding.type, binding.propertyName);
var curve = AnimationUtility.GetEditorCurve(clip, binding);
curveInfos.Add(new CurveInfo(key, curve));
}
}
// create the StreamingAssets folder if it does not exist
try
{
if (!Directory.Exists(Application.streamingAssetsPath))
{
Directory.CreateDirectory(Application.streamingAssetsPath);
}
}
catch (IOException ex)
{
Debug.LogError(ex.Message);
}
// create a new file e.g. AnimationCurves.dat in the StreamingAssets folder
var json = JsonConvert.SerializeObject(ClipCurves);
File.WriteAllText(filePath, json);
AssetDatabase.Refresh();
}
#endif
private void LoadClipCurves()
{
if (!File.Exists(filePath))
{
Debug.LogErrorFormat(this, "File "{0}" not found!", filePath);
return;
}
var fileStream = new FileStream(filePath, FileMode.Open);
var json = File.ReadAllText(filePath);
ClipCurves = JsonConvert.DeserializeObject<List<ClipInfo>>(json);
}
// now for getting a specific clip's curves
public AnimationCurve GetCurve(AnimationClip clip, string pathToObject, Type type, string propertyName)
{
// either not loaded yet or error -> try again
if (ClipCurves == null || ClipCurves.Count == 0) LoadClipCurves();
// still null? -> error
if (ClipCurves == null || ClipCurves.Count == 0)
{
Debug.LogError("Apparantly no clipCurves loaded!");
return null;
}
var clipInfo = ClipCurves.FirstOrDefault(ci => ci.ClipInstanceID == clip.GetInstanceID());
// does this clip exist in the dictionary?
if (clipInfo == null)
{
Debug.LogErrorFormat(this, "The clip "{0}" was not found in clipCurves!", clip.name);
return null;
}
var key = CurveKey(pathToObject, type, propertyName);
var curveInfo = clipInfo.CurveInfos.FirstOrDefault(c => string.Equals(c.PathKey, key));
// does the curve key exist for the clip?
if (curveInfo == null)
{
Debug.LogErrorFormat(this, "The key "{0}" was not found for clip "{1}"", key, clip.name);
return null;
}
var keyframes = new Keyframe[curveInfo.Keys.Count];
for (var i = 0; i < curveInfo.Keys.Count; i++)
{
var keyframe = curveInfo.Keys[i];
keyframes[i] = new Keyframe(keyframe.Time, keyframe.Value, keyframe.InTangent, keyframe.OutTangent, keyframe.InWeight, keyframe.OutWeight)
{
weightedMode = keyframe.WeightedMode
};
}
var curve = new AnimationCurve(keyframes)
{
postWrapMode = curveInfo.PostWrapMode,
preWrapMode = curveInfo.PreWrapMode
};
// otherwise finally return the AnimationCurve
return curve;
}
}

然后你可以做一些类似的事情,例如

AnimationCurve originalCurve = AnimationCurvesManager.Instance.GetCurve(
clip, 
"some/relative/GameObject", 
typeof<SomeComponnet>, 
"somePropertyName"
);

如果属性/组件附加到根对象本身,则第二个参数pathToObject为空字符串。否则,它像往常一样在 Unity 的层次结构路径中给出,例如"ChildName/FurtherChildName"。

现在,您可以更改值并在运行时指定新曲线。


运行时分配新曲线

在运行时,您可以使用animator.runtimeanimatorController来检索RuntimeAnimatorController引用。

它有一个属性animationClips,该属性返回分配给此控制器的所有AnimationClip

然后,您可以使用例如 LinqFirstOrDefault按名称查找特定AnimationClip,最后使用AnimationClip.SetCurve为某个组件和属性分配新的动画曲线。

例如,类似的东西

// you need those of course
string clipName;
AnimationCurve originalCurve = AnimationCurvesManager.Instance.GetCurve(
clip, 
"some/relative/GameObject", 
typeof<SomeComponnet>, 
"somePropertyName"
);
// TODO 
AnimationCurve newCurve = SomeMagic(originalCurve);
// get the animator reference
var animator = animatorObject.GetComponent<Animator>();
// get the runtime Animation controller
var controller = animator.runtimeAnimatorController;
// get all clips
var clips = controller.animationClips;
// find the specific clip by name
// alternatively you could also get this as before using a field and
// reference the according script via the Inspector 
var someClip = clips.FirstOrDefault(clip => string.Equals(clipName, clip.name));
// was found?
if(!someClip)
{
Debug.LogWarningFormat(this, "There is no clip called {0}!", clipName);
return;
}
// assign a new curve
someClip.SetCurve("relative/path/to/some/GameObject", typeof(SomeComponnet), "somePropertyName", newCurve);

注意:在智能手机上输入,所以没有保修!但我希望这个想法变得清晰...


另请查看AnimationClip.SetCurve→中的示例 您可能希望在特定用例中使用Animation组件而不是Animator

最新更新