unity学习三(cesium for unity 创建简单的地图弹窗)

该文指导如何在Unity中构建一个弹窗样式,通过读取GeoJson文件,根据经纬度数据批量加载并显示点位信息。创建空对象Point,添加Canvas和Image组件,设置RenderMode为WorldSpace。接着,读取Json数据,利用C#脚本根据数据创建并定位游戏对象,显示点位名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、右击选择create Empty,建一个空的对象,在此命名为Point

 2、右击Point--UI--Canvas,创建一个canvas,命名pointCanvas

3、点击pointCanvas,Render Mode设置为World Space,然后设置pointCanvas的宽度高度和位置

 以下工具可以自行设置大小和位置

4、右击pointCanvas--UI--Image,新建一个image标签,命名Name,右击Name--UI--Text- TextMeshPro,中文的话需要自己导入字体文件,设置字体大小颜色等等

图片放到项目的UI路径下, 设置如下,就会是透明的了

将图片拖到Name下给他的背景图

这样太丑了,给它加个图标,右击pointCanvas--UI--Image,命名Icon,同样给他一张图片 

这样就建好一个弹窗的样式,接下来需要根据txt点位文件,根据数据的经纬度批量加载进行撒点,写一个C#脚本,

在scripts文件夹下右击--create--C# script,新建命名addPointData的脚本

脚本内容如下 

using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class addPointData : MonoBehaviour
{
    // Start is called before the first frame update
        List<GameObject> pointGameObjects = new List<GameObject>();
    void Start()
    {
        List<GameObject> list = createPoint();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public List<GameObject> createPoint() {
        Point point;
        double x, y;
        List<Feature> featureCollection = GeoJsonUtil.ReadGeoJson(Application.streamingAssetsPath + "/Data/GeoJson/水文站.json");
        foreach (Feature feature in featureCollection)
        {
            Geometry geometry = feature.Geometry;
            point = geometry as NetTopologySuite.Geometries.Point;
            x = point.X;
            y = point.Y;
            GameObject gameObject = transform.Find("pointCanvas").gameObject;
            string name = feature.Attributes["Name"].ToString();
            //print(name);
            double3 xy = TransformUtil.LongitudeLatitudeHeightToUnityPosition(x, y, 0);
            Vector3 position = new Vector3((float)xy.x, 50, (float)xy.z);
            GameObject cloneGameObject = Instantiate(gameObject, position, gameObject.transform.rotation, gameObject.transform.parent);
            TMP_Text a = cloneGameObject.GetComponentInChildren<TMP_Text>();
            a.text = name;
            pointGameObjects.Add(cloneGameObject);
        }
        return pointGameObjects;
    }
}

项目中没有的依赖可以通过项目--管理NuGet程序包进行安装

点击运行项目,就会克隆出很多pointCanvas,点击scene,双击clone的对象就可定位到点位的具体位置

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我就是你的语法糖️

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值