欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用Unity制作特寫鏡頭的示例代碼

 更新時間:2022年04月08日 15:40:57   作者:AlphaIcarus  
這篇文章主要介紹了如何利用Unity制作特寫鏡頭效果,文中的示例代碼講解詳細,對我們學(xué)習(xí)有一定的參考價值,感興趣的可以了解一下

類似這種效果

黑邊的大小可以自行調(diào)整

這里為了方便直接用兩個Button綁定了方法,有需要自行調(diào)用方法

1.首先制作上下兩層黑邊

創(chuàng)建Canvas然后在canvas上新建空物體,命名為CinemaCloseUpShot

在上面新建腳本CinemaCloseUP

public class CinemaCloseUP : MonoBehaviour
{
    public float targetSizeInput;	//上下黑條的寬度
    public float showTime;		    //進行縮放所需的時間

    private RectTransform topBar, bottomBar;	//聲明上下兩個 RectTransform,該組件在UI中控制UI大小、位置等參數(shù)
    private float changeSizeAmount;	//上下黑條變化量
    private bool isActive;		    //是否進行特寫
    private float targetSize;		//
    private void Awake()
    {
        //創(chuàng)建上方黑條,類型為圖片
        GameObject gameObject = new GameObject("topBar", typeof(Image));
        gameObject.transform.SetParent(transform, false);	//將之前的空物體設(shè)為父物體
        gameObject.GetComponent<Image>().color = Color.black;//顏色設(shè)為黑色
        topBar = gameObject.GetComponent<RectTransform>();	//獲取黑條圖片的RectTransform
        
        //將上方圖片的錨點設(shè)為(0, 1)和(1, 1),其實就是父物體的左上角和右上角兩個錨點
        topBar.anchorMax = new Vector2(1, 1);				
        topBar.anchorMin = new Vector2(0, 1);
        topBar.sizeDelta = new Vector2(0, 0);	//默認圖片大小為0
        
		//創(chuàng)建下方黑條,類型為圖片
        gameObject = new GameObject("bottomBar", typeof(Image));
        gameObject.transform.SetParent(transform, false);
        gameObject.GetComponent<Image>().color = Color.black;
        bottomBar = gameObject.GetComponent<RectTransform>();
        
        //將下方圖片的錨點設(shè)為(0, 0)和(1, 0),其實就是父物體的左下角和右下角兩個錨點
        bottomBar.anchorMax = new Vector2(1, 0);
        bottomBar.anchorMin = new Vector2(0, 0);
        bottomBar.sizeDelta = new Vector2(0, 0);//默認圖片大小為0
    }
    
    private void Update()
    {
        if (isActive)
        {
            Vector2 sizeDelta = topBar.sizeDelta;	//聲明一個sizeDelta,并賦值為topBar.sizeDelta
            sizeDelta.y += changeSizeAmount * Time.deltaTime;//sizeDelta的y方向持續(xù)增加/減小,其實就是圖片的寬
            //根據(jù)下面兩個方法,顯示為正,隱藏為負
            if (changeSizeAmount > 0)	//點擊顯示的情況
            {
                if (sizeDelta.y >= targetSize)	//如果sizeDelta.y達到最大寬度
                {
                    sizeDelta.y = targetSize;	//設(shè)置sizeDelta的寬度
                    isActive = false;		    //停止變化
                }
            }
            else	//點擊隱藏的情況
            {
                if (sizeDelta.y <= targetSize)	//如果sizeDelta.y達到最小寬度(0)
                {
                    sizeDelta.y = targetSize;	//設(shè)置sizeDelta的最小寬度(0)
                    isActive = false;			//停止變化
                }
            }
            //因為上下黑條對稱,對上下黑條的寬度進行賦值
            topBar.sizeDelta = sizeDelta;
            bottomBar.sizeDelta = sizeDelta;
        }
    }

    public void ShowBar()
    {
        targetSize = targetSizeInput;	//點擊顯示,對寬度賦值(在Inspector面板中設(shè)定)
        changeSizeAmount = (targetSize - topBar.sizeDelta.y) / showTime;	//黑條的改變速率,這里為正數(shù)
        isActive = true;	//開始變化
    }

    public void Hide()
    {
        targetSize = 0f;	//點擊隱藏黑條寬度為0
        changeSizeAmount = (targetSize - topBar.sizeDelta.y) / showTime;	//黑條的改變速率,這里為負數(shù)
        isActive = true;	//開始變化
    }
}

然后設(shè)置合適的參數(shù)

分別為黑邊的高度和黑邊變化的時間

2.攝像頭聚焦的效果

給攝像機搭載腳本CameraPos

public class CameraPos : MonoBehaviour
{
    public bool CamMove;	//是否聚焦角色
    public float distance;	//距離角色的距離,可以用控制角色顯示的大小
    public float focusSpeed = 5f;	//聚焦的速度

    private Vector3 defaultPos;	//攝像頭默認位置
    private Vector3 targetPos;	//聚焦目標(biāo)的位置
    private bool isActive;		//開始移動
    private void Start()
    {
        defaultPos = transform.position;
    }
    private void Update()
    {
        if (isActive && CamMove)
        {
            transform.position = Vector3.Lerp(transform.position, targetPos, focusSpeed * Time.deltaTime);
        }
    }
    public void FocusOn(GameObject target)	//設(shè)置聚焦的目標(biāo)
    {
        //注意這里的Z坐標(biāo)不要大于0
        targetPos = target.transform.position + new Vector3(0, 0, defaultPos.z + distance);
        isActive = true;
    }
    public void ResetPos()	//還原攝像頭位置
    {
        targetPos = defaultPos;
        isActive = true;
    }
}

設(shè)置合適參數(shù)

注意這里Distance計算后的Z坐標(biāo)不要大于0,可以在代碼中添加限制

顯示效果:

3D場景中也是改變攝像頭位置來實現(xiàn)類似效果,實現(xiàn)的方法差不多

到此這篇關(guān)于利用Unity制作特寫鏡頭的示例代碼的文章就介紹到這了,更多相關(guān)Unity特寫鏡頭內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論