簡體   English   中英

如何使用unity3d在2D游戲中繪制單色背景?

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

我正在使用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順序。

這是正確的方法嗎? 如果是這樣,我應該如何更改抽獎順序?

您是否正在使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM