C#下创建GUI展开动画效果


int i;
bool show = false;
bool grow = false;
float min = 0.0f;
float max = 500.0f;
float height = 0.0f;
float speed = 0.0f;
//var skin:GUISkin;
void OnGUI()
{
    if (GUI.Button(new Rect(5, 5, 104, 25), "List"))
    {
        grow = true;
        if (!show)
            show = true;
    }
    if (show)
    {
        GUILayout.BeginArea(new Rect(7, 30, height, height), "", "Box");
        GUILayout.BeginVertical();
        for (i = 0; i < 18; i++)
            GUILayout.Button(i + ".Title");
        GUILayout.EndVertical();
        GUILayout.EndArea();
    }
    if (grow)
    {
        speed += (float)(Time.deltaTime * 5.0);
        Debug.Log("speed:" + speed);
        height = Mathf.Lerp(min, max, speed);
        Debug.Log("height:" + height);
        if (Mathf.Approximately(height, max))
        {
            grow = false;
            max = min;
            min = height;
            speed = 0.0f;
            if (min == 0)
                show = false;
        }
    }
}



GUILayout.BeginVertical 开始垂直组
开始一个垂直控件的组。
所有被渲染的控件,在这个组里一个接着一个被垂直放置。该组必须调用EndVertical关闭。

Vertical Layout. 垂直布局
void OnGUI()
{
    // Starts a vertical group
    //开始一个垂直组
    GUILayout.BeginVertical("box");

    GUILayout.Button("I'm the top button");
    GUILayout.Button("I'm the bottom button");

    GUILayout.EndVertical();
}

GUILayout.BeginArea 开始区域
在一个固定的屏幕区域,开始一个GUI控件的GUILayout布局块;简单的说,在屏幕上开始一个固定大小的布局区域。
默认,使用GUILayout创建的任意GUI控件,放置在屏幕的左上角。如果你想在任意区域放置一系列自动布局控件,使用GUILayout.BeginArea定义新的区域,为自动布局系统使用。
Explained Area of the example.
void OnGUI()
{
    // Starts an area to draw elements
    //在开始区域中创建元素
    GUILayout.BeginArea(new Rect(200, 200, 100, 100));
    GUILayout.Button("Click me");
    GUILayout.Button("Or me");
    GUILayout.EndArea();
}

Mathf.Lerp 插值
static function Lerp (from : float, to : float, t : float) : float
基于浮点数t返回a到b之间的插值,t限制在0~1之间。
当t = 0返回from,当t = 1 返回to。当t = 0.5 返回from和to的平均值。

// Fades from minimum to maximum in one second
// 在一秒内从minimum渐变到maximum

public float minimum = 10.0F;
public float maximum = 20.0F;
void Update()
{
    transform.position = new Vector3(Mathf.Lerp(minimum, maximum, Time.time), 0, 0);
}


Mathf.Approximately 近似
static function Approximately (a : float, b : float) : bool
比较两个浮点数值,看它们是否非常接近。
由于浮点数值不精确,不建议使用等于来比较它们。例如,1.0==10.0/10.0也许不会返回true。
public void Awake() {
     if (Mathf.Approximately(1.0F, 10.0F / 10.0F))
          print("same");
}
发布了11 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/LQ753799168/article/details/75373800