
- Hiệu ứng Tranh-Trong-Tranh - |
Chuẩn bị
basicLevel
Cách để làm
B1. Tạo một project mới bằng cách ấn Ctrl + N và điều chỉnh đường dẫn phù hợp. Nhấp chuột phải vào vùng trống trong thẻ Project và chọn Import Package | Custom Package và mở file basicLevel đã tải ở phần trên.

B2. Trong thẻ Project, vào thư mục 02_01_02 và double click vào file basicScene.

B3. Tại thẻ Hierarchy, chọn Creat | Camera để tạo thêm một camera mới trong game.

B4. Nhấp chọn Camera vừa tạo, tại thẻ Inspector điều chỉnh tọa độ và nhập giá trị Depth = 1 như hình sau:

B5. Nhấp chuột phải vào vùng trống trong thẻ Project và chọn Creat | C# Script và đặt tên là PictureInPicture.
B6. Double click vào file C# vừa tạo và nhập đoạn code sau vào:
using UnityEngine;
public class PictureInPicture: MonoBehaviour
{
public enum HorizontalAlignment
{
left,
center,
right
};
public enum VerticalAlignment
{
top,
middle,
bottom
};
public HorizontalAlignment horizontalAlignment = HorizontalAlignment.left;
public VerticalAlignment verticalAlignment = VerticalAlignment.top;
public enum ScreenDimensions
{
pixels,
screen_percentage
};
public ScreenDimensions dimensionsIn = ScreenDimensions.pixels;
public int width = 50;
public int height = 50;
public float xOffset = 0f;
public float yOffset = 0f;
public bool update = true;
private int hsize, vsize, hloc, vloc;
void Start ()
{
AdjustCamera ();
}
void Update ()
{
if (update)
AdjustCamera ();
}
void AdjustCamera ()
{
if (dimensionsIn == ScreenDimensions.screen_percentage) {
hsize = Mathf.RoundToInt (width * 0.01f * Screen.width);
vsize = Mathf.RoundToInt (height * 0.01f * Screen.height);
} else {
hsize = width;
vsize = height;
}
if (horizontalAlignment == HorizontalAlignment.left) {
hloc = Mathf.RoundToInt (xOffset * 0.01f * Screen.width);
} else if (horizontalAlignment == HorizontalAlignment.right) {
hloc = Mathf.RoundToInt ((Screen.width - hsize) - (xOffset * 0.01f * Screen.width));
} else {
hloc = Mathf.RoundToInt (((Screen.width * 0.5f) - (hsize * 0.5f)) - (xOffset * 0.01f * Screen.height));
}
if (verticalAlignment == VerticalAlignment.top) {
vloc = Mathf.RoundToInt ((Screen.height - vsize) - (yOffset * 0.01f * Screen.height));
} else if (verticalAlignment == VerticalAlignment.bottom) {
vloc = Mathf.RoundToInt (yOffset * 0.01f * Screen.height);
} else {
vloc = Mathf.RoundToInt (((Screen.height * 0.5f) - (vsize * 0.5f)) - (yOffset * 0.01f * Screen.height));
}
camera.pixelRect = new Rect (hloc, vloc, hsize, vsize);
}
}
B7. Save file C# vừa tạo bằng cách ấn Ctrl + S và gắn nó vào Camera bạn mới tạo khi nãy bằng cách kéo thả file C# trong thẻ Project vào Camera trong thẻ Hierarchy.

B8. Nhấp chọn Camera trong thẻ Hierarchy, bỏ chọn dấu check ở mục Audio Listener trong thẻ Inspector và thiết lập các điều chỉnh như hình sau:

B9. Ấn nút Play và xem thành quả.

hi, ở B7, khi kéo vào file Camera ở Hierarchy thì nó báo lỗi " Pls fix compile error..." Xin tư vấn giúp
ReplyDelete