Unity 小地图( 根据比例 绘制 简易)

确定地图左下角和右上角 计算人物在地图中的一个比例 在小地图上 绘制出来

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 计算人物在地图上的位置
/// </summary>
public class TeshMap : MonoBehaviour
{
    /// <summary>
    ///  定位场景模型的2个点
    /// </summary>
    public Transform mapPointA;
    public Transform mapPointB;
   // public Transform mapPointC;


    /// <summary>
    ///  角色
    /// </summary>
    public Transform player;
  //  public Transform sceneRoot;
  /// <summary>
  /// 圆形地图
  /// </summary>
    public TestMapShow testMapShow;

  

    Vector3 oldPos;

    /// <summary>
    /// 场景尺寸 比例
    /// </summary>
    float disZ;
    float disX;

    /// <summary>
    ///   场景所有水晶点
    /// </summary>
    public List<Transform> listPoint;

    List<Vector3> listPointPos;

    private void Start()
    {
    
        Debug.Log(mapPointA.eulerAngles + " mapPointA " + mapPointA.localEulerAngles);
       

        disX = Mathf.Abs(mapPointB.localPosition.x - mapPointA.localPosition.x);
        disZ = Mathf.Abs(mapPointB.localPosition.z - mapPointA.localPosition.z);

        MapPoint();


    }

    private void LateUpdate()
    {
        if (oldPos != player.localPosition)
        {
            oldPos = player.localPosition;
            MapPosRot();
        }

    }
    /// <summary>
    /// 计算位置 ( 坐标和旋转)
    /// </summary>
    private void MapPosRot()
    {
       

        float x = Mathf.Abs(oldPos.x - mapPointA.localPosition.x)/disX;
        float z = (Mathf.Abs(oldPos.z - mapPointA.localPosition.z))/disZ;


        //Debug.Log("MapPosX:  " + disX + " " + x);
        //Debug.Log("MapPosZ:  " + disZ + " " + z);
        // testMapShow.ShowPlayer(x, z,mapPointA.eulerAngles.y - mapPointA.localEulerAngles.y);
       // testMapShow.ShowPlayer(z, x, player.transform.localEulerAngles.y+179);
        testMapShow.MapRot(z, x, player.transform.localEulerAngles.y-80);
        // return pos;
    }
    /// <summary>
    ///  计算场景水晶点位置
    /// </summary>
    private void MapPoint()
    {
        if (listPoint.Count <= 0)
            return;

        listPointPos = new List<Vector3>();
        for (int i = 0; i < listPoint.Count; i++)
        {
            float x = Mathf.Abs(listPoint[i].localPosition.x - mapPointA.localPosition.x) / disX;
            float z = Mathf.Abs(listPoint[i].localPosition.z - mapPointA.localPosition.z) / disZ;

            Debug.Log(Mathf.Abs(listPoint[i].localPosition.z - mapPointA.localPosition.z) + "   "+ disX);

            listPointPos.Add(new Vector3(z, x, 0));
        }

        testMapShow.ShowPoint(listPointPos);
        testMapShow.LucencyMap();

    }
   

}

  

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 显示小地图信息
/// </summary>
public class TestMapShow : MonoBehaviour
{
    public RectTransform canvas;

    public RectTransform mapPlayer;

    public RectTransform mapPlayer2;

    public Transform mapParent;

    public Image point;

    public Color lucency;

    Vector2 startMapPos;

    private void Start()
    {
        startMapPos = canvas.anchoredPosition;
    }

    /// <summary>
    ///  小地图上所有Image
    ///  
    /// </summary>
    public List<Image> listAllPoint;
    /// <summary>
    ///  显示角色在小地图的位置 ( Position   EulerAngles)
    /// </summary>
    public void ShowPlayer( float x , float y , float rot)
    {
        Vector2 pos = new Vector2(canvas.rect.width * x > 5 ? canvas.rect.width * x : 5, canvas.rect.height * y > 5 ? canvas.rect.height * y : 5);
        mapPlayer.anchoredPosition = pos;
        mapPlayer.localRotation = Quaternion.Euler(0, 0, -rot);


    }

    /// <summary>
    /// 显示所有水晶点 (所有可触发交互的地方)
    /// </summary>
    /// <param name="list_Point"></param>
    public void ShowPoint(List<Vector3> list_Point)
    {
        listAllPoint = new List<Image>();
        listAllPoint.Add(mapPlayer.GetComponent<Image>());
        listAllPoint.Add(canvas.GetComponent<Image>());

        for (int i = 0; i < list_Point.Count; i++)
        {
            Debug.Log(list_Point[i]);
            Image newPoint = Instantiate(point, canvas);
            newPoint.GetComponent<RectTransform>().anchoredPosition = 
                new Vector2(canvas.rect.width * list_Point[i].x, canvas.rect.height * list_Point[i].y);
           // Debug.Log(" Point   " + canvas.rect.width * list_Point[i].x+"   "+canvas.rect.height * list_Point[i].y);
            newPoint.gameObject.SetActive(true);

            listAllPoint.Add(newPoint.GetComponent<Image>());
        }
        mapPlayer.transform.SetAsLastSibling();

        


    }
    /// <summary>
    ///  显示和隐藏地图
    /// </summary>
    public void ShowMap( bool state)
    {
        for (int i = 0; i < listAllPoint.Count; i++)
        {
            listAllPoint[i].DOColor(new Color(listAllPoint[i].color.r, listAllPoint[i].color.g, listAllPoint[i].color.b, state?0: lucency.a), 1f);
        }
    }
    /// <summary>
    /// 设置地图透明度
    /// </summary>
    public void LucencyMap()
    {
        for (int i = 0; i < listAllPoint.Count; i++)
        {
            listAllPoint[i].color = new Color(listAllPoint[i].color.r, listAllPoint[i].color.g, listAllPoint[i].color.b,lucency.a);
        }
    }

    /// <summary>
    ///  地图旋转 人不动 (为了头发)
    /// </summary>
    public void MapRot( float x, float y , float rot)
    {
        Vector2 pos = new Vector2(canvas.rect.width * x > 5 ? canvas.rect.width * x : 5, canvas.rect.height * y > 5 ? canvas.rect.height * y : 5);

        

        mapPlayer2.anchoredPosition = pos;

        //canvas.anchoredPosition = startMapPos + (mapPlayer.anchoredPosition - mapPlayer2.anchoredPosition);
        //pos -= startMapPos;
        // canvas.anchoredPosition = mapPlayer.anchoredPosition - pos;

        mapParent.localRotation = Quaternion.Euler(0, 0, rot);
        canvas.localRotation = Quaternion.Euler(0, 0, rot);

        mapPlayer2.transform.parent = mapPlayer.transform.parent;
        canvas.transform.parent = mapPlayer2.transform;
        mapPlayer2.anchoredPosition = mapPlayer.anchoredPosition;
        canvas.transform.parent = mapPlayer.transform.parent;
        mapPlayer2.transform.parent = canvas.transform;
        mapPlayer.transform.SetAsLastSibling();



    }



}

  

posted @ 2021-03-26 17:38  D个人笔记  阅读(480)  评论(0编辑  收藏  举报