• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by lsdrambo · Mar 12, 2017 at 06:07 PM · uimultiplayerbillboardworld spacesplit-screen

World Space UI facing multiple Cameras (Billboard)

I'm working on a split screen multiplayer game. I have many world space UI canvas always facing the camera (billboard). I have a hard time making them face both cameras at the same time.

I tried to rotate the canvas to face the cameras using Camera.onPreCull and Camera.onPreRender callbacks. The object do receive the callback from both cameras every frame, but only the rotation from the last camera to be rendered seems to affect the UI object. If I replace the object for a standard 3D plane with a MeshRenderer, everything works as expected, ie the plane faces both cameras. I also tried OnWillRenderObject but it doesn't work as it needs to be attached to a gameObject with a Renderer, which is not the case for a Canvas.

Here is my camera facing script:

 public class LookAtCamera : MonoBehaviour
 {
     void OnEnable() { Camera.onPreRender += PreRender; }    
     void OnDisable() { Camera.onPreRender -= PreRender; }    
     void PreRender(Camera cam)
     {
         transform.LookAt(transform.position + cam.transform.rotation * Vector3.forward,
                          cam.transform.rotation * Vector3.up);
     }
 }

I ran out of solutions and I implemented a workaround by duplicating Canvas for each cameras using camera specific layers on each of them:

 public class DuplicateGameObjectOnMultipleLayers : MonoBehaviour
 {
     [SerializeField]
     LayerMask layers;
 
     bool first = true;
 
     void Start()
     {
         for(int i = 0; i < 32; i++)
         {
             if(((1 << i) & layers) != 0)
             {
                 if(first)
                 {
                     gameObject.layer = i;
                     first = false;
                 }
                 else
                 {
                     var newGameObject = (GameObject)Instantiate(gameObject, transform.position, transform.rotation, transform.parent);
                     newGameObject.gameObject.layer = i;
                     newGameObject.GetComponent<DuplicateUIOnMultipleLayers>().enabled = false;  //Avoid infinite recursion
                 }
             }
         }
     }
 }

I had to modify the LookAtCamera scripts to apply rotation only for cameras that renders the object's layer:

 public class LookAtCamera : MonoBehaviour
 {
     void OnEnable() { Camera.onPreRender += PreRender; }    
     void OnDisable() { Camera.onPreRender -= PreRender; }
 
     void PreRender(Camera cam)
     {
         if((cam.cullingMask & (1 << gameObject.layer)) != 0)
         {
             transform.LookAt(transform.position + cam.transform.rotation * Vector3.forward,
                              cam.transform.rotation * Vector3.up);
         }
     }
 }

Clearly not the best solution.

Does anyone know how to have a world space UI canvas facing multiple camera at the same time without duplicating it?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image koval832 · Jul 02, 2017 at 08:53 AM 0
Share

@lsdrambo Hey! Did you find more elegant solution? :) Also ran into the same issue. Thanks.

avatar image lsdrambo koval832 · Oct 04, 2017 at 07:12 PM 0
Share

No, I'm still using the "duplicating canvas solution" ...

avatar image Bip901 · Nov 18, 2017 at 11:27 AM 0
Share

I have this problem as well. I'll try the double canvas solution...

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Restricting Canvas navigation for each player 0 Answers

Splitscreen w/ different GUIs on each camera 2 Answers

Drop in/ Drop out Co-op 0 Answers

UNET - UI Not updating with Healthbar and other bars. 1 Answer

How to make a split-screen? (2 cameras rendering at same time) 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges