78 lines
2.2 KiB
C#
78 lines
2.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public partial class RobotController
|
|
{
|
|
public class PickDropHandler
|
|
{
|
|
private Animator animator;
|
|
private AnimationClip animationUClip;
|
|
private AnimationClip animationDClip;
|
|
|
|
public void Init(Animator animator)
|
|
{
|
|
this.animator = animator;
|
|
animator.SetBool("p", false);
|
|
animator.SetBool("d", false);
|
|
|
|
InitializeAnimationClips();
|
|
}
|
|
|
|
private void InitializeAnimationClips()
|
|
{
|
|
if (animator == null || animator.runtimeAnimatorController == null)
|
|
{
|
|
Debug.LogError("Animator hoặc AnimatorController không được gán!");
|
|
return;
|
|
}
|
|
|
|
var clips = animator.runtimeAnimatorController.animationClips;
|
|
foreach (var clip in clips)
|
|
{
|
|
if (clip.name.ToLower().Contains("pick"))
|
|
animationUClip = clip;
|
|
else if (clip.name.ToLower().Contains("drop"))
|
|
animationDClip = clip;
|
|
}
|
|
}
|
|
|
|
public IEnumerator PlayPick(MonoBehaviour runner)
|
|
{
|
|
if (runner == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(runner));
|
|
}
|
|
|
|
animator.SetBool("p", true);
|
|
animator.SetBool("d", false);
|
|
yield return new WaitForSeconds(animationUClip != null ? animationUClip.length : 0f);
|
|
}
|
|
|
|
public IEnumerator PlayDrop(MonoBehaviour runner)
|
|
{
|
|
if (runner == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(runner));
|
|
}
|
|
|
|
animator.SetBool("p", false);
|
|
animator.SetBool("d", true);
|
|
yield return new WaitForSeconds(animationDClip != null ? animationDClip.length : 0f);
|
|
}
|
|
|
|
public void AttachObjToAmr(Transform endEffector, Transform obj)
|
|
{
|
|
if (obj == null) return;
|
|
obj.SetParent(endEffector);
|
|
obj.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
|
|
}
|
|
|
|
public void DetachObj(Transform obj)
|
|
{
|
|
if (obj != null)
|
|
obj.SetParent(null);
|
|
}
|
|
}
|
|
}
|