54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
public class CheckAnimation : MonoBehaviour
|
|
{
|
|
// Hàm kiểm tra xem GameObject có animation hay không
|
|
public bool HasAnimation(GameObject targetObject)
|
|
{
|
|
// Kiểm tra Animator Component (Mecanim)
|
|
Animator animator = targetObject.GetComponent<Animator>();
|
|
if (animator != null)
|
|
{
|
|
// Kiểm tra xem Animator có AnimatorController được gắn không
|
|
if (animator.runtimeAnimatorController != null)
|
|
{
|
|
Debug.Log($"{targetObject.name} có Animator với AnimatorController.");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"{targetObject.name} có Animator nhưng không có AnimatorController.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Kiểm tra Animation Component (Legacy)
|
|
Animation animation = targetObject.GetComponent<Animation>();
|
|
if (animation != null)
|
|
{
|
|
// Kiểm tra xem Animation có AnimationClip được gắn không
|
|
if (animation.clip != null || animation.GetClipCount() > 0)
|
|
{
|
|
Debug.Log($"{targetObject.name} có Animation với AnimationClip.");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"{targetObject.name} có Animation nhưng không có AnimationClip.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Nếu không có Animator hoặc Animation
|
|
Debug.Log($"{targetObject.name} không có Animator hoặc Animation.");
|
|
return false;
|
|
}
|
|
|
|
// Ví dụ sử dụng
|
|
void Start()
|
|
{
|
|
// Kiểm tra chính GameObject mà script này được gắn vào
|
|
bool hasAnim = HasAnimation(gameObject);
|
|
Debug.Log($"Kết quả kiểm tra: {hasAnim}");
|
|
}
|
|
} |