简体   繁体   中英

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. 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.

Is it the correct approach? If so, how should I change it's draw order?

Are you using unity Pro? If so you can use post-processing shaders. http://docs.unity3d.com/Manual/script-GrayscaleEffect.html

If you want to have a 2d background, just use the 2d setting when starting Unity. Then your background can be texture sprites layered upon each other. No reason to draw on the GUI except for actual GUI items. http://answers.unity3d.com/questions/637876/how-to-make-background-for-2d-game.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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