Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Set renderCamera in renderMode 'Screen Space - Camera' from code?

Discussion in 'UGUI & TextMesh Pro' started by Rodyfish, Nov 3, 2014.

  1. Rodyfish

    Rodyfish

    Joined:
    Sep 4, 2014
    Posts:
    18
    Im trying to instantiate Canvas prefabs on running time, and I want the Canvas to have RenderMode 'Screen Space - Camera'.

    Other than that, I have a separate GuiCam (Camera) in the scene, and I want to set it as the renderCamera for the instantiated canvas. I cant seem to find a way that works for me, to do this from code.

    I'm using C#.

    Q: How do you set renderCamera on a Canvas from code? Do you have any suggestions? :)
     
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    the canvas class should have a .renderMode which you can set as the Canvas.RenderMode.ScreenSpaceCamera.

    Same goes for the camera there is the worldCamera you can set.
     
  3. Rodyfish

    Rodyfish

    Joined:
    Sep 4, 2014
    Posts:
    18
    I doesn't work for me, and I cant find anything in the documentation local. Do I need a certain library? Im using 4.6.20b.
     
    Last edited: Nov 4, 2014
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    you'll have to show a code snippet to what you're doing as both those functions on the canvas have been around since very early betas if not alpha.
     
  5. Rodyfish

    Rodyfish

    Joined:
    Sep 4, 2014
    Posts:
    18
    Oki I'll try. There is a good chance, that there is something fundamental I'm just not aware of. The following code is attached to the canvas I'm instantiating runtime.

    The canvas has RenderMode 'Screen Space - Camera', and I want to set the renderCamera for this canvas to cam in the code.

    But I cant find Canvas.Rendermode, og canvas.rendermode.'something'.

    Code (CSharp):
    1.  
    2. private Camera cam;
    3.  
    4.     // Use this for initialization
    5.     void Start ()
    6.     {
    7.         Canvas canvas = gameObject.GetComponent<Canvas>() ?? null;
    8.         cam = GameObject.FindGameObjectWithTag("GuiCamera").camera;
    9.         if(canvas != null)
    10.         {
    11.         // Set canvas' renderCamera to cam
    12.         }
    13.         else print ("derp!");
    14.  
    15.     }
     
  6. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    So i just copied your script into a new script in a blank project and was able to do the following with help from autocomplete.

    Code (CSharp):
    1. private Camera cam;
    2.  
    3.     // Use this for initialization
    4.     void Start()
    5.     {
    6.         Canvas canvas = gameObject.GetComponent<Canvas>();
    7.         cam = GameObject.FindGameObjectWithTag("GuiCamera").camera;
    8.         if (canvas != null)
    9.         {
    10.             canvas.renderMode = RenderMode.OverlayCamera;
    11.             canvas.worldCamera = cam;
    12.         }
    13.  
    14.     }
     
    SirDJCat, A_Marraff, sean244 and 2 others like this.
  7. Rodyfish

    Rodyfish

    Joined:
    Sep 4, 2014
    Posts:
    18
    Oh, I have tried the second line of yours, but didn't new I needed to do the first. Thanks for the help, Ill appreciate it!
     
  8. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    i dont know if you need to do the first or not. If you already have set the canvas in the inspector to "Screen Space - Camera" then you shouldnt need to.
     
  9. SirDJCat

    SirDJCat

    Joined:
    Jan 4, 2020
    Posts:
    30
    11 years later and this was still super helpful!