Render Texture的使用(截取rendertexture的一幀到Texture2D)


游戲里人物角色太多,每個角色都要有張頭像或全身照等,這樣就必須截取大量的圖片,花費大量的時間,有時截取的不滿意還得重新截,即浪費時間又浪費精力.所以就想了個投機取巧的方法.那就是用unity搭建一個照相館.

首先用CreateEmpty創建一個空的GameObject,命名為GetPicsObject,將他的Transform.position置為0;

接着創建一個Camera,命名為ShowCamera,把這個Camera放到上面GetPicsObject下,作為GetPicsObject的子物體.

接着用CreateEmpty創建另一個空的GameObject,命名為charactorPos,用來確定創建角色所在的位置,也放到GetPicsObject下,作為GetPicsObject的子物體.

調整Camera的位置,確保相機視圖為你想要截取照,層次如下圖所示

.

接着創建C#腳本,命名為GetPicTest

 

using UnityEngine; using System.Collections; using System.Collections.Generic; public class GetPicTest : MonoBehaviour { /// <summary>
    /// 創建角色所在位置 /// </summary>
    public Transform playerPos; /// <summary>
    /// 聲明一個全身照的RenderTexture /// </summary>
    public RenderTexture fullBodyRender; /// <summary>
    /// 將fullBodyRender的特定一幀動畫保存到fullBodyTex /// </summary>
    public Texture2D fullBodyTex; /// <summary>
    /// 全身像相機 /// </summary>
    public Camera showCamera; /// <summary>
    /// 保存角色預置 /// </summary>
    public List<GameObject> charactorsPrefabs = new List<GameObject>(); public int _width = 120; public int _height = 120; public GameObject obj; // Use this for initialization
    void Start() { fullBodyRender = new RenderTexture(_width, _height, 32); //fullBodyRender = RenderTexture.GetTemporary(_width, _height, 0, RenderTextureFormat.ARGBFloat);
           fullBodyRender.anisoLevel = 0; fullBodyRender.filterMode = FilterMode.Point; showCamera.targetTexture = fullBodyRender; showCamera.pixelRect = new Rect(0, 0, _width, _height); } void OnGUI() { GUI.DrawTexture(new Rect(Screen.width - _width, Screen.height - _height, _width, _height), fullBodyRender, ScaleMode.ScaleToFit);//全身
                int j = -1; for (int i = 0; i < charactorsPrefabs.Count; i++) { if (i % 10 == 0) { j++; } if (GUI.Button(new Rect(j*40,i%10*30,40,30),"" + charactorsPrefabs[i].name)) { if (obj) { Destroy(obj); } obj = GameObject.Instantiate(charactorsPrefabs[i]) as GameObject; obj.transform.parent = playerPos; obj.transform.localPosition = Vector3.zero; obj.animation.Play("idle"); obj.animation.wrapMode = WrapMode.Loop; CharacterController controller = obj.GetComponent<CharacterController>(); float h = controller.height; float scaleY = obj.transform.localScale.y; float multiNum = 1.5f / h / scaleY; obj.transform.localScale = new Vector3(obj.transform.localScale.x * multiNum, obj.transform.localScale.y * multiNum, obj.transform.localScale.z * multiNum); } } GUI.Box(new Rect(Screen.width - _width, 0, _width, _height),""); if (GUI.Button(new Rect(Screen.width / 2 - 70, 0, 140, 30), "GetPic")) { fullBodyTex = getTexture2d(fullBodyRender); } if (fullBodyTex) { GUI.DrawTexture(new Rect(Screen.width - _width, 0, _width, _height), fullBodyTex, ScaleMode.ScaleToFit); } } public Texture2D getTexture2d(RenderTexture renderT) { if (renderT == null) return null;
int width = renderT.width; int height = renderT.height; Texture2D tex2d = new Texture2D(width, height, TextureFormat.ARGB32, false); RenderTexture.active = renderT; tex2d.ReadPixels(new Rect(0, 0, width, height), 0, 0); tex2d.Apply(); //byte[] b = tex2d.EncodeToPNG(); //Destroy(tex2d); //File.WriteAllBytes(Application.dataPath + "1.jpg", b); return tex2d; } }

 

將GetPicTest賦給到GetPicsObject,把charactor賦給Playerpos,把ShowCamera賦給Show Camera,如下圖。

左邊一排按鈕選擇角色,中心上方GetPic按鈕截取圖標顯示在右上角.右下角為RenderTexture,如下圖。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM