19 lines
4.2 KiB
C#
19 lines
4.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
namespace XericLibrary.Runtime.Blueprint.QuickGraph
|
|
{
|
|
[BlueprintTool(phase: ToolPhase.Render, order: 0)] [BlueprintTheme("QuickGraph")]
|
|
public class QuickUGUIGraphGridBackgroundTool : BlueprintTool
|
|
{
|
|
private const string Name = "__Bp_GridBackground"; private QuickGraphRenderStyleResolver _styles; private Image _image; private Material _material; private string _ownedShader; private bool _transformErrorLogged; private bool _materialUnavailableErrorLogged;
|
|
public override void OnInitialize() { _styles = new QuickGraphRenderStyleResolver(Graph); _styles.Changed += _ => Graph?.MarkDirty(); Ensure(); Rebind(_styles.Snapshot.Grid); }
|
|
public override void OnConfigChanged() => _styles?.Refresh();
|
|
public override void OnRender() { Ensure(); if (_styles == null) return; var style = _styles.Snapshot.Grid; Rebind(style); Apply(style); }
|
|
private void Ensure() { if (_image != null || Graph?.Canvas?.GetRootTransform() == null) return; var root = Graph.Canvas.GetRootTransform(); var existing = root.Find(Name); if (existing != null) _image = existing.GetComponent<Image>(); if (_image == null) { if (existing != null) Object.DestroyImmediate(existing.gameObject); var go = new GameObject(Name, typeof(RectTransform), typeof(Image)); go.hideFlags = HideFlags.HideAndDontSave; go.transform.SetParent(root, false); go.transform.SetAsFirstSibling(); var rt = go.GetComponent<RectTransform>(); rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.one; rt.offsetMin = Vector2.zero; rt.offsetMax = Vector2.zero; _image = go.GetComponent<Image>(); } }
|
|
private void Rebind(QuickGraphGridRenderStyle style) { if (_image == null) return; Material target = style.Material; string owned = null; if (target == null && !string.IsNullOrEmpty(style.ShaderName)) { if (_material != null && _ownedShader == style.ShaderName) return; var shader = Shader.Find(style.ShaderName); if (shader != null) { target = new Material(shader) { name = "BpGridBackground_StyleMat" }; owned = style.ShaderName; } } if (target == null) { if (!_materialUnavailableErrorLogged) { Debug.LogError($"[QuickGraph] 网格 material 与 shader 均不可用: GraphId={Graph?.GraphId}, material={style.Material}, shader=\"{style.ShaderName}\"。"); _materialUnavailableErrorLogged = true; } } else _materialUnavailableErrorLogged = false; if (target == _material) return; ReleaseMaterial(); _material = target; _ownedShader = owned; _image.material = target; }
|
|
private void Apply(QuickGraphGridRenderStyle style) { if (_image == null) return; _image.raycastTarget = style.RaycastTarget; _image.maskable = style.Maskable; if (_material == null || Graph?.Canvas == null) return; var size = _image.rectTransform.rect.size; var zoom = Graph.Canvas.ZoomLevel; if (style.Size != 0f && zoom != 0f) { var pan = Graph.Canvas.PanOffset; _material.SetVector("_Transform", new Vector4(size.x / (zoom * style.Size), size.y / (zoom * style.Size), -(pan.x + .5f * size.x) / (zoom * style.Size), -(pan.y + .5f * size.y) / (zoom * style.Size))); _transformErrorLogged = false; } else if (!_transformErrorLogged) { Debug.LogError("[QuickGraph] 网格样式 size 或 zoom 为 0,未写入 _Transform。"); _transformErrorLogged = true; } _material.SetFloat("_GridOverlayPower", style.OverlayPower); _material.SetFloat("_GridLineThreshold", style.Threshold); _material.SetFloat("_GridExp", style.Exp); _material.SetColor("_GridColor", style.Color); _material.SetColor("_GridBackgroundColor", style.Background); _material.SetFloat("_LodTransthd", style.LodTransitionThreshold); _material.SetFloat("_LodTransitionStart", style.LodTransitionStart); _material.SetFloat("_LodTransitionEnd", style.LodTransitionEnd); _material.SetVector("_CellCenter", style.CellCenter); _material.SetFloat("_LineShapeScale", style.LineShapeScale); }
|
|
private void ReleaseMaterial() { if (_image != null) _image.material = null; if (_material != null && _ownedShader != null) { if (Application.isPlaying) Object.Destroy(_material); else Object.DestroyImmediate(_material); } _material = null; _ownedShader = null; }
|
|
public override void OnDestroy() { _styles?.Dispose(); ReleaseMaterial(); if (_image != null) { if (Application.isPlaying) Object.Destroy(_image.gameObject); else Object.DestroyImmediate(_image.gameObject); } _image = null; }
|
|
}
|
|
}
|