Skip to content

Commit b7d55f7

Browse files
committed
用Animation组件播放Legacy clips
1 parent 3946db1 commit b7d55f7

File tree

6 files changed

+118
-14
lines changed

6 files changed

+118
-14
lines changed

Assets/Live2D/Cubism/Framework/Json/CubismMotion3Json.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public AnimationClip ToAnimationClip(bool shouldImportAsOriginalWorkflow = false
221221
wrapMode = Meta.Loop
222222
? WrapMode.Loop
223223
: WrapMode.Default,
224-
legacy = false
224+
legacy = true
225225
#else
226226
frameRate = Meta.Fps,
227227
legacy = true,
@@ -332,9 +332,7 @@ public AnimationClip ToAnimationClip(AnimationClip animationClip, bool shouldImp
332332
AnimationUtility.SetEditorCurve(animationClip, curveBinding, animationCurve);
333333
#else
334334
// UNITY_RUNTIME
335-
// animationClip.legacy = true;
336335
animationClip.SetCurve(relativePath, type, propertyName, animationCurve);
337-
// animationClip.legacy = false;
338336
#endif
339337
}
340338

Assets/Live2D/Cubism/Framework/Motion/CubismMotionController.cs

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Live2D.Cubism.Framework.MotionFade;
22
using System;
33
using UnityEngine;
4+
using UnityEngine.Playables;
5+
using UnityEngine.Animations;
46
using System.Collections;
57

68
namespace Live2D.Cubism.Framework.Motion
@@ -24,6 +26,8 @@ public class CubismMotionController : MonoBehaviour
2426
public Action<int, AnimationClip> OnAnimationStart; // 动画开始时触发
2527
public Action<int, AnimationClip> OnAnimationEnd; // 动画结束时触发
2628

29+
private Animation[] layerAnimations;
30+
2731
#endregion
2832

2933
#region Public Methods
@@ -137,13 +141,27 @@ private void OnEnable()
137141
Debug.LogError("CubismFadeMotionList is not set in CubismFadeController.");
138142
return;
139143
}
140-
141-
SyncAnimators();
142-
InitializeOverrideController();
144+
// SyncAnimators();
145+
// InitializeOverrideController();
143146
InitializeStateArrays();
147+
InitializeAnimationComponents();
144148
isActive = true;
145149
}
146150

151+
private void InitializeAnimationComponents()
152+
{
153+
layerAnimations = new Animation[layerCount];
154+
for (int i = 0; i < layerCount; i++)
155+
{
156+
Animation anim = gameObject.GetComponent<Animation>();
157+
if (anim == null)
158+
{
159+
anim = gameObject.AddComponent<Animation>();
160+
}
161+
layerAnimations[i] = anim;
162+
}
163+
}
164+
147165
private void InitializeComponents()
148166
{
149167
fadeMotionList = GetComponent<CubismFadeController>().CubismFadeMotionList;
@@ -213,5 +231,93 @@ private void SyncAnimatorParameters()
213231
}
214232

215233
#endregion
234+
235+
#region Animation Component Methods
236+
237+
/// <summary>
238+
/// 使用 Animation 组件播放旧版动画剪辑,支持优先级和速度。
239+
/// </summary>
240+
public void PlayLegacyAnimation(AnimationClip legacyClip, int layerIndex = 0, int priority = CubismMotionPriority.PriorityNormal,
241+
bool isLoop = true, float speed = 1.0f, Action onComplete = null)
242+
{
243+
if (!CanPlayLegacyAnimation(legacyClip, layerIndex, priority))
244+
{
245+
Debug.LogWarning($"Cannot play legacy animation: {legacyClip?.name} on Layer {layerIndex}");
246+
return;
247+
}
248+
249+
SetupLegacyAnimation(legacyClip, layerIndex, priority, isLoop, speed);
250+
StartCoroutine(MonitorLegacyAnimation(layerIndex, legacyClip, onComplete));
251+
}
252+
253+
/// <summary>
254+
/// 停止指定层的旧版动画。
255+
/// </summary>
256+
public void StopLegacyAnimation(int layerIndex)
257+
{
258+
if (!IsValidLayer(layerIndex) || !isLayerPlaying[layerIndex]) return;
259+
260+
layerAnimations[layerIndex].Stop();
261+
ResetLayerState(layerIndex);
262+
Debug.Log($"Stopped legacy animation on Layer {layerIndex}");
263+
}
264+
265+
private bool CanPlayLegacyAnimation(AnimationClip clip, int layerIndex, int priority)
266+
{
267+
return enabled && isActive && clip != null && clip.legacy && IsValidLayer(layerIndex)
268+
&& (priority == CubismMotionPriority.PriorityForce || motionPriorities[layerIndex] <= priority);
269+
}
270+
271+
private void SetupLegacyAnimation(AnimationClip legacyClip, int layerIndex, int priority, bool isLoop, float speed)
272+
{
273+
// 设置 Animation 组件状态
274+
Animation animation = layerAnimations[layerIndex];
275+
animation.playAutomatically = false;
276+
animation.Stop(); // 停止当前动画
277+
278+
// 添加或替换 Clip
279+
string clipName = $"Layer{layerIndex}_Legacy_{legacyClip.name}";
280+
animation.AddClip(legacyClip, clipName);
281+
282+
// 配置播放参数
283+
AnimationState state = animation[clipName];
284+
state.speed = speed;
285+
state.wrapMode = isLoop ? WrapMode.Loop : WrapMode.Once;
286+
287+
// 更新层状态
288+
motionPriorities[layerIndex] = priority;
289+
shouldLoop[layerIndex] = isLoop;
290+
isLayerPlaying[layerIndex] = true;
291+
292+
// 开始播放
293+
animation.Play(clipName);
294+
295+
OnAnimationStart?.Invoke(layerIndex, legacyClip);
296+
Debug.Log($"Playing legacy animation {legacyClip.name} on Layer {layerIndex} | Priority: {priority} | Loop: {isLoop} | Speed: {speed}");
297+
}
298+
299+
private IEnumerator MonitorLegacyAnimation(int layerIndex, AnimationClip legacyClip, Action onComplete)
300+
{
301+
Animation animation = layerAnimations[layerIndex];
302+
AnimationState state = animation[$"Layer{layerIndex}_Legacy_{legacyClip.name}"];
303+
304+
yield return new WaitForSeconds(legacyClip.length / state.speed);
305+
306+
if (!shouldLoop[layerIndex] || !isLayerPlaying[layerIndex])
307+
{
308+
OnAnimationEnd?.Invoke(layerIndex, legacyClip);
309+
ResetLayerState(layerIndex);
310+
onComplete?.Invoke();
311+
Debug.Log($"Legacy animation {legacyClip.name} ended on Layer {layerIndex}");
312+
}
313+
else
314+
{
315+
Debug.Log($"Legacy animation {legacyClip.name} looping on Layer {layerIndex}");
316+
}
317+
}
318+
319+
#endregion
320+
321+
216322
}
217323
}

Assets/Live2D/Cubism/Samples/OriginalWorkflow/Demo/CubismSampleController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private void Update()
182182

183183
Debug.Log("Tap body : Play : " + _tapBodyMotions[motionIndex].name);
184184

185-
_motionController.PlayAnimation(_tapBodyMotions[motionIndex], isLoop: false, priority:CubismMotionPriority.PriorityNormal);
185+
_motionController.PlayLegacyAnimation(_tapBodyMotions[motionIndex], isLoop: false, priority:CubismMotionPriority.PriorityNormal);
186186
}
187187
// Tap head.
188188
else if (hitArea == HitArea.Head)
@@ -214,7 +214,7 @@ private void SpecifiedAnimationCheck()
214214

215215
Debug.Log("Body animation : Play : " + _loopMotion.name);
216216

217-
_motionController.PlayAnimation(_loopMotion, priority:CubismMotionPriority.PriorityIdle);
217+
_motionController.PlayLegacyAnimation(_loopMotion, priority:CubismMotionPriority.PriorityIdle);
218218
}
219219
}
220220

@@ -226,7 +226,7 @@ private void SpecifiedAnimationCheck()
226226
private void AnimationEnded(float instanceId)
227227
{
228228
// Play loop motion.
229-
_motionController.PlayAnimation(_loopMotion, priority:CubismMotionPriority.PriorityIdle);
229+
_motionController.PlayLegacyAnimation(_loopMotion, priority:CubismMotionPriority.PriorityIdle);
230230

231231
Debug.Log("Body animation : Play : " + _loopMotion.name);
232232
}

Assets/Scenes/SampleScene.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:2c9909376d50e5c3bb69ff7cd31e23f6236af42a8b00bf719e062a0c8d713b57
3-
size 418115
2+
oid sha256:309d7dcaec187662a0657dc5a35fa6f2a06aacceba01173ef0aa9f431bf8cac4
3+
size 418487

Assets/Scripts/Live2D/DynamicFadeMotionSetup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private void OnDestroy()
5959
private void PlayIdleAnimation()
6060
{
6161
_motionController.StopAnimation(0);
62-
_motionController.PlayAnimation(_loopMotion, priority: CubismMotionPriority.PriorityIdle, isLoop: true);
62+
_motionController.PlayLegacyAnimation(_loopMotion, priority: CubismMotionPriority.PriorityIdle, isLoop: true);
6363
}
6464

6565
/// <summary>
@@ -82,7 +82,7 @@ public void PlayMotion(string groupName, int index, int layerIndex = 0,
8282
}
8383

8484
var clip = clips[index];
85-
_motionController.PlayAnimation(clip, layerIndex, priority, isLoop, speed: 1, onComplete: PlayIdleAnimation);
85+
_motionController.PlayLegacyAnimation(clip, layerIndex, priority, isLoop, speed: 1, onComplete: PlayIdleAnimation);
8686
}
8787

8888
/// <summary>

Assets/Scripts/Live2D/MotionController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void PlayMotion()
159159
// 随机选择一个动作
160160
int randomMotionIndex = UnityEngine.Random.Range(0, clips.Count);
161161
var clip = clips[randomMotionIndex];
162-
_motionController.PlayAnimation(clip, layerIndex: 0, priority: 2, isLoop: false);
162+
_motionController.PlayLegacyAnimation(clip, layerIndex: 0, priority: 2, isLoop: false);
163163
Debug.Log($"Randomly playing motion from group '{selectedGroup}' at index {randomMotionIndex}.");
164164
}
165165

0 commit comments

Comments
 (0)