using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour
{
[Header("3d物体分散")]
public List<Transform> list_obj;
[Header("根节点root")]
public Transform center;
[Header("gui预制件")]
public GameObject image;
//gui身上挂载
private Image_data[] image_Dates;
private Line_data[] line_Dates;
public Canvas canvas;
// Start is called before the first frame update
void Start()
{
Init();
}
// Update is called once per frame
void Update()
{
Update_image();
Update_line();
}
void Init()
{
if (canvas == null)
{
canvas =FindObjectOfType<Canvas>();
}
//中心点
GameObject obj_center = Instantiate(image, canvas.transform.GetChild(0));
obj_center.AddComponent<Image_data>().vector3 = center.position;
obj_center.GetComponent<Image>().color = Color.green;
obj_center.name = "root";
//分支点与线条
for (int i = 0; i < list_obj.Count; i++)
{
GameObject obj = Instantiate(image, canvas.transform.GetChild(0));
obj.AddComponent<Image_data>().vector3 = list_obj[i].position;
GameObject line = Instantiate(image, canvas.transform.GetChild(1));
Line_data line_Data = line.AddComponent<Line_data>();
line_Data.start_pos = obj_center;
line_Data.end_pos = obj;
line_Data.color = Color.red;
line_Data.width = 2;
}
image_Dates = FindObjectsOfType<Image_data>();
line_Dates= FindObjectsOfType<Line_data>();
}
/// <summary>
/// 更新热点
/// </summary>
void Update_image()
{
if (image_Dates.Length <= 0) return;
for (int i = 0; i < image_Dates.Length; i++)
{
//3d物体世界坐标转屏幕坐标,父类原点在左下角 canvas.scaleFactor作用于自适应画布大小
Vector3 target_pos = Camera.main.WorldToScreenPoint(image_Dates[i].vector3) / canvas.scaleFactor;
//正面
if (target_pos.z >= 0)
{
if (target_pos.x <= 0)
{
target_pos.x = 0;
}
else if (target_pos.x >= Screen.width)
{
target_pos.x = Screen.width;
}
if (target_pos.y <= 0)
{
target_pos.y = 0;
}
else if (target_pos.y >= Screen.height)
{
target_pos.y = Screen.height;
}
}//背面
else
{
target_pos.x = 0;
target_pos.y = 0;
}
image_Dates[i].transform.position = new Vector3(target_pos.x, target_pos.y, 0);
}
}
/// <summary>
/// 更新线条
/// </summary>
void Update_line()
{
if (line_Dates.Length <= 0) return;
for (int i = 0; i < line_Dates.Length; i++)
{
Line_data line_Data = line_Dates[i].GetComponent<Line_data>();
RectTransform rectTransform = line_Dates[i].GetComponent<RectTransform>();
rectTransform.sizeDelta = new Vector2(line_Data.width, Vector2.Distance(line_Data.start_pos.transform.position, line_Data.end_pos.transform.position));
rectTransform.pivot = new Vector2(0.5f, 0f);
rectTransform.anchoredPosition =line_Data.start_pos.transform.position;
//设置角度
double angle = Math.Atan2(line_Data.end_pos.transform.position.y -line_Data.start_pos.transform.position.y, line_Data.end_pos.transform.position.x -line_Data.start_pos.transform.position.x) * 180 / Math.PI;
line_Dates[i].GetComponent<RectTransform>().rotation = Quaternion.Euler(0, 0, (float)angle + 270);
}
}
}
/// <summary>
/// 热点类
/// </summary>
public class Image_data:MonoBehaviour
{
//3d坐标存起来
public Vector3 vector3;
public Color color;
}
/// <summary>
/// 线条类
/// </summary>
public class Line_data : MonoBehaviour
{
public GameObject start_pos;
public GameObject end_pos;
public Color color;
public int width;
}
2DGUI跟随3d物体移动
猜你喜欢
转载自blog.csdn.net/fanfan_hongyun/article/details/127224930
今日推荐
周排行