赞
踩
通过点击鼠标左键达到“开镜”效果,代码如下:
- using DG.Tweening;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class AdjustFOV : MonoBehaviour
- {
- //相机视野缩减等级
- private enum GandeFOV
- {
- one,two,three,four,Null
- }
-
- private Camera m_camera;
- private float initfov;
- private GandeFOV gf=GandeFOV .Null;
- private bool useFour ;
- void Start()
- {
- m_camera = GetComponent<Camera>();
- initfov = m_camera.fieldOfView;//记录相机初始视野值
- }
-
- void Update()
- {
- if (Input.GetKeyDown (KeyCode.Alpha1))
- {
- gf = GandeFOV.one;
- }
- if (Input.GetKeyDown (KeyCode.Alpha2 ))
- {
- gf = GandeFOV.two;
- }
- if (Input.GetKeyDown (KeyCode.Alpha3 ))
- {
- gf = GandeFOV.three;
- }
- if (Input.GetKeyDown (KeyCode.Alpha4 ))
- {
- gf = GandeFOV.four;
- }
- //按下鼠标左键缩减相机视野
- if (Input .GetMouseButtonDown(0))
- {
- switch (gf)
- {
- //直接变
- case GandeFOV.one:
- m_camera.fieldOfView = 40;
- break;
- //缓慢变
- case GandeFOV.two:
- StartCoroutine("ChangeFOV");
- break;
- //平滑缓慢变
- case GandeFOV.three:
- //方法原理:在某段时间(1s)内,局部变量(currentfov)持续向目标结果(40)变化,
- //目标变量(m_camera.fieldOfView)持续接收局部变量的值
- float currentfov = m_camera.fieldOfView;
- DOTween.To(() => currentfov, x => currentfov = x, 40, 1)
- .OnUpdate(() => m_camera.fieldOfView = currentfov);
- break;
- //平滑缓慢变
- case GandeFOV.four:
- useFour = true;
- break;
- default:
- break;
- }
- }
- //抬起鼠标左键恢复相机视野
- if (Input.GetMouseButtonUp(0))
- {
- if (gf==GandeFOV.one)
- {
- m_camera.fieldOfView = initfov;
- }
- if (gf==GandeFOV .two)
- {
- StopCoroutine("ChangeFOV");
- m_camera.fieldOfView = initfov;
- }
- if (gf==GandeFOV.three )
- {
- float currentfov = m_camera.fieldOfView;
- DOTween.To(() => currentfov, x => currentfov = x, initfov, 1)
- .OnUpdate(() => m_camera.fieldOfView = currentfov);
- }
- if (gf==GandeFOV.four)
- {
- useFour = false;
- }
-
- }
- if (gf == GandeFOV.four)
- {
- if (useFour)
- {
- //Mathf.Lera(起点值,终点值,比例)
- m_camera.fieldOfView = Mathf.Lerp(m_camera.fieldOfView, 40, 0.1f);
- if (Mathf.Abs(m_camera.fieldOfView - 40) < 0.1f)
- m_camera.fieldOfView = 40;
- }
- else
- {
- m_camera.fieldOfView = Mathf.Lerp(m_camera.fieldOfView, initfov, 0.1f);
- if (Mathf.Abs(m_camera.fieldOfView - initfov) < 0.1f)
- m_camera.fieldOfView = initfov;
- }
- }
-
- }
- //每等0.2秒缩减5米的相机视野,效果不流畅
- private IEnumerator ChangeFOV()
- {
- while (m_camera .fieldOfView >40)
- {
- m_camera.fieldOfView -= 5;
- yield return new WaitForSeconds(0.2f);
- }
- }
-
- }
数据缓慢变化的方法:
1、Mathf.Lera(起点值,终点值,比例)
2、 DOTween.To(() =>起点变量 , x => 起点变量 = x, 终点值, 1)
.OnUpdate(() => 需改变的变量 = 起点变量);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。