栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity中UGUI小地图实现的原理

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity中UGUI小地图实现的原理

计算出Unity中场景的长于宽,计算出人物在场景中的X与Y轴,然后计算X与Y的位置在场景大地图上的比例,按照比例给小地图赋值即可。

public class UIMap : MonoBehaviour
{
    //左下角的点
    [SerializeField]
    Transform Map_one;
    //右上角的点
    [SerializeField]
    Transform Map_Two;
    //获取人物
    [SerializeField]
    Transform player;
    //获取地图上面的红点
    [SerializeField]
    RectTransform mao_Point;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        SmallMap();
    }
    void SmallMap()
    {
        //获取大地图的长和宽
        float mapLength = Mathf.Abs(Map_one.position.x - Map_Two.position.x);
        //宽
        float mapwidth = Mathf.Abs(Map_one.position.z - Map_Two.position.z);

        //获取小地图的长和宽
        float smallMapLength = GetComponent().rect.height;
        //长
        float smallMapwidth = GetComponent().rect.width;

        //获取玩家在大地图中的坐标
        float PlayX = player.position.x;
        float PlayY = player.position.z;

        //获取玩家位置在大地图上的比例
        float scaleX = PlayX / mapLength;
        float scaleY = PlayY / mapwidth;

        //计算地图上的红点在小地图的比例
        float PointX = scaleX * smallMapwidth;
        float PointY = scaleY * smallMapLength;

        //让地图上的红点实时跟随人物的变化而变化
        mao_Point.localPosition = new Vector2(PointX, PointY);


        
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901841.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号