Unity製作特寫鏡頭
阿新 • • 發佈:2022-04-08
類似這種效果
黑邊的大小可以自行調整
這裡為了方便直接用兩個Button綁定了方法,有需要自行呼叫方法
1.首先製作上下兩層黑邊
建立Canvas然後在canvas上新建空物體,命名為CinemaCloseUpShot
在上面新建指令碼CinemaCloseUP
public class CinemaCloseUP : MonoBehaviour { public float targetSizeInput; //上下黑條的寬度 public float showTime; //進行縮放所需的時間 private RectTransform topBar, bottomBar; //宣告上下兩個 RectTransform,該元件在UI中控制UI大小、位置等引數 private float changeSizeAmount; //上下黑條變化量 private bool isActive; //是否進行特寫 private float targetSize; // private void Awake() { //建立上方黑條,型別為圖片 GameObject gameObject = new GameObject("topBar", typeof(Image)); gameObject.transform.SetParent(transform, false); //將之前的空物體設為父物體 gameObject.GetComponent<Image>().color = Color.black;//顏色設為黑色 topBar = gameObject.GetComponent<RectTransform>(); //獲取黑條圖片的RectTransform //將上方圖片的錨點設為(0, 1)和(1, 1),其實就是父物體的左上角和右上角兩個錨點 topBar.anchorMax = new Vector2(1, 1); topBar.anchorMin = new Vector2(0, 1); topBar.sizeDelta = new Vector2(0, 0); //預設圖片大小為0 //建立下方黑條,型別為圖片 gameObject = new GameObject("bottomBar", typeof(Image)); gameObject.transform.SetParent(transform, false); gameObject.GetComponent<Image>().color = Color.black; bottomBar = gameObject.GetComponent<RectTransform>(); //將下方圖片的錨點設為(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方向持續增加/減小,其實就是圖片的寬 //根據下面兩個方法,顯示為正,隱藏為負 if (changeSizeAmount > 0) //點選顯示的情況 { if (sizeDelta.y >= targetSize) //如果sizeDelta.y達到最大寬度 { sizeDelta.y = targetSize; //設定sizeDelta的寬度 isActive = false; //停止變化 } } else //點選隱藏的情況 { if (sizeDelta.y <= targetSize) //如果sizeDelta.y達到最小寬度(0) { sizeDelta.y = targetSize; //設定sizeDelta的最小寬度(0) isActive = false; //停止變化 } } //因為上下黑條對稱,對上下黑條的寬度進行賦值 topBar.sizeDelta = sizeDelta; bottomBar.sizeDelta = sizeDelta; } } public void ShowBar() { targetSize = targetSizeInput; //點選顯示,對寬度賦值(在Inspector面板中設定) changeSizeAmount = (targetSize - topBar.sizeDelta.y) / showTime; //黑條的改變速率,這裡為正數 isActive = true; //開始變化 } public void Hide() { targetSize = 0f; //點選隱藏黑條寬度為0 changeSizeAmount = (targetSize - topBar.sizeDelta.y) / showTime; //黑條的改變速率,這裡為負數 isActive = true; //開始變化 } }
然後設定合適的引數
分別為黑邊的高度和黑邊變化的時間
2.攝像頭聚焦的效果
給攝像機搭載指令碼CameraPos
public class CameraPos : MonoBehaviour { public bool CamMove; //是否聚焦角色 public float distance; //距離角色的距離,可以用控制角色顯示的大小 public float focusSpeed = 5f; //聚焦的速度 private Vector3 defaultPos; //攝像頭預設位置 private Vector3 targetPos; //聚焦目標的位置 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) //設定聚焦的目標 { //注意這裡的Z座標不要大於0 targetPos = target.transform.position + new Vector3(0, 0, defaultPos.z + distance); isActive = true; } public void ResetPos() //還原攝像頭位置 { targetPos = defaultPos; isActive = true; } }
設定合適引數
注意這裡Distance計算後的Z座標不要大於0,可以在程式碼中新增限制
顯示效果: