扣血抖动和FPS显示

一、扣血抖动

打斗的时候,当受到伤害时,为了使游戏更加真实,会使主角抖动;但这只是让玩家看起来的,其实只需要抖动窗口即可实现。
其原理是摄像机窗口,既屏幕视口。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShakeCamera : MonoBehaviour
{

    public float ShakeLevel = 3f;//振动幅度
    public float setShakeTime = 0.5f;//振动时间
    public float shakeFps = 45f;//振动的fps

    public bool isShakeCamera = false;
    public float fps;
    public float shakeTime = 0.0f;
    public float frameTime = 0.0f;
    public float shakeDelta = 0.005f;
    public Camera mainCamera;

    void OnEnable()
    {
        isShakeCamera = true;
        mainCamera = this.GetComponent<Camera>();
        shakeTime = setShakeTime;
        fps = shakeFps;
        frameTime = 0.03f;
        shakeDelta = 0.005f;
    }

    // Update is called once per frame
    void Update()
    {
        if (isShakeCamera)
        {
            if (shakeTime > 0)
            {
                shakeTime -= Time.deltaTime;
                if (shakeTime <= 0)
                {
                    enabled = false;
                }
                else
                {
                    frameTime += Time.deltaTime;
                    if (frameTime > 1.0 / fps)
                    {
                        frameTime = 0;
                        mainCamera.rect = new Rect(shakeDelta * (-1.0f + ShakeLevel * Random.value), shakeDelta * (-1.0f + ShakeLevel * Random.value), 1.0f, 1.0f);
                    }
                }
            }
        }
    }
    void OnDisable()
    {
        mainCamera.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
        isShakeCamera = false;
    }
}

二、FPS的显示

玩射击游戏的时候,或者玩其他游戏,会有一个显示帧率的,可能是真的,也有可能的固定的。自己的一些小游戏可以实现。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/*
 * 帧率
 * 
 */
public class FPS : MonoBehaviour {

    private float lastUpdateShowTime = 0;//上一次更新帧率的时间
    private float updateTime = 0.05f;//更新显示帧率的时间间隔
    private int frames = 0;//帧数
    private float frameDeltaTime = 0;//帧间间隔

    private float Fps;
    private Rect fps, deltaTime;
    private GUIStyle style = new GUIStyle();
	// Use this for initialization
	void Awake () {
        Application.targetFrameRate = 100;
	}
	void Start()
    {
        lastUpdateShowTime = Time.realtimeSinceStartup;
        fps = new Rect(0, 0, 100, 100);
        deltaTime=new Rect(0,30,100,100);
        style.fontSize = 30;
        style.normal.textColor = Color.red;
    }
	// Update is called once per frame
	void Update () {
        frames++;
        if (Time.realtimeSinceStartup-lastUpdateShowTime>=updateTime)
        {
            Fps = frames / (Time.realtimeSinceStartup - lastUpdateShowTime);
            frameDeltaTime = (Time.realtimeSinceStartup - lastUpdateShowTime) / frames;
            frames = 0;
            lastUpdateShowTime = Time.realtimeSinceStartup;
        }
	}
    void OnGUI()
    {
        GUI.Label(fps, "FPS:" + Fps, style);
        GUI.Label(deltaTime, "时间间隔:" + frameDeltaTime, style);
    }
}

你可能感兴趣的:(unity3d,unity3d)