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的对象就可定位到点位的具体位置