using UnityEngine;
using UnityEngine.UI;
using System.Collections;
///
/// 片头动画流程控制器:控制 man 的 Idle → Shoot → Fall 流程,
/// 摔倒后黑幕转场,显示 woman 和 maninjury,切换到 camera2。
/// 挂载在 [Managers] 上,由 PageController 在用户选定固定方式后调用。
///
public class OpeningFlowController : MonoBehaviour
{
[Header("=== 角色引用 ===")]
[SerializeField] private Animator _manAnimator;
[SerializeField] private Animator _man2Animator;
[SerializeField] private GameObject _man2Object;
[SerializeField] private GameObject _womanObject;
[SerializeField] private GameObject _manInjuryObject;
[Header("=== 黑幕 UI ===")]
[SerializeField] private GameObject _blackScreenPanel;
[SerializeField] private Image _blackScreenImage;
[Header("=== 时间配置 ===")]
[SerializeField] private float _idleDuration = 2f;
[SerializeField] private float _fadeDuration = 1f;
[SerializeField] private float _fallHoldDuration = 1.5f;
private void Start()
{
AutoAssignReferences();
if (_womanObject != null) _womanObject.SetActive(false);
if (_manInjuryObject != null) _manInjuryObject.SetActive(false);
// 确保所有 ping/chanrao/chanraotuibu 子级隐藏
DeactivateManInjuryChildren();
if (_blackScreenPanel != null)
{
_blackScreenPanel.SetActive(true);
if (_blackScreenImage != null)
_blackScreenImage.color = new Color(0, 0, 0, 0);
Debug.Log("[OpeningFlow] BlackScreenPanel 已就绪,初始透明");
}
Debug.Log("[OpeningFlow] 就绪,等待用户选择模式和固定方式。");
}
private void AutoAssignReferences()
{
var manGo = GameObject.Find("man");
if (manGo != null && _manAnimator == null)
_manAnimator = manGo.GetComponent();
var man2Go = GameObject.Find("man2");
if (man2Go != null)
{
if (_man2Animator == null) _man2Animator = man2Go.GetComponent();
if (_man2Object == null) _man2Object = man2Go;
}
if (_womanObject == null) _womanObject = GameObject.Find("woman");
if (_manInjuryObject == null) _manInjuryObject = GameObject.Find("manInjury");
if (_blackScreenPanel == null)
_blackScreenPanel = GameObject.Find("BlackScreenPanel");
if (_blackScreenPanel == null)
{
var allImages = FindObjectsOfType(true);
foreach (var img in allImages)
{
if (img.name == "BlackScreenPanel")
{
_blackScreenPanel = img.gameObject;
_blackScreenImage = img;
Debug.Log("[OpeningFlow] BlackScreenPanel 通过 includeInactive 兜底找到");
break;
}
}
}
if (_blackScreenPanel != null && _blackScreenImage == null)
_blackScreenImage = _blackScreenPanel.GetComponent();
if (_blackScreenPanel == null)
Debug.LogWarning("[OpeningFlow] BlackScreenPanel 未找到!黑幕转场将不会显示");
else
Debug.Log($"[OpeningFlow] BlackScreenPanel 已找到: activeInHierarchy={_blackScreenPanel.activeInHierarchy}, Image={_blackScreenImage != null}");
}
public void PlayOpeningFlow(System.Action onComplete)
{
Debug.Log($"[OpeningFlow] PlayOpeningFlow() 被调用, enable={_enableOpeningFlow}");
if (!_enableOpeningFlow)
{
Debug.Log("[OpeningFlow] 开场动画已禁用,直接跳过");
// 跳过动画但状态还是要切:隐藏man/man2,显示woman/manInjury,切camera2
if (_manAnimator != null) _manAnimator.gameObject.SetActive(false);
if (_man2Object != null) _man2Object.SetActive(false);
if (_womanObject != null) _womanObject.SetActive(true);
if (_manInjuryObject != null) { _manInjuryObject.SetActive(true); DeactivateManInjuryChildren(); }
SwitchToCamera2();
onComplete?.Invoke();
return;
}
if (_manAnimator != null) { _manAnimator.gameObject.SetActive(true); _manAnimator.SetBool("IsStart", false); _manAnimator.SetBool("IsFull", false); _manAnimator.Play("manIdle", 0, 0f); }
if (_man2Object != null) _man2Object.SetActive(true);
if (_womanObject != null) _womanObject.SetActive(false);
if (_manInjuryObject != null) _manInjuryObject.SetActive(false);
if (_blackScreenPanel != null) _blackScreenPanel.SetActive(false);
var mainCamGo = GameObject.Find("Main Camera");
if (mainCamGo != null) { var c = mainCamGo.GetComponent(); if (c != null) c.enabled = true; }
var cam2Go = GameObject.Find("camera2");
if (cam2Go != null) { var c = cam2Go.GetComponent(); if (c != null) c.enabled = false; }
StartCoroutine(RunOpeningFlow(onComplete));
}
private IEnumerator RunOpeningFlow(System.Action onComplete)
{
Debug.Log("=== [OpeningFlow] 片头动画流程开始 ===");
if (_manAnimator != null) { _manAnimator.SetBool("IsStart", false); _manAnimator.SetBool("IsFull", false); }
yield return null;
Debug.Log("[OpeningFlow] Step1: man + man2 Idle (1s)");
yield return new WaitForSeconds(1f);
// Step 2: Shoot + 篮球音效
Debug.Log("[OpeningFlow] Step2: Shoot 开始");
if (AudioManager.I != null) AudioManager.I.PlayBasketballSfx();
if (_manAnimator != null) { _manAnimator.Play("Shoot", 0, 0f); Debug.Log("[OpeningFlow] Shoot Play 已触发"); }
yield return new WaitForSeconds(3.15f);
Debug.Log("[OpeningFlow] Step2: Shoot 结束");
// Step 3: 摔倒 + manCall 音效
Debug.Log("[OpeningFlow] Step3: manfull 开始");
if (_manAnimator != null) { _manAnimator.Play("manfull", 0, 0f); }
yield return new WaitForSeconds(2.1f);
if (AudioManager.I != null) AudioManager.I.PlayManCallSfx();
Debug.Log("[OpeningFlow] manfull 播放完成, 停留 2s");
yield return new WaitForSeconds(2f);
Debug.Log("[OpeningFlow] Step4: 黑幕");
yield return StartCoroutine(FadeBlackScreen(0f, 1f));
if (_manAnimator != null) { _manAnimator.gameObject.SetActive(false); Debug.Log("[OpeningFlow] man 已隐藏"); }
if (_man2Object != null) { _man2Object.SetActive(false); Debug.Log("[OpeningFlow] man2 已隐藏"); }
if (_womanObject != null) { _womanObject.SetActive(true); Debug.Log("[OpeningFlow] woman 已显示"); }
if (_manInjuryObject != null) { _manInjuryObject.SetActive(true); DeactivateManInjuryChildren(); Debug.Log("[OpeningFlow] manInjury 已显示,子级已清理"); }
SwitchToCamera2();
yield return new WaitForSeconds(0.3f);
yield return StartCoroutine(FadeBlackScreen(1f, 0f));
Debug.Log("=== [OpeningFlow] 片头动画流程完成 ===");
onComplete?.Invoke();
}
private void SwitchToCamera2()
{
var cam2Go = GameObject.Find("camera2");
if (cam2Go != null) { var c = cam2Go.GetComponent(); if (c != null) { c.enabled = true; Debug.Log("[OpeningFlow] camera2 已启用"); } }
else Debug.LogWarning("[OpeningFlow] 未找到 camera2");
var mainGo = GameObject.Find("Main Camera");
if (mainGo != null) { var c = mainGo.GetComponent(); if (c != null) { c.enabled = false; Debug.Log("[OpeningFlow] 主摄像机已禁用"); } }
}
private IEnumerator FadeBlackScreen(float fromAlpha, float toAlpha)
{
if (_blackScreenPanel == null || _blackScreenImage == null)
{
Debug.LogWarning("[OpeningFlow] BlackScreenPanel/Image 为空,跳过转场");
yield break;
}
_blackScreenPanel.SetActive(true);
float elapsed = 0f;
while (elapsed < _fadeDuration)
{
elapsed += Time.deltaTime;
float t = Mathf.Clamp01(elapsed / _fadeDuration);
_blackScreenImage.color = new Color(0, 0, 0, Mathf.Lerp(fromAlpha, toAlpha, t));
yield return null;
}
_blackScreenImage.color = new Color(0, 0, 0, toAlpha);
if (toAlpha <= 0.01f)
_blackScreenPanel.SetActive(false);
}
[Header("=== 开关 ===")]
[SerializeField] private bool _enableOpeningFlow = true;
[Header("=== 音效 ===")]
[SerializeField] private AudioClip _basketballClip;
[SerializeField] private AudioClip _manCallClip;
private void DeactivateManInjuryChildren()
{
if (_manInjuryObject == null) return;
foreach (Transform child in _manInjuryObject.transform)
{
if (child.name.Contains("ping") || child.name.Contains("chanrao"))
child.gameObject.SetActive(false);
}
Debug.Log("[OpeningFlow] manInjury ping/chanrao 已全部隐藏");
}
}