Unity 自定义Image形状
代码如下(示例):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class HuiZhiBianYan: Graphic
{
private void OnGUI()
{
// 实时检测更新绘制 OnPopulateMesh 中 transform.child 位置
SetAllDirty();
}
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
if (transform.childCount <= 2)
{
return;
}
Color32 color32 = color;
foreach (Transform child in transform)
{
if (child.gameObject.activeSelf)
{
vh.AddVert(child.localPosition, color32, new Vector2(0f, 0f));
}
else
{
return;
}
}
for (int i = 0; i < transform.childCount - 2; i++)
{
// 几何图形中的三角形
vh.AddTriangle(i + 1, i + 2, 0);
}
}
}
提示:通过子物体坐标来绘制Image顶点 可自行更改
本文介绍如何在Unity中利用Graphics组件,通过子物体的局部坐标实现自定义Image的形状绘制,包括OnGUI和OnPopulateMesh的代码示例,适用于希望扩展UI灵活性的开发者。
5993

被折叠的 条评论
为什么被折叠?



