1、添加Img
create->ui->img
把圖片拖進去
2、和分數一樣、調整位置
3、修改角色腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Fly : MonoBehaviour
{//獲取小鳥(剛體)private Rigidbody2D bird;//速度public float speed;//跳躍public float jump;//是否存活public static bool life = true;//獲取動畫器private Animator animator;//結束圖片private GameObject gameOver;//結束跳轉時間private float time;// Start is called before the first frame updatevoid Start(){bird = GetComponent<Rigidbody2D>();animator = GetComponent<Animator>();gameOver = GameObject.Find("Canvas/Image");}// Update is called once per framevoid Update(){//存活的時候才能運動if (life){bird.velocity = new Vector2(speed, bird.velocity.y);//鼠標點擊給目標一個縱向速度if (Input.GetMouseButtonDown(0)){bird.velocity = new Vector2(bird.velocity.x, jump);}}else {time += Time.deltaTime;if (time>=3) {FadeInOut.SwitchScene("start");}}//當觸碰到死亡的時候出現gameOver.SetActive(!life);}//如果碰撞器撞到了某個物體private void OnCollisionEnter2D(Collision2D collision){if (life==true) {Bling.blinking();}life = false;animator.SetBool("life", false);}
}
4、開始按鈕初始化參數
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class StartBtnLis : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}//監聽鼠標按下private void OnMouseDown(){Debug.Log("測試按下");//對象按比例縮小transform.localScale = transform.localScale * 0.8f;}//監聽鼠標松開private void OnMouseUp(){//對象按比例擴大transform.localScale = transform.localScale / 0.8f;Fly.life = true;Score.score = 0;FadeInOut.SwitchScene("game");}
}
修改淡入淡出bug,在淡入前點擊開始按鈕卡住問題
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class FadeInOut : MonoBehaviour
{//腳本傳進來的圖片public Texture img;//透明度public static float alpha = 0;//淡出public static bool fadeOut = false;//淡入public static bool fadeIn = false;//前端傳過來的速度public float speed;//場景private static string scene;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}//渲染頁面調用的得是這個方法private void OnGUI(){// Time.deltaTime 上一幀與當前幀間隔幀數if (fadeOut){alpha += speed * Time.deltaTime;if (alpha>1) {fadeOut = false;fadeIn = true;//場景切換SceneManager.LoadScene(scene);}}if (fadeIn) {alpha -= speed * Time.deltaTime;if (alpha<0) {fadeIn = false;}}//調整透明度GUI.color = new Color(GUI.color.r,GUI.color.g,GUI.color.b,alpha);//把場景繪制一張黑色圖片GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),img);}public static void SwitchScene(string newScene){if (fadeIn) fadeIn = false;fadeOut = true;scene = newScene;}
}