在遊戲中,我們通常會遇到這樣的選擇界面,通常人物會顯示在UI層級之前,這時候我們就需要使用一點兒小技巧。
首先,我們需要在UI中添加RawImage,大小屬性自己調整,而後新建camera並照射物體,最終將camera與rawimage聯繫起來就行,代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
public RawImage showZombieRaw;
// Use this for initialization
void Start () {
if (gameObject.GetComponent<Camera>().targetTexture == null)
{
RenderTexture rt = new RenderTexture((int)showZombieRaw.GetComponent<RectTransform>().rect.size.x,
(int)showZombieRaw.GetComponent<RectTransform>().rect.size.y, 16, RenderTextureFormat.Default);
showZombieRaw.texture = rt;
gameObject.GetComponent<Camera>().targetTexture = rt;
}
//}
}
// Update is called once per frame
void Update () {
}
}
最終實現效果如下: