Unity中製做遊戲的快照遊戲支持玩家拍快照

Unity中製做遊戲的快照遊戲支持玩家拍快照

有些遊戲支持玩家「拍快照」,也就是將遊戲的精彩瞬間以圖片的形式記錄下來的功能。這個功能比較有趣,並且之後的用途也會很廣,爲此本節打算介紹:截取矩形區域內遊戲視圖,並將其顯示在視圖其它區域的方法。具體的操做步驟以下本文選自Unity遊戲開發技巧集錦框架

1)在Project視圖裏,建立一個C#腳本文件,並命名爲ScreenTexture。在此腳本中編寫以下的代碼:函數

  • 01     using UnityEngine;spa

  • 02     using System.Collections;orm

  • 03    遊戲

  • 04     public class ScreenTexture : MonoBehaviour圖片

  • 05     {遊戲開發

  • 06              //公有成員開發

  • 07              public int photoWidth = 50;                                    //矩形的寬度it

  • 08              public int photoHeight = 50;                                  //矩形的高度io

  • 09              public int thumbProportion = 25;                          //截圖的顯示比例

  • 10              public Color borderColor = Color.white;             //矩形框架的顏色

  • 11              public int borderWidth = 2;                                              //矩形框的寬度

  • 12              //私有成員

  • 13              private Texture2D texture;

  • 14              private Texture2D border;

  • 15              private int screenWidth;

  • 16              private int screenHeight;

  • 17              private int frameWidth;

  • 18              private int frameHeight;

  • 19              private bool shoot = false;

  • 20              // 腳本初始化時,調用此函數

  • 21              void Start ()

  • 22              {

  • 23                       screenWidth = Screen.width;

  • 24                       screenHeight = Screen.height;

  • 25                       frameWidth = Mathf.RoundToInt(screenWidth * photoWidth * 0.01f);

  • 26                       frameHeight = Mathf.RoundToInt(screenHeight * photoHeight * 0.01f);

  • 27                       texture = new Texture2D (frameWidth,frameHeight,TextureFormat.RGB24,false);

  • 28                       border = new Texture2D (1,1,TextureFormat.ARGB32, false);

  • 29                       border.SetPixel(0,0,borderColor);

  • 30                       border.Apply();

  • 31              }

  • 32              // 運行遊戲時,每幀都調用此函數

  • 33              void Update ()

  • 34              {

  • 35                        //鼠標左鍵按下的時候

  • 36                       if (Input.GetKeyUp(KeyCode.Mouse0))

  • 37                                 StartCoroutine(CaptureScreen());

  • 38              }

  • 39              //Game視圖上,繪製紋理

  • 40              void OnGUI ()

  • 41              {

  • 42                        //繪製矩形框的四個邊

  • 43                       GUI.DrawTexture(

  • 44                                                   new Rect(

  • 45                                                             (screenWidth*0.5f)-(frameWidth*0.5f) - borderWidth*2,

  • 46                                                             ((screenHeight*0.5f)-(frameHeight*0.5f)) - borderWidth,

  • 47                                                             frameWidth + borderWidth*2,

  • 48                                                             borderWidth),

  • 49                                                   border,ScaleMode.StretchToFill);

  • 50                       GUI.DrawTexture(

  • 51                                                   new Rect(

  • 52                                                             (screenWidth*0.5f)-(frameWidth*0.5f) - borderWidth*2,

  • 53                                                             (screenHeight*0.5f)+(frameHeight*0.5f),

  • 54                                                             frameWidth + borderWidth*2,

  • 55                                                             borderWidth),

  • 56                                                   border,ScaleMode.StretchToFill);

  • 57                       GUI.DrawTexture(

  • 58                                                   new Rect(

  • 59                                                             (screenWidth*0.5f)-(frameWidth*0.5f)- borderWidth*2,

  • 60                                                             (screenHeight*0.5f)-(frameHeight*0.5f),

  • 61                                                             borderWidth,

  • 62                                                             frameHeight),

  • 63                                                   border,ScaleMode.StretchToFill);

  • 64                       GUI.DrawTexture(

  • 65                                                   new Rect(

  • 66                                                             (screenWidth*0.5f)+(frameWidth*0.5f),

  • 67                                                             (screenHeight*0.5f)-(frameHeight*0.5f),

  • 68                                                             borderWidth,

  • 69                                                             frameHeight),

  • 70                                                   border,ScaleMode.StretchToFill);

  • 71                        //繪製矩形框中截取到的Game視圖

  • 72                       if(shoot)

  • 73                       {

  • 74                                 GUI.DrawTexture(

  • 75                                                   new Rect (

  • 76                                                             10,

  • 77                                                             10,

  • 78                                                             frameWidth*thumbProportion*0.01f,

  • 79                                                             frameHeight*thumbProportion* 0.01f),

  • 80                                                   texture,ScaleMode.StretchToFill);

  • 81                       }

  • 82              }

  • 83              //截取矩形框裏的Game視圖

  • 84              IEnumerator CaptureScreen ()

  • 85              {

  • 86                       yield return new WaitForEndOfFrame();

  • 87                       texture.ReadPixels(

  • 88                                 new Rect(

  • 89                                          (screenWidth*0.5f)-(frameWidth*0.5f),

  • 90                                (screenHeight*0.5f)-(frameHeight*0.5f),

  • 91                                frameWidth,

  • 92                                          frameHeight),

  • 93                                 0,0);

  • 94                       texture.Apply();

  • 95                       shoot = true;

  • 96              }

  • 97     }

腳本代碼中,40行的OnGUI()函數,使用GUI.DrawTexture()繪製了矩形框,以及矩形框中截取到的遊戲視圖;84行的CaptureScreen()函數,使用texture.ReadPixels()讀取矩形框中的全部像素點,而後存儲到texture中。觸發「拍照」功能的操做是,在Game視圖中的任意位置,單擊鼠標左鍵,即36行代碼實現的功能。

2)將腳本ScreenTexture賦予Main Camera,而後選中Main Camera,在Inspector視圖裏能夠設置腳本組件的一些屬性,如圖2-19所示本文選自Unity遊戲開發技巧集錦

2-19  Main CameraScreenTexture腳本組件的各屬性       2-20  當前的Scene視圖

3)爲遊戲的場景添加一些幾何體,並調整它們各自的位置,如圖2-20所示。

4)運行遊戲,當在Game視圖中的任意位置,單擊鼠標左鍵的時候,可在視圖的左上角看到矩形框中截取到的遊戲視圖,如圖2-21所示本文選自Unity遊戲開發技巧集錦

2-21  運行遊戲,查看截圖的效果

相關文章
相關標籤/搜索