【问题标题】:How to draw a monochromatic background in a 2d game with unity3d?如何用unity3d在2d游戏中绘制单色背景?
【发布时间】:2014-10-29 03:43:30
【问题描述】:

我正在使用 Unity3D 创建一个 2D 游戏,但我对它很陌生。 我正在尝试绘制一个单色背景,前面有一些精灵。

我找到了这段代码:

using UnityEngine;
using System.Collections;

public class GUIRect : MonoBehaviour {

    public Color color;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
    private static Texture2D _staticRectTexture;
    private static GUIStyle _staticRectStyle;

    // Note that this function is only meant to be called from OnGUI() functions.
    public static void GUIDrawRect( Rect position, Color color )
    {
        if( _staticRectTexture == null )
        {
            _staticRectTexture = new Texture2D( 1, 1 );
        }

        if( _staticRectStyle == null )
        {
            _staticRectStyle = new GUIStyle();
        }

        _staticRectTexture.SetPixel( 0, 0, color );
        _staticRectTexture.Apply();

        _staticRectStyle.normal.background = _staticRectTexture;

        GUI.Box( position, GUIContent.none, _staticRectStyle );
    }

    void OnGUI() {
        GUIDrawRect(Rect.MinMaxRect(0, 0, Screen.width, Screen.height), color);
    }
}

我将它附加到一个空的游戏对象上,它运行良好,但我无法确定它与场景中其他精灵之间的 z 顺序。

这是正确的方法吗?如果是这样,我应该如何更改它的绘制顺序?

【问题讨论】:

    标签: c# unity3d monochrome


    【解决方案1】:

    您使用的是 Unity Pro 吗?如果是这样,您可以使用后处理着色器。 http://docs.unity3d.com/Manual/script-GrayscaleEffect.html

    如果你想要一个 2d 背景,只需在启动 Unity 时使用 2d 设置。 然后你的背景可以是相互分层的纹理精灵。除了实际的 GUI 项目外,没有理由在 GUI 上绘图。 http://answers.unity3d.com/questions/637876/how-to-make-background-for-2d-game.html

    【讨论】:

    • 我没有使用 unity pro D:但是谢谢你的提示。那么要创建单色背景,我应该使用纹理吗?并在运行时改变它的颜色?
    • 是的,背景可以像纹理(jpg 等)一样简单。如果您处于 2d 模式,只需从您的资产中选择它并将其拖到屏幕上。然后只需确保您的相机处于正交模式并且您已关闭!如果您想要一个“分层”背景,您只需使用带有 Alpha 层的纹理,这样您就可以看穿它。
    • 没有看到你的最后一个问题-“gameObject.renderer.material.color = Color(0.777, 08, 0.604);”
    • 另外,如果您对编程不太感兴趣并且想要在变量(即颜色值)之间进行大量动画或缓动,那么请查看 iTween。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    相关资源
    最近更新 更多