Search Unity

Split Screen and Canvas

Discussion in 'Scripting' started by p1zzaman, Jan 23, 2019.

  1. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    Hi,

    I have a split screen game I am creating and was wondering how to assign the UI canvas for each player in its own camera view.

    I have an inventory UI that shows up when the player pushes a button, however, both of them are showing up on player 1's split screen.

    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    The canvas can be set to Screen Space - Camera for its Render mode.

    Clone the canvas and set the first one to camera1, the second one to camera2.

    I just tried it now, it works pretty slick. Each canvas is constrained and proportioned to its camera.
     
  3. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    awesome dude! Thanks!!
     
    Kurt-Dekker likes this.
  4. p1zzaman

    p1zzaman

    Joined:
    Jan 1, 2017
    Posts:
    64
    Also to add, set the canvas planeDistance to something very close like 1. Since my camera is of isometric top down view, the default 100 will render the canvas below the ground terrain prefabs. Needs to be up close to the camera.
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    Good point. Another thing you can do is just have a separate UI camera that matches each of the main cameras, i.e., has the same Rect sub-screen area and layer them above each main camera, and set their Clear to Depth Only.

    That way you can actually put those cameras somewhere else, like way down below the world, and give them very short frustums just far enough to see the UI canvas in front of them.

    That way you a) don't have to look at them in the scene, and b) they'll never clip your 3D scene. You can also fiddle with layers and put them wherever, but I prefer physical separation in scene world space most of the time.
     
  6. Asherhero

    Asherhero

    Joined:
    Aug 8, 2019
    Posts:
    5
    hey I want to make a 2 players game and I want to split the screen for it.
    In one screen there will be Player 1 and in the other there will be Player 2.Can someone tell me how to do this
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    Use the rect property in your cameras to make the two views render side-by-side, i.e., set one the left camera rect to (0,0,0.5f,1) and the right rect to (0.5f,0, 0.5f, 1)

    And then as my post above notes, if you have UI, set the canvases to screen-space-camera and drag each camera into it.

    If you support both one- and two-player modes, you might want to break all your cameras out into two separate additively-loaded "camera rig" scenes that you can choose to load based on mode.
     
  8. Asherhero

    Asherhero

    Joined:
    Aug 8, 2019
    Posts:
    5
    Can you sent me a video for it.
     
  9. Lockes_TheThief

    Lockes_TheThief

    Joined:
    Apr 28, 2014
    Posts:
    17
    I'm having issues with the split screen UI. I already made a canvas that was based on overlay. I followed your directions and duplicated the canvas and set them to camera. I put cameras1 and 2 in their respective locations. But now my entire canvas is gone. I've tried a plane distance between 100 to 1. I want to try the other suggestion of the UI camera but I can't really grasp the concept of it.
     
  10. Lockes_TheThief

    Lockes_TheThief

    Joined:
    Apr 28, 2014
    Posts:
    17
  11. Lockes_TheThief

    Lockes_TheThief

    Joined:
    Apr 28, 2014
    Posts:
    17
    Anyone has a fix for waypoints, yet? I've tried all the suggestions here, but the waypoint marker is still "bleeding" into the other player's viewport. I'm using vertical split screens. When the image moves off of player 1's viewport in the direction of player 2's, the marker doesn't disappear or hug the corners of player 1's viewport. Instead it just keeps going like it is on a full screen. @Kurt-Dekker @p1zzaman
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    No, this is your job to do. Here is how:

    1. Find out what computes the position of the waypoint marker on screen.

    2. Find out why it is not constrained to the visual area of the player you want it to be constrained to.

    3. Fix that.

    Remember, code is only a small part of the problem. The rest is setup. The best code in the world for clipping waypoints won't work if you fail to configure the scene with the proper rectangles for clipping.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
    eses likes this.
  13. Contato

    Contato

    Joined:
    Oct 2, 2015
    Posts:
    16
    Thank you so much, I completely forgot about that and I was having a bad time here.