游戲小地圖方案

Unity小地圖的實(shí)現(xiàn)

游戲,特別是RPG類型,基本都需要添加小地圖功能來輔助玩家。

實(shí)現(xiàn)小地圖需解決的問題:

  1. 小地圖(貼圖)從哪里來?
  2. 如何把場景中單位的坐標(biāo)轉(zhuǎn)換到小地圖上
  3. 小地圖視角怎么跟玩家視角一致

思路:

  1. 讓美術(shù)垂直90°方向以正交視角俯視整個(gè)場景,然后截取場景輸出成圖片,截取的大小讓美術(shù)設(shè)置,一般不超過2048*2048
  2. 保存截圖時(shí)的矩陣
  3. 通過第2點(diǎn)保存的矩陣,在游戲運(yùn)行時(shí)把場景中單位的世界坐標(biāo)轉(zhuǎn)換到小地圖下的局部坐標(biāo)

實(shí)現(xiàn):

  1. 根據(jù)設(shè)置(這里寫死了800*600),設(shè)定截圖窗口的大小,同時(shí)Camera視角校準(zhǔn)為玩家視角,我這里是直接使用Scene窗口
if (GUILayout.Button("調(diào)整截圖窗口大小"))
{   
    var windowSize = Vector2(800,600);
    var lastSceneView = SceneView.lastActiveSceneView;

    // 場景的Main Camera
    var mainCamera = UnityEngine.Camera.main;
    // SmoothFollow是鏡頭跟蹤的組件,這里知道SmoothFollow里有個(gè)rotationAngle字段是記錄玩家視角即可
    XS.SmoothFollow smoothFollow = null;

    if (mainCamera)
    {
        smoothFollow = mainCamera.GetComponent<XS.SmoothFollow>();
    }
    if (smoothFollow == null)
    {
        return;
    }

    lastSceneView.position = new Rect(Vector3.zero, windowSize);

    Quaternion currentRotation = Quaternion.Euler(90, smoothFollow.rotationAngle, 0);
    lastSceneView.LookAt(Vector3.zero, currentRotation, lastSceneView.size, true);
    lastSceneView.isRotationLocked = true;
}
  1. 截圖時(shí),以Scene的大小為圖片寬高,并預(yù)先算出此時(shí)的world2Screen矩陣和screen2World矩陣,用于后面的邏輯運(yùn)算
if (GUILayout.Button("截取小地圖"))
{
    var lastSceneView = SceneView.lastActiveSceneView;
    var sceneCamera = lastSceneView.camera;
    if (!sceneCamera.orthographic)
    {
        return;
    }

    var width = sceneCamera.pixelWidth;
    var height = sceneCamera.pixelHeight;
    var cameraPos = sceneCamera.transform.position;
    var camreaSize = sceneCamera.orthographicSize;
    
    Matrix4x4 world2Screen = sceneCamera.projectionMatrix * sceneCamera.worldToCameraMatrix;
    Matrix4x4 screen2World = world2Screen.inverse;

    RenderTexture sceneCameraRT = RenderTexture.GetTemporary((int)width, (int)height, 32, RenderTextureFormat.ARGBFloat);
    sceneCamera.targetTexture = sceneCameraRT;
    sceneCamera.Render();
    sceneCamera.targetTexture = null;

    // targetData是用于保存數(shù)據(jù)組件,掛載在場景的一個(gè)空Object上
    string sceneName = targetData.gameObject.scene.name;

    var path = EditorUtility.SaveFilePanel("Save SmallMap", "", sceneName, "png");
    if (!string.IsNullOrEmpty(path))
    {
        if (SaveRenderTextureToPNG(sceneCameraRT, path))
        {
            targetData.smallMapCameraCenter = cameraPos;
            targetData.smallMapCameraSize = camreaSize;

            targetData.smallMapWorldToScreenMatrix = world2Screen;
            targetData.smallMapScreenToWorldMatrix = screen2World;
            targetData.smallMapCutSize = new Vector2(width, height);

            UnityEditor.EditorUtility.SetDirty(targetData);
            UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(targetData.gameObject.scene);
        }
        else
        {
            Debug.LogError("Save RenderTexture to file error");
        }
    }
    RenderTexture.ReleaseTemporary(sceneCameraRT);
}
  1. 根據(jù)第2點(diǎn)保存的兩個(gè)矩陣和圖片大小,通過以下接口(Lua實(shí)現(xiàn))即可完成坐標(biāo)轉(zhuǎn)換,里面涉及的數(shù)學(xué)知識請自行查閱。
-- 世界坐標(biāo)轉(zhuǎn)圖片的局部坐標(biāo)
function manualWorldToScreenPoint(world2Screen, x, z, pixelWidth, pixelHeight)
    local temp = world2Screen * UnityEngine.Vector4(x, 10, z, 1.0)
    if temp.w == 0 then
        return UnityEngine.Vector3.zero
    else
        temp.x = (temp.x/temp.w + 1)*0.5 * pixelWidth
        temp.y = (temp.y/temp.w + 1)*0.5 * pixelHeight
        return temp.x, temp.y
    end
end

-- 圖片的局部坐標(biāo)轉(zhuǎn)世界坐標(biāo)
function manualScreenPointToWorld(screen2World, x, y, pixelWidth, pixelHeight)
    local in_x = 2.0 * (x / pixelWidth) - 1.0
    local in_y = 2.0 * (y / pixelHeight) - 1.0
    local in_z = 10
    local in_w = 1.0
    local pos = screen2World * UnityEngine.Vector4(in_x,in_y,in_z,in_w)
    if pos.w == 0 then
        return UnityEngine.Vector3.zero
    else
        pos.w = 1.0 / pos.w
        pos.x = pos.x*pos.w
        pos.y = pos.y*pos.w
        pos.z = pos.z*pos.w
        return pos.x, pos.z
    end
end

額外:

小地圖一般需要添加小地圖尋路功能,這里我使用的方案是給小地圖添加PointerClick事件,根據(jù)點(diǎn)擊的坐標(biāo)點(diǎn)轉(zhuǎn)換成世界坐標(biāo)點(diǎn),然后從在一點(diǎn)(當(dāng)然是上移到場景上空)通過射線往下檢測,得到與路面(路面需要設(shè)置特定Layer)的交點(diǎn),這個(gè)交點(diǎn)就是目標(biāo)點(diǎn),接下來就是UnityEngine.AI.NavMeshAgent的事情了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容