| | |
| | | //默认10,不同的颜色(包括调整alpha)视觉上描边粗细有差异,故shader里乘以0.2适应不同情况 |
| | | public int OutlineWidth = 10; |
| | | |
| | | private static List<UIVertex> m_VetexList = new List<UIVertex>(); |
| | | private List<UIVertex> m_VetexList = new List<UIVertex>(); |
| | | |
| | | protected override void Start() |
| | | { |
| | |
| | | { |
| | | if (base.graphic.material == null || base.graphic.material.shader.name != "TSF Shaders/UI/OutlineEx") |
| | | { |
| | | // #if UNITY_EDITOR |
| | | |
| | | var texMaterial = ResManager.Instance.LoadAsset<Material>("Materials", "OutlineMat"); |
| | | if (texMaterial != null) |
| | | { |
| | |
| | | { |
| | | Debug.LogError("没有找到材质OutlineMat.mat"); |
| | | } |
| | | // #else |
| | | // var shader = Shader.Find("TSF Shaders/UI/OutlineEx"); |
| | | // base.graphic.material = new Material(shader); |
| | | // #endif |
| | | } |
| | | |
| | | if (base.graphic.canvas) |
| | |
| | | base.graphic.canvas.additionalShaderChannels |= v2; |
| | | } |
| | | } |
| | | |
| | | this._Refresh(); |
| | | |
| | | // 注册字体纹理重建事件(动态字体首次渲染时可能未就绪) |
| | | if (base.graphic is Text textComponent && textComponent.font != null) |
| | | { |
| | | Font.textureRebuilt += OnFontTextureRebuilt; |
| | | } |
| | | } |
| | | } |
| | | |
| | | private void OnFontTextureRebuilt(Font font) |
| | | { |
| | | if (base.graphic is Text textComponent && textComponent.font == font) |
| | | { |
| | | // 在字体重建回调中不能直接调用SetVerticesDirty,需要延迟 |
| | | if (gameObject.activeInHierarchy) |
| | | { |
| | | StartCoroutine(DelayRefresh()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private System.Collections.IEnumerator DelayRefresh() |
| | | { |
| | | yield return null; |
| | | this._Refresh(); |
| | | } |
| | | |
| | | protected override void OnDestroy() |
| | | { |
| | | base.OnDestroy(); |
| | | Font.textureRebuilt -= OnFontTextureRebuilt; |
| | | } |
| | | |
| | | protected override void OnDisable() |
| | | { |
| | | base.OnDisable(); |
| | | Font.textureRebuilt -= OnFontTextureRebuilt; |
| | | } |
| | | |
| | | protected override void OnEnable() |
| | | { |
| | | base.OnEnable(); |
| | | if (base.graphic is Text textComponent && textComponent.font != null) |
| | | { |
| | | Font.textureRebuilt += OnFontTextureRebuilt; |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | private void _Refresh() |
| | | { |
| | | /*if (base.graphic.material.GetInt("_OutlineWidth") != this.OutlineWidth || base.graphic.material.GetColor("_OutlineColor") != this.OutlineColor) |
| | | { |
| | | base.graphic.material.SetColor("_OutlineColor", this.OutlineColor); |
| | | base.graphic.material.SetInt("_OutlineWidth", this.OutlineWidth); |
| | | }*/ |
| | | base.graphic.SetVerticesDirty(); |
| | | } |
| | | |
| | |
| | | var uv = pVertex.uv0; |
| | | |
| | | |
| | | uv += new Vector4((pUVX / pTriangleX.magnitude * posXOffset * (Vector2.Dot(pTriangleX, Vector2.right) > 0 ? 1 : -1)).x, (pUVX / pTriangleX.magnitude * posXOffset * (Vector2.Dot(pTriangleX, Vector2.right) > 0 ? 1 : -1)).y, 0, 0); |
| | | uv += new Vector4((pUVY / pTriangleY.magnitude * posYOffset * (Vector2.Dot(pTriangleY, Vector2.up) > 0 ? 1 : -1)).x, (pUVY / pTriangleY.magnitude * posYOffset * (Vector2.Dot(pTriangleY, Vector2.up) > 0 ? 1 : -1)).y, 0, 0); |
| | | // X方向UV偏移 |
| | | float xSign = Vector2.Dot(pTriangleX, Vector2.right) >= 0 ? 1f : -1f; |
| | | Vector2 uvOffsetX = (pUVX / pTriangleX.magnitude) * posXOffset * xSign; |
| | | |
| | | // Y方向UV偏移 - 使用更稳定的判断方式 |
| | | float ySign = pTriangleY.y >= 0 ? 1f : -1f; |
| | | Vector2 uvOffsetY = (pUVY / pTriangleY.magnitude) * posYOffset * ySign; |
| | | |
| | | uv += new Vector4(uvOffsetX.x + uvOffsetY.x, uvOffsetX.y + uvOffsetY.y, 0, 0); |
| | | |
| | | pVertex.uv0 = uv; |
| | | |