阶段备份2

This commit is contained in:
XgC2961 2025-08-29 17:27:42 +08:00
parent ab64006e5a
commit 0e541101db
2 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,76 @@
using UnityEngine;
public class ModelController : MonoBehaviour
{
private Animator animator;
private string currentAnim = "";
private bool isPlaying = false;
[Header("默认动画状态名称")]
public string defaultAnimName = "Idle";
void Awake()
{
animator = GetComponent<Animator>();
}
/// <summary>
/// 播放默认动画从0开始
/// </summary>
public void PlayAnimation()
{
if (animator == null || string.IsNullOrEmpty(defaultAnimName)) return;
if (!AnimatorHasState(defaultAnimName))
{
Debug.LogWarning($"Animator 上不存在状态: {defaultAnimName}");
return;
}
currentAnim = defaultAnimName;
animator.enabled = true;
animator.Play(currentAnim, 0, 0f);
animator.Update(0f);
isPlaying = true;
}
/// <summary>
/// 设置动画进度 0~1
/// </summary>
public void SetAnimationProgress(float normalizedTime)
{
if (animator == null || string.IsNullOrEmpty(currentAnim)) return;
if (!gameObject.activeSelf) return;
animator.enabled = true;
animator.Play(currentAnim, 0, Mathf.Clamp01(normalizedTime));
animator.Update(0f);
animator.enabled = false;
isPlaying = false;
}
/// <summary>
/// 页面切换或复位时调用
/// </summary>
public void ResetAnimation()
{
if (animator == null) return;
if (!gameObject.activeSelf) return;
animator.enabled = true;
animator.Play(currentAnim != "" ? currentAnim : defaultAnimName, 0, 0f);
animator.Update(0f);
animator.enabled = false;
currentAnim = "";
isPlaying = false;
}
private bool AnimatorHasState(string stateName)
{
for (int i = 0; i < animator.layerCount; i++)
{
if (animator.HasState(i, Animator.StringToHash(stateName)))
return true;
}
return false;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c86ed53e8144b2b4781bab9d7e73c21e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: