Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto rotate angle #3

Open
hungfhp opened this issue Mar 29, 2020 · 1 comment
Open

Auto rotate angle #3

hungfhp opened this issue Mar 29, 2020 · 1 comment

Comments

@hungfhp
Copy link

hungfhp commented Mar 29, 2020

How i can setup this option?

@notyekta
Copy link

notyekta commented Mar 8, 2023

I wrote this code to animate the script. I dont know if it's too expensive to use or not

public enum AnimationType { Static,Yoyo, Restart, Linear};
public AnimationType animationType;

public float animSpeed;
private int dir;
public bool clockwise;

private void Update()
{
    if(animationType != AnimationType.Static)
    {
        AnimationRR();
    }
}

public void ModifyColor(float angle)
{
Rect rect = graphic.rectTransform.rect;
Vector2 dir = UIGradientUtils.RotationDir(angle);

    VertexHelper vh = new VertexHelper();

    if (!m_ignoreRatio)
        dir = UIGradientUtils.CompensateAspectRatio(rect, dir);

    UIGradientUtils.Matrix2x3 localPositionMatrix = UIGradientUtils.LocalPositionMatrix(rect, dir);

    UIVertex vertex = default(UIVertex);
    for (int i = 0; i < vh.currentVertCount; i++)
    {
        vh.PopulateUIVertex(ref vertex, i);
        Vector2 localPosition = localPositionMatrix * vertex.position;
        vertex.color *= Color.Lerp(m_color2, m_color1, localPosition.y);
        vh.SetUIVertex(vertex, i);
    }

    graphic.SetVerticesDirty();
}

public void AnimationRR()
{
    switch (animationType)
    {
        case AnimationType.Yoyo:
            if (dir == 0)
            {
                m_angle = Mathf.MoveTowards(m_angle, -180f, Time.deltaTime * animSpeed);
                if (m_angle < -179)
                {
                    dir = 1;
                }
            }
            else
            {
                m_angle = Mathf.MoveTowards(m_angle, 180f, Time.deltaTime * animSpeed);
                if (m_angle > 179)
                {
                    dir = 0;
                }
            }
            break;
        case AnimationType.Linear:
            if (clockwise)
            {
                m_angle = Mathf.MoveTowards(m_angle, -180f, Time.deltaTime * animSpeed);
                if (m_angle < -179)
                {
                    m_angle = 180f;
                }
            }
            else
            {
                m_angle = Mathf.MoveTowards(m_angle, 180f, Time.deltaTime * animSpeed);
                if (m_angle > 179)
                {
                    m_angle = -180;
                }
            }
            break;
        default:
            //Debug.LogError("Invalid animation type: " + animationType);
            break;
    }

    ModifyColor(m_angle);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants