unity小地图制作代码....

using UnityEngine;
using UnityEngine.EventSystems;

public class UIMiniMap : MonoBehaviour, IPointerDownHandler
{
    /// <summary>
    /// 小地图上的A点
    /// </summary>
    public RectTransform a;
    /// <summary>
    /// 小地图上的B点
    /// </summary>
    public RectTransform b;
    /// <summary>
    /// 控制的东西,这里使用cube代替
    /// </summary>
    public GameObject players;
    /// <summary>
    /// 3D物体0点,设置初始点
    /// </summary>
    public GameObject zerpos;
    /// <summary>
    /// 摄像机
    /// </summary>
    private Camera UICamera;
    /// <summary>
    /// 地图大小
    /// </summary>
    public float scale = 160;
    /// <summary>
    /// A,B两点
    /// </summary>
    private Vector3 acpos;
    private Vector3 bcpos;

    void Start()
    {
        setCamera(gameObject);
    }

    void setCamera(GameObject gam)
    {
        if (gam.GetComponent<Canvas>() != null)
        {
            Canvas can = gam.GetComponent<Canvas>();
            //设置画布相机的渲染模式为worldCamera
            if (can.renderMode == RenderMode.ScreenSpaceOverlay)
            {
                UICamera = null;
            }
          else
            {
                //
                UICamera = can.worldCamera;
            }
            return;
        }
        //获得父类,Canvas物体
        gam = gam.transform.parent.gameObject;
        //在运行的过程中实时设置摄像机的渲染模式
        setCamera(gam);
    }
    /// <summary>
    /// 鼠标点击事件的处理
    /// </summary>
    /// <param name="eventData">鼠标点击事件处理类</param>
    public void OnPointerDown(PointerEventData eventData)
    {
        III(eventData);
    }
    public void OnDrag(PointerEventData eventData)
    {
        III(eventData);
    }

    /// <summary>
    /// 处理鼠标点击事件的方法
    /// </summary>
    /// <param name="eventData"></param>
    void III(PointerEventData eventData)
    {
            if (UICamera == null)
            {
                //获取A,B两点,并保存
                acpos = a.position;
                bcpos = b.position;
            }
            else
            {
                acpos = UICamera.WorldToScreenPoint(a.position);
                bcpos = UICamera.WorldToScreenPoint(b.position);
            }
        //计算两个点的差值
            float h = bcpos.y - acpos.y;
            float w = bcpos.x - acpos.x;
<span style="white-space:pre">	</span>//计算鼠标点击的点与A,B两点的差值
            float tempH = eventData.position.y - acpos.y;
            float tempW = eventData.position.x - acpos.x;
        //小地图与大地图的转换公式
            float dtw = tempW / w * scale;
            float dth = tempH / h * scale;
        //把新的位置赋值给players,实现在小地图中的移动
            Vector3 tempPos = new Vector3(dtw, players.transform.position.y, dth);
            players.transform.position = zerpos.transform.position + tempPos;
    }
}

你可能感兴趣的:(unity3d,地图)