udpate 0.6.6
This commit is contained in:
@@ -63,6 +63,59 @@ namespace XericLibrary.Runtime.UIGraph
|
||||
/// </summary>
|
||||
protected int m_DirtyEndSegment = -1;
|
||||
|
||||
/// <summary>报表用:这一帧真正提交的顶点数(=缓存收缩后的大小,不是历史最高水位)</summary>
|
||||
public int LiveVertexCount { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 每块顶点上限:UGUI 的 65535 是**每个 Graphic 各自**的上限,线段数一多就要切块。
|
||||
/// 布局是均匀的(每段 VerticesPerSegment 个顶点),所以按段数切即可。
|
||||
/// </summary>
|
||||
public const int ChunkVertexBudget = 16000;
|
||||
|
||||
/// <summary>一块:只画段 [FirstSegment, LastSegment)</summary>
|
||||
[RequireComponent(typeof(CanvasRenderer))]
|
||||
private sealed class LineChunk : MaskableGraphic
|
||||
{
|
||||
public CacheLineRendererBase Owner;
|
||||
public int FirstSegment;
|
||||
public int LastSegment;
|
||||
|
||||
/// <summary>贴图跟宿主走:宿主换贴图,块也得跟着换</summary>
|
||||
public override Texture mainTexture
|
||||
=> Owner != null ? Owner.mainTexture : base.mainTexture;
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
raycastTarget = false;
|
||||
SyncOwnerVisual();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 块是**独立的 Graphic**:宿主身上的材质不会自动继承。
|
||||
/// 注意 Graphic.material 的 getter 在没有用户材质时返回的是 defaultMaterial,
|
||||
/// 所以这里读到/写下的都是“宿主实际会用的那一份”(曲线基类那份就是 XericUICurve)。
|
||||
/// </summary>
|
||||
public void SyncOwnerVisual()
|
||||
{
|
||||
if (Owner == null) return;
|
||||
if (material != Owner.material) material = Owner.material;
|
||||
}
|
||||
|
||||
protected override void OnPopulateMesh(VertexHelper vh)
|
||||
{
|
||||
vh.Clear();
|
||||
if (Owner != null) Owner.PopulateSlice(vh, FirstSegment, LastSegment);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly List<LineChunk> m_Chunks = new List<LineChunk>();
|
||||
private int[] m_SegmentIndexStart = new int[0];
|
||||
|
||||
/// <summary>报表用:切了几块 / 最大的那块有多少顶点</summary>
|
||||
public int ChunkCount => m_Chunks.Count;
|
||||
public int MaxChunkVertices { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否使用 Jobs 模式
|
||||
/// </summary>
|
||||
@@ -199,6 +252,7 @@ namespace XericLibrary.Runtime.UIGraph
|
||||
m_MeshIndexCache.Clear();
|
||||
m_DirtyStartSegment = -1;
|
||||
m_DirtyEndSegment = -1;
|
||||
DisableChunks();
|
||||
SetVerticesDirty();
|
||||
}
|
||||
|
||||
@@ -267,6 +321,14 @@ namespace XericLibrary.Runtime.UIGraph
|
||||
for (int i = 0; i < growBy; i++)
|
||||
m_MeshVertexCache.Add(new UIVertex());
|
||||
}
|
||||
else if (m_MeshVertexCache.Count > neededVertices)
|
||||
{
|
||||
// 缩小到当前真正需要的大小:缓存只增不减时,OnPopulateMesh 会把**整个列表**
|
||||
// 交给 VertexHelper,于是提交的顶点数是"历史最高水位"——一旦超过 UGUI 的 65535
|
||||
// 就永久报错(数据降下来也不回落)。必须每次推送都跟着当前内容收缩。
|
||||
m_MeshVertexCache.RemoveRange(neededVertices, m_MeshVertexCache.Count - neededVertices);
|
||||
}
|
||||
LiveVertexCount = neededVertices;
|
||||
|
||||
m_MeshIndexCache.Clear();
|
||||
|
||||
@@ -302,8 +364,16 @@ namespace XericLibrary.Runtime.UIGraph
|
||||
}
|
||||
|
||||
// 5) 生成三角形索引
|
||||
// 每段索引的起点表要**先**扩容:写它的就是 GenerateIndicesForRange 里那趟循环,
|
||||
// 放到后面扩容等于"这一趟谁都写不进去",表里只剩末尾标记,
|
||||
// 非最末那几块就会读出 indexEnd=0 ⇒ 提交了顶点却一个三角形都没有(块在、线不见)。
|
||||
int recordedSegments = GetTotalSegmentCount();
|
||||
if (m_SegmentIndexStart.Length < recordedSegments + 1)
|
||||
m_SegmentIndexStart = new int[recordedSegments + 1];
|
||||
GenerateIndicesForRange(m_DirtyStartSegment, m_DirtyEndSegment,
|
||||
globalSegBase);
|
||||
m_SegmentIndexStart[recordedSegments] = m_MeshIndexCache.Count;
|
||||
RebuildChunks();
|
||||
|
||||
// 清除脏标记
|
||||
SetVerticesDirty();
|
||||
@@ -378,6 +448,8 @@ namespace XericLibrary.Runtime.UIGraph
|
||||
for (int si = 0; si < subSegCount; si++)
|
||||
{
|
||||
// 子段 si == 0 是该条线的首段 → 无前驱同线段 → hasPrevInLine = false
|
||||
if (segIdx >= 0 && segIdx < m_SegmentIndexStart.Length)
|
||||
m_SegmentIndexStart[segIdx] = m_MeshIndexCache.Count;
|
||||
bool hasPrevInLine = (si > 0);
|
||||
LineMeshGenerator.GenerateSegmentIndices(segIdx, hasPrevInLine, m_MeshIndexCache);
|
||||
segIdx++;
|
||||
@@ -500,13 +572,102 @@ namespace XericLibrary.Runtime.UIGraph
|
||||
|
||||
protected override void OnPopulateMesh(VertexHelper vh)
|
||||
{
|
||||
// 本体不画:所有线段都交给受顶点预算约束的子块
|
||||
vh.Clear();
|
||||
}
|
||||
|
||||
if (m_MeshVertexCache.Count == 0 || m_MeshIndexCache.Count == 0)
|
||||
return;
|
||||
/// <summary>按顶点预算把段切成若干块(顺序不变,画家序不受影响)</summary>
|
||||
private void RebuildChunks()
|
||||
{
|
||||
int total = GetTotalSegmentCount();
|
||||
int perChunk = Mathf.Max(1, ChunkVertexBudget / LineMeshGenerator.VerticesPerSegment);
|
||||
int chunks = (total + perChunk - 1) / perChunk;
|
||||
|
||||
// 将缓存的顶点和索引写入 VertexHelper
|
||||
vh.AddUIVertexStream(m_MeshVertexCache, m_MeshIndexCache);
|
||||
EnsureChunkCount(chunks);
|
||||
MaxChunkVertices = 0;
|
||||
for (int c = 0; c < chunks; c++)
|
||||
{
|
||||
int first = c * perChunk;
|
||||
int last = Mathf.Min(total, first + perChunk);
|
||||
m_Chunks[c].FirstSegment = first;
|
||||
m_Chunks[c].LastSegment = last;
|
||||
m_Chunks[c].enabled = true;
|
||||
m_Chunks[c].SyncOwnerVisual();
|
||||
m_Chunks[c].SetVerticesDirty();
|
||||
int verts = (last - first) * LineMeshGenerator.VerticesPerSegment;
|
||||
if (verts > MaxChunkVertices) MaxChunkVertices = verts;
|
||||
}
|
||||
for (int c = chunks; c < m_Chunks.Count; c++) m_Chunks[c].enabled = false;
|
||||
}
|
||||
|
||||
private void EnsureChunkCount(int count)
|
||||
{
|
||||
while (m_Chunks.Count < count)
|
||||
{
|
||||
// Graphic 的 CanvasRenderer 是**懒创建**的(只在属性被访问时补),而 UGUI 的 Graphic
|
||||
// 自己并不要求它 —— 三个基类都是显式贴了 RequireComponent 才有的。块这里直接建出来,
|
||||
// 不依赖"哪天 Rebuild 恰好被调到"(没有它 Rebuild 第一句就 return,静默不画)。
|
||||
var chunkGo = new GameObject("LineChunk" + m_Chunks.Count,
|
||||
typeof(RectTransform), typeof(CanvasRenderer));
|
||||
// 先挂父级再 AddComponent:OnEnable 里的 CacheCanvas 与注册看的是**当时**的父级
|
||||
var rect = (RectTransform)chunkGo.transform;
|
||||
rect.SetParent(transform, false);
|
||||
rect.anchorMin = Vector2.zero;
|
||||
rect.anchorMax = Vector2.one;
|
||||
rect.offsetMin = Vector2.zero;
|
||||
rect.offsetMax = Vector2.zero;
|
||||
// 块是运行时建的,不标 DontSave 的话 Play 里存一下就会把它们写进场景/预制体
|
||||
chunkGo.hideFlags = HideFlags.DontSave;
|
||||
var chunk = chunkGo.AddComponent<LineChunk>();
|
||||
chunk.Owner = this;
|
||||
chunk.raycastTarget = false;
|
||||
m_Chunks.Add(chunk);
|
||||
}
|
||||
}
|
||||
|
||||
private void DisableChunks()
|
||||
{
|
||||
for (int c = 0; c < m_Chunks.Count; c++)
|
||||
{
|
||||
if (m_Chunks[c] != null) m_Chunks[c].enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把段 [firstSegment, lastSegment) 的顶点与三角形写进这个块。
|
||||
/// 块首多带**前一段**的顶点:同一根线里子段的转角连接三角形会引用上一段的顶点,
|
||||
/// 不带就会在块的接缝处缺一个角。
|
||||
/// </summary>
|
||||
internal void PopulateSlice(VertexHelper vh, int firstSegment, int lastSegment)
|
||||
{
|
||||
int total = GetTotalSegmentCount();
|
||||
if (firstSegment < 0) firstSegment = 0;
|
||||
if (lastSegment > total) lastSegment = total;
|
||||
if (firstSegment >= lastSegment) return;
|
||||
|
||||
int perSeg = LineMeshGenerator.VerticesPerSegment;
|
||||
int vertexStart = Mathf.Max(0, firstSegment - 1) * perSeg;
|
||||
int vertexEnd = lastSegment * perSeg;
|
||||
if (vertexEnd > m_MeshVertexCache.Count) vertexEnd = m_MeshVertexCache.Count;
|
||||
if (vertexStart >= vertexEnd) return;
|
||||
|
||||
int indexStart = (firstSegment < m_SegmentIndexStart.Length)
|
||||
? m_SegmentIndexStart[firstSegment] : m_MeshIndexCache.Count;
|
||||
int indexEnd = (lastSegment < m_SegmentIndexStart.Length)
|
||||
? m_SegmentIndexStart[lastSegment] : m_MeshIndexCache.Count;
|
||||
if (indexEnd > m_MeshIndexCache.Count) indexEnd = m_MeshIndexCache.Count;
|
||||
|
||||
for (int v = vertexStart; v < vertexEnd; v++) vh.AddVert(m_MeshVertexCache[v]);
|
||||
for (int i = indexStart; i + 2 < indexEnd; i += 3)
|
||||
{
|
||||
int a = m_MeshIndexCache[i];
|
||||
int b = m_MeshIndexCache[i + 1];
|
||||
int d = m_MeshIndexCache[i + 2];
|
||||
if (a < vertexStart || a >= vertexEnd) continue;
|
||||
if (b < vertexStart || b >= vertexEnd) continue;
|
||||
if (d < vertexStart || d >= vertexEnd) continue;
|
||||
vh.AddTriangle(a - vertexStart, b - vertexStart, d - vertexStart);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -521,14 +682,37 @@ namespace XericLibrary.Runtime.UIGraph
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
// 几何长在子块上,宿主关掉不等于它们停了:Graphic 停用只影响自己那份 VertexHelper
|
||||
DisableChunks();
|
||||
base.OnDisable();
|
||||
}
|
||||
|
||||
protected override void OnDestroy()
|
||||
{
|
||||
DestroyChunks();
|
||||
base.OnDestroy();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 块是宿主运行时建的子物体:只删组件不删它们,会留下一堆孤儿块,
|
||||
/// 组件再挂回来时 EnsureChunkCount 又建一组同名的 ⇒ 两组块叠着画。
|
||||
/// </summary>
|
||||
private void DestroyChunks()
|
||||
{
|
||||
for (int c = 0; c < m_Chunks.Count; c++)
|
||||
{
|
||||
var chunk = m_Chunks[c];
|
||||
if (chunk == null) continue;
|
||||
var chunkGo = chunk.gameObject;
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying) DestroyImmediate(chunkGo); else Destroy(chunkGo);
|
||||
#else
|
||||
Destroy(chunkGo);
|
||||
#endif
|
||||
}
|
||||
m_Chunks.Clear();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user