2

我无法理解如何分离控制器的业务逻辑和 Unity3D GUI 控件的视图。

例如,如果我有一个GUI.Box,我将如何实现一个控制器来淡入或淡出OnGUI视图的生命周期阶段?

看法

using UnityEngine;

public class ExampleView : MonoBehaviour {

    protected void OnGUI () {
        GUI.Box(new Rect(0, 0, 100, 100), "Title");
    }

}

如果我实例化一个控制器来更改它alphaGUI.color则需要Update()来自主视图线程的通知。

为了粗略地封装功能,如果这是一个单独的脚本,它可以实现为:

using UnityEngine;

public class ExampleView : MonoBehaviour {

    private Color color;

    protected void Start () {
        color = Color.white;
    }

    protected void OnGUI () {
        GUI.color = color;
        GUI.Box(new Rect(0, 0, 100, 100), "Title");
    }

    protected void Update () {
        if(color.a > 0)
            color.a -= Time.deltaTime / 3;
    }

}

类似于 iTweeniTween.fadeTo(gameObject, ...如何使用以下语句为 Unity3d GUI 控件实现对属性的更改进行动画处理FadeOut()

除非指定了多个控制器实例,否则可能无法针对单个 GUI 控件OnGUI()。但是,控制隔离的 GUI 实例(例如淡入淡出)会很酷,GUI.Box然后是GUI.Label.

4

2 回答 2

1

你在寻找这样的东西吗?:

using UnityEngine;

public class ExampleView : MonoBehaviour {

private Color color;
private bool bFadeIn = false;
private bool bFadeOut = false;

protected void Start () {
    color = Color.white;
} 
public void FadeOut(){
    bFadeOut = true;
    bFadeIn = false;
}    
public void FadeIn(){
    bFadeIn = true;
    bFadeOut = false;
}

protected void OnGUI () {
    GUI.color = color;
    GUI.Box(new Rect(0, 0, 100, 100), "Title");
}

protected void Update () {
    if(bFadeOut && color.a > 0.0)color.a -= Time.deltaTime / 3;
    if(bFadeIn && color.a < 1.0)color.a += Time.deltaTime / 3;
}
}

您可能还希望包含一个标志来确定控件何时不可见,如果不可见,则跳过完整的 GuiRender 函数。

于 2012-07-02T16:27:25.883 回答
0

不知道这是否是您正在寻找的,但它是一种向 gui 添加淡入淡出效果的简单方法。

using UnityEngine;
using System.Collections;

/**Fade In/Out Instructions:
* 1.Create bool fadeIn. Create float alpha.
* 2.Call Fade() method at start of OnGUI().
* 3.Put 'GUI.color = new Color(1,1,1,alpha)' above the elements you want to fade'.
* 4.To fade in, fadeIn = true. 
* 5.To fade out, fadeIn = false.
* 6.To deactivate buttons on fade out, wrap gui in 'if(alpha > 0)'
**/

public class MainMenuGUI : MonoBehaviour {

    int buttonWidth;
    int buttonHeight;

    public float alpha;
    public bool fadeIn;

// Use this for initialization
void Start () {

    buttonWidth = 100;
    buttonHeight = 50;

    fadeIn = true;
}

void OnGUI(){

    Fade();

    GUI.color = new Color(1,1,1,alpha);

    if(alpha > 0)
    {
        if (GUI.Button (new Rect ((Screen.width/2)-50, (Screen.height/2)-(buttonHeight*4/2), 100, 50), "PLAY"))
        {
            fadeIn = false;
            Debug.Log("Play pressed");
        }

        if (GUI.Button (new Rect ((Screen.width/2)-50, ((Screen.height/2)-(buttonHeight*4/2)+ buttonHeight), 100, 50), "SCORES")) 
        {
            Debug.Log("Scores pressed");
        }

        if (GUI.Button (new Rect ((Screen.width/2)-50, ((Screen.height/2)-(buttonHeight*4/2)+ buttonHeight*2), 100, 50), "OPTIONS")) 
        {
            Debug.Log("Options pressed");
        }

        if (GUI.Button (new Rect ((Screen.width/2)-50, ((Screen.height/2)-(buttonHeight*4/2)+ buttonHeight*3), 100, 50), "EXIT")) 
        {
            Debug.Log("Exit pressed");
        }
    }
}

void Fade(){
    if(fadeIn){
        alpha = Mathf.Clamp(alpha+0.01f,0,1);
    }else{
        alpha = Mathf.Clamp(alpha-0.01f,0,1);   
    }
}

}

祝你游戏好运:)

于 2013-11-20T20:14:06.217 回答