這幾天在製作介面做好了后才發現美術的 animation 因爲路徑變動而出現 missing,以前項目一直沒花心思去處理這塊,後來才發現其實直接處理變動路徑就可以達到目的,不用再回頭調整 animation
工具思路是將 Animation 中出現 obj missing 的路徑做檢查,如果名稱跟填入的規則有一致,那就自動換成新的路徑名稱,再保存回去
下面是出現 missing 的 animation
我們將兩個出現 missing 的路徑分別填入並置換成新的路徑名稱,並點選 開始 Rebind
處理完畢后的效果,missing 都沒了,animation 也正常了
實現方式如下
void RebindAnimation(AnimationClip clip, GameObject root)
{
var bindings = AnimationUtility.GetCurveBindings(clip);
int fixCount = 0;
foreach (var binding in bindings)
{
var obj = AnimationUtility.GetAnimatedObject(root, binding);
// 若找不到原物件
if (obj == null)
{
string newPath = TryReplacePath(binding.path);
if (!string.IsNullOrEmpty(newPath))
{
var curve = AnimationUtility.GetEditorCurve(clip, binding);
AnimationUtility.SetEditorCurve(clip, binding, null);
var newBinding = binding;
newBinding.path = newPath;
AnimationUtility.SetEditorCurve(clip, newBinding, curve);
fixCount++;
AddLog($"✅ 修正路徑:{binding.path} → {newPath}");
}
else
{
AddLog($"⚠️ 無法找到對應物件:{binding.path}");
}
}
}
AssetDatabase.SaveAssets();
AddLog($"完成修正,共處理 {fixCount} 條。");
}
string TryReplacePath(string oldPath)
{
foreach (var rule in replaceRules)
{
if (!string.IsNullOrEmpty(rule.from) && oldPath.Contains(rule.from))
{
string newPath = oldPath.Replace(rule.from, rule.to);
return newPath;
}
}
return null;
}
留言
張貼留言