Chủ Nhật, 29 tháng 12, 2013

COOKBOOK - HIỂN THỊ BẢN ĐỒ THU NHỎ


  - Hiển thị bản đồ thu nhỏ -
Trong hầu hết các game, một góc nhìn rộng trong game rất có giá trị để định vị và bắt thông tin. Bản đồ thu nhỏ là một thứ tuyệt vời giành cho người chơi có thể mở rộng góc nhìn cả trong chế độ góc nhìn thứ nhất hoặc ba.



Chuẩn bị 
Maze.unitypackage

Cách để làm

B1. Nhấp phải vào vùng trống trong thẻ Project, chọn Import Package | Custom Package và mở file Maze.unitypackage vừa tải. Double click vào Demo để mở scene này lên.

B2. Nhấp chọn 3rd Person Controller ở thẻ Hierarchy, tại thẻ Inspector, nhấp vào ô giá trị phía sau Layer và chọn Add Layer, gõ vào User Layer 8 là NoMap.


  

B3. Nhấp chuột chọn lại 3rd Person Controller ở thẻ Hierarchy và điều chỉnh Layer thành NoMap.


B4. Tại thẻ Hierarchy, nhấp nút Create | Camera và đổi tên thành Map Camera.


B5. Nhấp chuột chọn Map Camera vừa tạo, tại thẻ Inspector điều chỉnh các thông số như hình sau:


B6. Tại thẻ Project, nhấp nút Create | C# Script và đặt tên là GenerateMap. Double click vào file vừa tạo và chèn đoạn code sau vào:

using UnityEngine;
using System.Collections;

public class GenerateMap : MonoBehaviour {
    public Transform target;
    public Texture2D marker;
    public float camHeight = 1.0f;
    public bool freezeRotation = true;
    public float camDistance = 2.0f;
    public enum ha {left, center, right};
    public enum va {top, middle, bottom};
    public ha horizontalAlignment = ha.left;
    public va verticalAlignment = va.top;
    public enum sd {pixels, screen_percentage};
    public sd dimensionsIn = sd.pixels;
    public int width = 50;
    public int heigth = 50;
    public float xOffset = 0f;
    public float yOffset = 0f;
   
    void Start(){
        Vector3 angles = transform.eulerAngles;
        angles.x = 90;
        angles.y = target.transform.eulerAngles.y;
        transform.eulerAngles = angles;
        Draw();
    }
    void Update(){
        transform.position = new Vector3(target.transform.position.x, target.transform.position.y + camHeight, target.transform.position.z);
        camera.orthographicSize = camDistance;       
        if (freezeRotation){
            Vector3 angles = transform.eulerAngles;
            angles.y = target.transform.eulerAngles.y;
            transform.eulerAngles = angles;
        }
    }
    void Draw(){
        int hsize = Mathf.RoundToInt(width * 0.01f * Screen.width);
        int vsize = Mathf.RoundToInt(heigth * 0.01f * Screen.height);
        int hloc = Mathf.RoundToInt(xOffset * 0.01f * Screen.width);
        int vloc = Mathf.RoundToInt((Screen.height - vsize) - (yOffset * 0.01f * Screen.height));  

        if(dimensionsIn == sd.screen_percentage){
            hsize = Mathf.RoundToInt(width * 0.01f * Screen.width);
            vsize = Mathf.RoundToInt(heigth * 0.01f * Screen.height);
        } else {
            hsize = width;
            vsize = heigth;
        }
       
        switch(horizontalAlignment){
        case ha.left:
            hloc = Mathf.RoundToInt(xOffset * 0.01f * Screen.width);
            break;
        case ha.right:
            hloc = Mathf.RoundToInt((Screen.width - hsize) - (xOffset * 0.01f * Screen.width));
            break;
        case ha.center:
            hloc = Mathf.RoundToInt(((Screen.width * 0.5f) - (hsize * 0.5f)) - (xOffset * 0.01f * Screen.height));
            break;
        }
        switch(verticalAlignment){
        case va.top:
            vloc = Mathf.RoundToInt((Screen.height - vsize) - (yOffset * 0.01f * Screen.height));
            break;
        case va.bottom:
            vloc =Mathf.RoundToInt(yOffset * 0.01f * Screen.height);
            break;
        case va.middle:
            vloc = Mathf.RoundToInt(((Screen.height * 0.5f) - (vsize * 0.5f)) - (yOffset * 0.01f * Screen.height));
            break;
        }
        camera.pixelRect = new Rect(hloc,vloc,hsize,vsize);
    }
    void OnGUI(){
        Vector3 markerPos = camera. camera.WorldToViewportPoint(target.position);
        int pointX =  Mathf.RoundToInt((camera.pixelRect.xMin + camera.pixelRect.xMax) * markerPos.x);
        int pointY =  Mathf.RoundToInt(Screen.height - (camera.pixelRect.yMin + camera.pixelRect.yMax) * markerPos.y);
        GUI.DrawTexture( new Rect(pointX-(marker.width * 0.5f),pointY-(marker.height * 0.5f),marker.width,marker.height), marker, ScaleMode.StretchToFill, true, 10.0f);
    }
}


B7. Kéo thả file C# vừa tạo vào Map Camera, nhấp chọn Map Camera và qua thẻ Inspector điều chỉnh các thông số như sau:


B8. Nhấn nút Play để kiểm tra thành quả.


Còn nữa...

- Sử dụng Render Texture (Pro only)
Nếu bạn đang sử dụng phiên bản pro, bạn có thể dùng Render Texture và hiển thị bản đồ của bạn bằng cách dùng GUI. DrawTexture hoặc Graphics.DrawTexture. Xem thêm tại đây:
http://docs.unity3d.com/Documentation/Components/class-RenderTexture.html

- Sử dụng bản đồ thu nhỏ với mục đích khác
Bạn có thể dễ dàng sử dụng chức năng này để tạo góc nhìn hướng từ trên xuống mặt đất làm bản đổ cho các thể loại game đua xe. Chỉ cần định vị và hướng camera vào nhân vật và gắn thêm biểu tượng đánh dấu.


Bài viết nên xem
Cookbook - HƯỚNG DẪN TẠO HIỆU ỨNG TRANH TRONG TRANH

Không có nhận xét nào:

Đăng nhận xét