简体   繁体   English

如何使用unity3d在2D游戏中绘制单色背景?

[英]How to draw a monochromatic background in a 2d game with unity3d?

I'm creating a 2D game with Unity3D, but I'm quite new to it. 我正在使用Unity3D创建2D游戏,但是我对此很陌生。 I'm trying to draw a monochromatic background, with some sprites in front of it. 我正在尝试绘制单色背景,并在其前面放置一些精灵。

I found this code: 我发现此代码:

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);
    }
}

I attached it to an empty game object, and it's working good, but I can't decide the z-ordering between it and other sprites in the scene. 我将其附加到一个空的游戏对象上,并且运行良好,但是我无法确定它与场景中其他精灵之间的z顺序。

Is it the correct approach? 这是正确的方法吗? If so, how should I change it's draw order? 如果是这样,我应该如何更改抽奖顺序?

Are you using unity Pro? 您是否正在使用Unity Pro? If so you can use post-processing shaders. 如果是这样,则可以使用后处理着色器。 http://docs.unity3d.com/Manual/script-GrayscaleEffect.html http://docs.unity3d.com/Manual/script-GrayscaleEffect.html

If you want to have a 2d background, just use the 2d setting when starting Unity. 如果要使用2d背景,只需在启动Unity时使用2d设置即可。 Then your background can be texture sprites layered upon each other. 然后,您的背景可以是彼此叠加的纹理精灵。 No reason to draw on the GUI except for actual GUI items. 除了实际的GUI项目外,没有理由在GUI上进行绘制。 http://answers.unity3d.com/questions/637876/how-to-make-background-for-2d-game.html http://answers.unity3d.com/questions/637876/how-to-make-background-for-2d-game.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM