2014-10-29 333 views
1

我正在用Unity3D创建2D游戏,但我对它很陌生。 我想绘制一个单色背景,在它前面有一些小精灵。如何使用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排序。

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

回答

1

你使用的是统一的专业版吗?如果是这样,你可以使用后期处理着色器。 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

+0

我不使用团结亲D:但谢谢你的提示。 那么要创建一个单色背景,我应该使用纹理? 并在运行时改变它的颜色? – ProGM 2014-10-29 04:20:30

+0

是的,背景可以像纹理一样简单(jpg等)。如果您处于2D模式,只需从您的资产中选择它并将其拖到屏幕上即可。然后只要确保你的相机是正确的,你离开了!如果你想要一个“分层”的背景,你只需要使用一个带有alpha图层的纹理,这样你就可以看穿它。 – 2014-10-29 05:55:33

+1

没有看到最后一个问题 - “gameObject.renderer.material.color = Color(0.777,08,0.604);” – 2014-10-29 06:01:36