2014-10-29 331 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