using UnityEngine;
using UnityEngine.UIElements;
namespace CustomUI
{
public class RombElement : VisualElement
{
public new class UxmlFactory : UxmlFactory<RombElement> { }
public RombElement()
{
generateVisualContent += GenerateVisualContent;
}
Vertex[] vertices = new Vertex[4];
ushort[] indices = { 0, 1, 2, 2, 3, 0};
void GenerateVisualContent(MeshGenerationContext mgc)
{
vertices[0].tint = new Color32(255, 0, 0, 255);
vertices[1].tint = new Color32(0, 255, 0, 255);
vertices[2].tint = new Color32(0, 0, 255, 255);
vertices[3].tint = new Color32(17, 55, 55, 255);
var top = 0;
var left = 0f;
var middleX = contentRect.width / 2;
var middleY = contentRect.height / 2;
var right = contentRect.width;
var bottom = contentRect.height;
vertices[0].position = new Vector3(left, middleY, Vertex.nearZ);
vertices[1].position = new Vector3(middleX, top, Vertex.nearZ);
vertices[2].position = new Vector3(right, middleY, Vertex.nearZ);
vertices[3].position = new Vector3(middleX, bottom, Vertex.nearZ);
MeshWriteData mwd = mgc.Allocate(vertices.Length, indices.Length);
mwd.SetAllVertices(vertices);
mwd.SetAllIndices(indices);
}
}
}
Спасиб за статью!
Оставлю мини-шутку все равно
Рассказываю как создавать кастомные UI элементы через создание Mesh’а, используся Unity и UI Toolkit.Шаг 1: Наследуемся от VisualElement
Шаг 2: Unity выпускает новое соглашение с разработчиками
Шаг 3: Устанавливаем Unreal Engine
...
Шаг 4: Мудохаемся с Unreal Engine
Шаг 5: Возвращаемся в Unity
Хорошая шутка)
UI Toolkit в целом выглядит очень вкусно, особенно если есть опыт работы с вебом. Но они совсем его не обновляют, печаль.
Насколько знаю, они над ним работают, даже выпускают фичи:
https://forum.unity.com/threads/whats-new-in-the-ui-toolkit-documentation.1256697/
Вот уж что не ожидал увидеть на ДТФ, так это гайд по Unity. Да не просто по Unity, а по UI Toolkit. Да не просто по UI Toolkit, а по UI Toolkit для рантайма, а не редактора.
Насколько помню, были уже гайды по Unity, а вот по UI Toolkit, вроде не было, тут да)