标签代码
using System;
using UnityEngine;
[Serializable]
public class Item
{
public Vector3 direction;
public string content;
public float length;
public Transform transform;
public bool show;//是否显示
//public GameObject lable;//显示的标签
}
镜头显示标签代码
using System.Collections.Generic;
using UnityEngine;
using FrameWork;
using UnityEngine.Rendering;
/// <summary>
/// 屏幕标签
/// </summary>
public class ScreenDrowLine : MonoBehaviour
{
[SerializeField] Material mat;
[SerializeField] List<GameObject> Texts = new List<GameObject>();
public List<Item> items = new List<Item>();
[SerializeField] Transform lablesTra;
private void Start()
{
GameEvent.typeEventSystem.Register<InstantiateLable>(InstantiateLable);
}
#region URP渲染模式需另加代码
private void OnEnable()
{
RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
}
private void OnDisable()
{
RenderPipelineManager.endCameraRendering -= OnEndCameraRendering;
}
private void OnEndCameraRendering(ScriptableRenderContext arg1, Camera arg2)
{
OnPostRender();
}
#endregion
void OnPostRender()
{
GL.Color(Color.red);
GL.PushMatrix();
GL.LoadOrtho();
mat.SetPass(0);
for (int i = 0; i < items.Count; i++)
{
//if (items[i].show && items[i].transform.gameObject.activeSelf)
//{
// Texts[i].gameObject.SetActive(true);
GL.Begin(GL.LINES);
var pos = Camera.main.WorldToScreenPoint(items[i].transform.position);
GL.Vertex(new Vector2(pos.x / Screen.width, pos.y / Screen.height));
pos = Camera.main.WorldToScreenPoint(items[i].transform.position + items[i].direction * items[i].length);
if (pos.x > 1550 || pos.x < 45 || pos.y > 870 || pos.y < 110)
{
items[i].length = Mathf.Lerp(items[i].length, 0, Time.deltaTime * 5);
pos = Camera.main.WorldToScreenPoint(items[i].transform.position + items[i].direction * items[i].length);
}
else
{
var len = Mathf.Lerp(items[i].length, 2, Time.deltaTime * 5);
var temp = Camera.main.WorldToScreenPoint(items[i].transform.position + items[i].direction * len);
if (temp.x < 1540 && temp.x > 55 && temp.y < 860 && temp.y > 120)
{
pos = temp;
items[i].length = len;
}
}
GL.Vertex(new Vector2(pos.x / Screen.width, pos.y / Screen.height));
GL.End();
Texts[i].transform.position = pos;
//}
//else
//{
// Texts[i].gameObject.SetActive(false);
//}
}
GL.PopMatrix();
}
/// <summary>
/// 实例标签
/// </summary>
public void InstantiateLable(InstantiateLable instantiateLable)
{
LableItem ll = Instantiate(Resources.Load<LableItem>("UI/Prefab/LableItem"), lablesTra);
ll.SetText($"{instantiateLable.readyInfo.post}-{instantiateLable.readyInfo.name}");
Texts.Add(ll.gameObject);
Item item = new Item();
item.direction = new Vector3(0, 0, 0);
item.length = 2;
item.transform = instantiateLable.pepleTra;
item.show = true;
items.Add(item);
}
}
属性窗口
?
|