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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Enable and disable Renderer or Canvas not working

Discussion in 'Scripting' started by foxvalleysoccer, Apr 6, 2017.

  1. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    Every 5 seconds I need to take a photo and display it in the world. That part is working fine.
    Once a photo is taken it need to disappear 1 second after. That's where I'm stuck.
    I've gotten it to disappear but then I cant get the new picture to reappear when its function is called again.

    Any ideas?

    I've tried:
    m_CanvasRenderer.enabled = false;
    m_CanvasRenderer.enabled = true;
    m_Canvas = null;
    m_Canvas.setActive(false);
    m_Canvas.setActive(true);



    Code (CSharp):
    1. public class PhotoCaptureExample : MonoBehaviour
    2. {
    3.     GestureRecognizer m_GestureRecognizer;
    4.     GameObject m_Canvas = null;
    5.     Renderer m_CanvasRenderer = null;
    6.     PhotoCapture m_PhotoCaptureObj;
    7.     CameraParameters m_CameraParameters;
    8.     bool m_CapturingPhoto = false;
    9.     Texture2D m_Texture = null;
    10.  
    11.     void Start()
    12.     {
    13.         Initialize();
    14.         InvokeRepeating("TakePhoto", 5.0f, 5.0f);
    15.         //StartCoroutine(TakePhoto());
    16.     }
    17.  
    18. void TakePhoto()
    19.     {
    20.         if (m_CapturingPhoto)
    21.         {
    22.             return;
    23.         }
    24.         m_CapturingPhoto = true;
    25.         m_PhotoCaptureObj.TakePhotoAsync(OnPhotoCaptured);
    26.     }
    27.  
    28.     void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    29.     {
    30.        // m_CanvasRenderer.enabled = true;
    31.  
    32.         if (m_Canvas == null)
    33.         {
    34.  
    35.             m_Canvas = GameObject.CreatePrimitive(PrimitiveType.Quad);
    36.             m_Canvas.name = "PhotoCaptureCanvas";
    37.             m_CanvasRenderer = m_Canvas.GetComponent<Renderer>() as Renderer;
    38.             m_CanvasRenderer.material = new Material(Shader.Find("AR/HolographicImageBlend"));
    39.         }
    40.  
    41.         Matrix4x4 cameraToWorldMatrix;
    42.         photoCaptureFrame.TryGetCameraToWorldMatrix(out cameraToWorldMatrix);
    43.         Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse;
    44.  
    45.         Matrix4x4 projectionMatrix;
    46.         photoCaptureFrame.TryGetProjectionMatrix(out projectionMatrix);
    47.  
    48.         photoCaptureFrame.UploadImageDataToTexture(m_Texture);
    49.         m_Texture.wrapMode = TextureWrapMode.Clamp;
    50.  
    51.         m_CanvasRenderer.sharedMaterial.SetTexture("_MainTex", m_Texture);
    52.         m_CanvasRenderer.sharedMaterial.SetMatrix("_WorldToCameraMatrix", worldToCameraMatrix);
    53.         m_CanvasRenderer.sharedMaterial.SetMatrix("_CameraProjectionMatrix", projectionMatrix);
    54.         m_CanvasRenderer.sharedMaterial.SetFloat("_VignetteScale", 1.0f);
    55.  
    56.         // Position the canvas object slightly in front
    57.         // of the real world web camera.
    58.         Vector3 position = cameraToWorldMatrix.GetColumn(3) - cameraToWorldMatrix.GetColumn(2);
    59.  
    60.         // Rotate the canvas object so that it faces the user.
    61.         Quaternion rotation = Quaternion.LookRotation(-cameraToWorldMatrix.GetColumn(2), cameraToWorldMatrix.GetColumn(1));
    62.  
    63.         m_Canvas.transform.position = position;
    64.         m_Canvas.transform.rotation = rotation;
    65.  
    66.         m_CapturingPhoto = false;
    67.  
    68.         float counter = 0; float target = 1;
    69.         while (counter < target)
    70.         {
    71.             counter += Time.deltaTime;
    72.         }
    73.         // m_CanvasRenderer.enabled = false;
    74.      
    75.     }
    76. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    First off, I admit though while I grasp the concept of most of your code, it's over my head.
    However, the ability to turn something on/off , visibility-wise should be pretty straight-forward.
    Whatever you turned off (renderer/canvas), after the canvas check against null .. turn it back on.
    One note, though, is that you're forcing a spin loop waiting for 1 second in your script. That's a different issue, though.
    If you turn on the renderer manually in the inspector on the second time through, is the photo there just invisible?
     
  3. Malleck666

    Malleck666

    Joined:
    Jan 9, 2015
    Posts:
    235
    Get the <Canvas> component and just toggle that instead of the actual gameObject.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just a note, when I was reading this post, it appeared as though he was creating a unique "canvas" improv.
    I could be wrong or misunderstanding it, but his canvas is a quad..
     
  5. Malleck666

    Malleck666

    Joined:
    Jan 9, 2015
    Posts:
    235
    You are correct.

    My bad, early mornings...
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    All good.. Confused me, too. I sometimes use 'general' names like that, but other times I can see how un-nice it can be when you go back/others read it :)
     
  7. foxvalleysoccer

    foxvalleysoccer

    Joined:
    May 11, 2015
    Posts:
    108
    I'm working in the hololens so I cant turn the renderer on manually in the inspector while running.
    I thought it would be strait forward to turning something invisible then visible again.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Should be straight forward. Hope you figure it out.