Unity5의 uGUI에서 문자 그래디언트(Gradient)

1821 단어
코드는 다음과 같습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[AddComponentMenu("UI/Effects/Gradient")]
public class Gradient : BaseMeshEffect
{
    [SerializeField] private Color32 topColor = Color.white;

    [SerializeField] private Color32 bottomColor = Color.black;

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive()) {
            return;
        }

        var vertexList = new List<UIVertex>();
        vh.GetUIVertexStream(vertexList);
        int count = vertexList.Count;
        
        ApplyGradient(vertexList, 0, count);
        vh.Clear();
        vh.AddUIVertexTriangleStream(vertexList);
    }

    private void ApplyGradient(List<UIVertex> vertexList, int start, int end)
    {
        float bottomY = vertexList[0].position.y;
        float topY = vertexList[0].position.y;
        for (int i = start; i < end; ++i) {
            float y = vertexList[i].position.y;
            if (y > topY) {
                topY = y;
            } else if (y < bottomY) {
                bottomY = y;
            }
        }

        float uiElementHeight = topY - bottomY;
        for (int i = start; i < end; ++i) {
            UIVertex uiVertex = vertexList[i];
            uiVertex.color = Color32.Lerp(bottomColor, topColor, (uiVertex.position.y - bottomY)/uiElementHeight);
            vertexList[i] = uiVertex;
        }
    }
}

주로 우송의 문장과 그 아래의 회답을 참고하였다.여기서 하나 더 설명하자면 Unity의 uGUI의 특수 효과 처리는 모두 정점에 기초한 것이다. 이것은 글씨체에 음영, 묘사, 점차적인 처리를 전문적으로 쓰는 것이 아니라 정점 처리로 그 효과를 시뮬레이션한 것이다.따라서 글에 음영 (묘사) 과 점차적인 변화가 동시에 있을 때 스크립트의 추가 순서에 주의해야 한다.
먼저 점차적인 스크립트를 추가한 다음에 묘사된 스크립트를 추가해야 한다. 그렇지 않으면 코드 논리에 따라 처리한다. 점차적인 정점 처리는 묘사된 정점 색깔도 모두 수정한다. 즉, 묘사 효과가 없다.

좋은 웹페이지 즐겨찾기