チラ裏Unity

主にUnityについての備忘録ですが誰か様の為になれば

NGUIでのレイヤー管理

NGUI2.7のUIPanel下にGameObjectを追加してUIPanelとは別のLayerを設定しても、実行時に警告ログを出力しつつUIPanelのLayerで上書きされてしまいます

You can't place widgets on a layer different than the UIPanel that manages them.
If you want to move widgets to a different layer, parent them to a new panel instead.

 NGUIを使ってゲームっぽいものを作ろうとした時に、UIPanel下のレイヤーが全て統一されてしまうと少し不便です

 

レイヤー毎にUIPanelを作成する

警告ログで指示されている通りに解決します

  1.  Anchor下にUIPanelを追加する(NGUI > Create a Panel)
  2. 追加したUIPanelのLayerを設定する
  3. (追加したUIPanel下に必要なGameObjectを追加する)
  4. (追加したGameObjectのLayerは実行時に2.で設定したLayerで上書きされる)
  5. 複数のUIPanelを同時に写し撮るため、CameraのCulling MaskでそれぞれのUIPanelに設定したLayerを選択する
  6. UICameraのEvent Receiver Maskも5.と同様にLayerを選択する

 

NGUIのソースコードを改変してレイヤーを上書きさせない

お薦めできない力技ですので自己責任において

  1. UIwidget.csの342行目をコメントアウト
  2. public void CheckLayer ()

    {

            if (mPanel != null && mPanel.gameObject.layer != gameObject.layer)

            {

                    Debug.LogWarning("You can't place widgets on a layer different than the UIPanel that manages them.\n" +

                            "If you want to move widgets to a different layer, parent them to a new panel instead.", this);

    //              gameObject.layer = mPanel.gameObject.layer;

            }

    }