Text渐变

 

#

public class MyText : Text
{

    [SerializeField]
    protected bool _gray = false;

    [SerializeField]
    protected bool _gradient = false;
    [SerializeField]
    private bool _isMultiLineGradient = true;
    [SerializeField]
    private Color _topColor = Color.white;
    [SerializeField]
    private Color _bottomColor = Color.black;

    public bool Gray
    {
        set
        {
            if (_gray != value)
            {
                _gray = value;
                SetVerticesDirty();
            }
        }
        get
        {
            return _gray;
        }
    }

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        base.OnPopulateMesh(vh);
        if (_gray)
        {
            var tempVert = new UIVertex();
            var grayColor = Color.gray;
            int n = vh.currentVertCount;
            for (int i = 0; i < n; i++)
            {
                SetVertColor(vh, ref tempVert, i, ref grayColor);
            }
        }
        else if (_gradient)
        {
            UpdateGradient(vh);
        }
    }

    private void UpdateGradient(VertexHelper vh)
    {
        if (!string.IsNullOrEmpty(m_Text))
        {
            int vertCount = vh.currentVertCount;
            if (0 == vertCount)
                return;
            //Debug.Log($"===== {vertCount}, {vh.currentIndexCount}");

            if (!_isMultiLineGradient) //每一行独立的渐变
            {
                var tempVert = new UIVertex();
                for (var i = 0; i < vertCount; i += 4) //顺时针方向: left-top, right-top, right-bottom, left-bottom
                {
                    SetVertColor(vh, ref tempVert, i, ref _topColor);
                    SetVertColor(vh, ref tempVert, i + 1, ref _topColor);
                    SetVertColor(vh, ref tempVert, i + 2, ref _bottomColor);
                    if (i + 3 < vertCount)
                        SetVertColor(vh, ref tempVert, i + 3, ref _bottomColor);
                }
            }
            else //所有行一起渐变
            {
                UIVertex tempVert = new UIVertex();
                var topY = float.MinValue;
                var bottomY = float.MaxValue;
                //找出最上面的y和最下面的y
                for (var i = 0; i < vertCount; i++)
                {
                    vh.PopulateUIVertex(ref tempVert, i);
                    var pos = tempVert.position;
                    if (pos.y > topY)
                    {
                        topY = pos.y;
                    }
                    else if (pos.y < bottomY)
                    {
                        bottomY = pos.y;
                    }
                }

                var height = topY - bottomY;
                for (var i = 0; i < vertCount; i++)
                {
                    vh.PopulateUIVertex(ref tempVert, i);

                    var color = Color32.Lerp(_bottomColor, _topColor, (tempVert.position.y - bottomY) / height);
                    tempVert.color = color;

                    vh.SetUIVertex(tempVert, i);
                }
            }
        }
    }

    static void SetVertColor(VertexHelper vh, ref UIVertex tempVert, int index, ref Color c)
    {
        vh.PopulateUIVertex(ref tempVert, index);
        tempVert.color = c;
        vh.SetUIVertex(tempVert, index);
    }

}

# Inspector部分

[CustomEditor(typeof(MyText))]
[CanEditMultipleObjects]
public class MyTextEditor : Editor
{

    private Type _editorType;
    private UnityEditor.Editor _editorInstance;

    protected MyText m_text;

    SerializedProperty m_gray;

    SerializedProperty _gradient;
    SerializedProperty _isMultiLineGradient;
    SerializedProperty _topColor;
    SerializedProperty _bottomColor;

    private void OnEnable()
    {
        if (null != _editorInstance)
            return;

        if (null == _editorType)
        {
            var typeFullName = "UnityEditor.UI.TextEditor";
            var dllName = "UnityEditor.UI";
            _editorType = Type.GetType($"{typeFullName},{dllName}");
        }
        if (null == _editorType) return;

        if (null != targets)
        {
            _editorInstance = CreateEditor(targets, _editorType);

            m_gray = serializedObject.FindProperty("_gray");

            _gradient = serializedObject.FindProperty("_gradient");
            _isMultiLineGradient = serializedObject.FindProperty("_isMultiLineGradient");
            _topColor = serializedObject.FindProperty("_topColor");
            _bottomColor = serializedObject.FindProperty("_bottomColor");
        }
    }

    private void OnDisable()
    {
        if (null != _editorInstance)
        {
            DestroyImmediate(_editorInstance);
            _editorInstance = null;
        }
    }

    public override void OnInspectorGUI()
    {
        if (null != _editorInstance)
            _editorInstance.OnInspectorGUI();
        OnInspectorGUI_Text();
    }

    private void OnInspectorGUI_Text()
    {
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(m_gray);

        EditorGUILayout.LabelField("-----------渐变-----------");
        EditorGUILayout.PropertyField(_gradient);
        if (_gradient.boolValue)
        {
            EditorGUILayout.PropertyField(_isMultiLineGradient);
            EditorGUILayout.PropertyField(_topColor);
            EditorGUILayout.PropertyField(_bottomColor);
        }

        if (EditorGUI.EndChangeCheck())
        {
            var b1 = serializedObject.ApplyModifiedProperties();
            Debug.Log($"modify: {b1}");

            var b2 = serializedObject.UpdateIfRequiredOrScript();
            Debug.Log($"update: {b2}");
        }
    }

}

 

【关于VertexHelper】

 # 单个顶点操作

PopulateUIVertex

SetUIVertex

 

# 顶点批量操作

GetUIVertexStream:注意这个接口会将四边形的顶点数从4个变为6个

AddUIVertexTriangleStream:顶点数需要是3的倍数

AddUIVertexStream:使用顶点和顶点索引

 

# 添加顶点

AddVert:添加1个顶点

AddUIVertexQuad:正方形的4个顶点

AddTriangle:三角形索引

 

【关于文字顶点坐标】

 # 文字顶点坐标是相对于Text的pivot的local坐标

第1个这的4个顶点坐标: (-1, 0), (31, 0), (31, -32), (-1, -32)

第3个这的4个顶点坐标: (-1, -68), (31, -68), (31, -100), (-1, -100)

 

# 参考

Unity 顶点基础 + OnPopulateMesh_断天涯zzz的博客-CSDN博客_unity 顶点

 

posted @ 2022-04-04 16:35  yanghui01  阅读(56)  评论(0编辑  收藏  举报