Graphics.DrawTexture() on 1 camera

Hi, I am trying to create some nice gui for my game. I have a split screen setup going on and I have come to some problem. Using GUITextures, I was able to draw only on the camera I choose, by that I mean, I could create a GUIGroup that has the dimensions of each camera, and use that to draw my GUI on both screens. The problem is that Graphics.DrawTexture doesn’t seem to have that functionality(as far as I can see), can anyone guide me on the best way to achieve this?

p.s. I switched to Graphics.DrawTexture so I could use the source Rectangle paramater to do 2d animations on my gui.

GUI. and Graphics. draws not to camera, but to screen. you can draw texture even without any camera, so you are not able to control graphics.drawtexture draws by switching cameras

I guess you could use one of the camera event to bind the texture drawing to a certain camera and to distinguish which one is rendering. See Camera.current and the mentioned events, especially OnRenderObject. This event is called for each camera. You have to setup the viewport yourself in this case.

Another way, probably the better, is to use OnGUI and setup two GUIs for each view. Keep in mind that there is the GUIUtility class which has some functions to convert between gui and screen space. Those functions take GUI.Groups into account.

edit

When using GUILayout you can use GUILayoutUtility.GetRect to reserve an area for the drawtexture call. The rects origin has to be converted into screen-space of course (GUIToScreenPoint) :wink:

I almost exclusively use GUILayout since it arranges itself and you can still specify fix heights / widths for certain elements.

//C#
void OnGUI()
{
GUILayout.BeginArea(new Rect(0,0,Screen.width,Screen.height));
GUILayout.BeginHorizontal();

GUILayout.BeginVertical();
// Left GUI here
GUILayout.EndVertical();

GUILayout.BeginVertical();
// Right GUI here
GUILayout.EndVertical();

GUILayout.EndHorizontal();
GUILayout.EndArea();

}

If the GUI is exactly the same for both sides, i would use a function that takes a context as parameter

void PlayerGUI(PlayerContext player)
{
    Rect myTexRect = GUILayoutUtility.GetRect(100,50);
    
    // GUI here
    
    if (Event.current.type == EventType.Repaint)
    {
        Vector2 tmp = GUIUtility.GUIToScreenPoint(new Vector2(myTexRect.x, myTexRect.y));
        myTexRect.x = tmp.x;
        myTexRect.y = tmp.y;
        Graphics.DrawTexture(myTexRect, /* ... */);
    }

}

//[...]
GUILayout.BeginVertical();
PlayerGUI(player1);
GUILayout.EndVertical();

GUILayout.BeginVertical();
PlayerGUI(player2);
GUILayout.EndVertical();
//[...]