228 lines
6.6 KiB
C#
228 lines
6.6 KiB
C#
using DG.Tweening;
|
||
using System.Collections;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class CameraManager : MonoBehaviour
|
||
{
|
||
public static CameraManager instance;
|
||
public Camera Camera;
|
||
|
||
/// <summary>
|
||
/// 所有的种子
|
||
/// </summary>
|
||
public GameObject[] seed_Obj;
|
||
public Transform[] StepTrans;
|
||
/// <summary>
|
||
/// 入水种子
|
||
/// </summary>
|
||
public GameObject Seed_Obj;
|
||
|
||
/// <summary>
|
||
/// 水面
|
||
/// </summary>
|
||
public GameObject water_Obj;
|
||
|
||
/// <summary>
|
||
/// 农药
|
||
/// </summary>
|
||
public Transform pesticide_Trans;
|
||
|
||
public Transform pesticideRot_Trans;
|
||
|
||
|
||
public float minFOV = 30f; // 最小视野
|
||
public float maxFOV = 60f; // 最大视野
|
||
public float sensitivity = 5f; // 滚轮灵敏度
|
||
|
||
|
||
[Header("动画设置")]
|
||
public Animator SeedAnimator;
|
||
|
||
[Header("物体设置")]
|
||
[SerializeField] private GameObject[] objects; // 在Inspector中分配5个物体
|
||
[SerializeField] private float hideDuration = 1f; // 每个物体消失的持续时间
|
||
[SerializeField] private float delayBetweenObjects = 0.5f; // 物体之间的间隔时间
|
||
|
||
private Material[] originalMaterials;
|
||
private Color[] originalColors;
|
||
private bool isSequenceRunning = false;
|
||
|
||
private void Awake()
|
||
{
|
||
instance = this;
|
||
InitializeMaterials();
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (Input.GetMouseButtonDown(0))
|
||
{
|
||
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||
RaycastHit hit;
|
||
bool raycast = Physics.Raycast(ray, out hit);
|
||
if (raycast)
|
||
{
|
||
if (hit.collider.gameObject.tag.Equals("CompleteSeeds"))
|
||
{
|
||
Debug.Log("被点击物体的标签是" + hit.collider.gameObject.tag);
|
||
hit.collider.gameObject.SetActive(false);
|
||
|
||
bool allSeedsHidden = true;
|
||
for (int i = 0; i < seed_Obj.Length; i++)
|
||
{
|
||
if (seed_Obj[i].activeInHierarchy)
|
||
{
|
||
allSeedsHidden = false;
|
||
Debug.Log("还有种子没有点到");
|
||
break; // 只要有一个种子没隐藏,就退出循环
|
||
}
|
||
}
|
||
|
||
// 检查是否所有种子都被隐藏(放在循环外)
|
||
if (allSeedsHidden)
|
||
{
|
||
Debug.Log("所有种子都被隐藏");
|
||
Camera.fieldOfView = 60;
|
||
StartCoroutine(SeedIsFalse());
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
float scrollInput = Input.GetAxis("Mouse ScrollWheel");
|
||
|
||
if (scrollInput != 0)
|
||
{
|
||
float newFOV = Camera.fieldOfView - scrollInput * sensitivity;
|
||
newFOV = Mathf.Clamp(newFOV, minFOV, maxFOV);
|
||
Camera.fieldOfView = newFOV;
|
||
|
||
Debug.Log($"当前FOV: {newFOV}");
|
||
}
|
||
}
|
||
public IEnumerator SeedIsFalse()
|
||
{
|
||
Camera.transform.SetPositionAndRotation
|
||
(StepTrans[2].transform.position, StepTrans[1].transform.rotation);
|
||
yield return new WaitForSeconds(1f);
|
||
Seed_Obj.transform.DOMoveY(0, 1f);
|
||
yield return new WaitForSeconds(2.1f);
|
||
pesticide_Trans.transform.DOLocalRotate
|
||
(new Vector3(15,90,-90),1.5f);
|
||
yield return new WaitForSeconds(1.5f);
|
||
water_Obj.transform.DOMoveY(0.1f, 1.5f);
|
||
yield return new WaitForSeconds(2.5f);
|
||
UI_FadePanel.Instance.Init();
|
||
yield return new WaitForSeconds(9f);
|
||
UI_FadePanel.Instance.Next_Btn.gameObject.SetActive(true);
|
||
}
|
||
public void InitializeMaterials()
|
||
{
|
||
originalMaterials = new Material[objects.Length];
|
||
originalColors = new Color[objects.Length];
|
||
|
||
for (int i = 0; i < objects.Length; i++)
|
||
{
|
||
if (objects[i] != null && objects[i].TryGetComponent<Renderer>(out var renderer))
|
||
{
|
||
originalMaterials[i] = renderer.material;
|
||
originalColors[i] = renderer.material.color;
|
||
// 所有物体初始隐藏
|
||
objects[i].SetActive(false);
|
||
SetObjectAlpha(objects[i], 0f);
|
||
}
|
||
}
|
||
}
|
||
|
||
public void OnNextButtonClick()
|
||
{
|
||
if (!isSequenceRunning)
|
||
{
|
||
StartCoroutine(ExecuteObjectSequence());
|
||
}
|
||
}
|
||
|
||
IEnumerator ExecuteObjectSequence()
|
||
{
|
||
isSequenceRunning = true;
|
||
|
||
// 1. 播放种子动画
|
||
SeedAnimator.SetBool("种下", true);
|
||
yield return new WaitForSeconds(1.5f); // 等待动画播放
|
||
|
||
// 2. 激活并显示第一个物体
|
||
if (objects[0] != null)
|
||
{
|
||
objects[0].SetActive(true);
|
||
SetObjectAlpha(objects[0], 1f);
|
||
}
|
||
|
||
// 3. 依次处理前n-1个物体(最后一个保持显示)
|
||
for (int i = 0; i < objects.Length - 1; i++)
|
||
{
|
||
if (objects[i] != null && objects[i].activeSelf)
|
||
{
|
||
yield return new WaitForSeconds(delayBetweenObjects);
|
||
yield return FadeOutObject(objects[i]);
|
||
objects[i].SetActive(false);
|
||
|
||
// 激活并显示下一个物体
|
||
if (objects[i + 1] != null)
|
||
{
|
||
objects[i + 1].SetActive(true);
|
||
SetObjectAlpha(objects[i + 1], 1f);
|
||
}
|
||
}
|
||
}
|
||
|
||
isSequenceRunning = false;
|
||
}
|
||
|
||
IEnumerator FadeOutObject(GameObject obj)
|
||
{
|
||
if (!obj.TryGetComponent<Renderer>(out var renderer)) yield break;
|
||
|
||
Material material = renderer.material;
|
||
Color startColor = material.color;
|
||
float elapsedTime = 0f;
|
||
|
||
while (elapsedTime < hideDuration)
|
||
{
|
||
float progress = elapsedTime / hideDuration;
|
||
material.color = new Color(
|
||
startColor.r,
|
||
startColor.g,
|
||
startColor.b,
|
||
Mathf.Lerp(1f, 0f, progress)
|
||
);
|
||
elapsedTime += Time.deltaTime;
|
||
yield return null;
|
||
}
|
||
|
||
material.color = new Color(startColor.r, startColor.g, startColor.b, 0f);
|
||
}
|
||
|
||
void SetObjectAlpha(GameObject obj, float alpha)
|
||
{
|
||
if (obj != null && obj.TryGetComponent<Renderer>(out var renderer))
|
||
{
|
||
Color color = renderer.material.color;
|
||
renderer.material.color = new Color(color.r, color.g, color.b, alpha);
|
||
}
|
||
}
|
||
|
||
public void ResetAllObjects()
|
||
{
|
||
for (int i = 0; i < objects.Length; i++)
|
||
{
|
||
if (objects[i] != null)
|
||
{
|
||
// 重置所有物体为隐藏状态
|
||
objects[i].SetActive(false);
|
||
SetObjectAlpha(objects[i], 0f);
|
||
}
|
||
}
|
||
}
|
||
}
|