Search Unity

Using Raycast with PreviewRenderUtility scene

Discussion in 'Immediate Mode GUI (IMGUI)' started by gilley033, Mar 15, 2019.

  1. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    I have some objects which contain Box Colliders in a preview render utility scene. I have a valid Ray created using the Camera.ScreenPointToRay method, in which the camera used is the camera from preview render utility. However, using this ray, Physics.RaycastNonAlloc does not hit any objects in the scene, even though I know it should.

    I assume this is because the Physics methods work on the scene found in the Scene View. If that is the case, is there no work around? I suppose I will need to write my own method for translating a ray to a hit?

    Any ideas? Has anyone got this working? I guess preview render utility is not meant to be used in this manner, however it would be useful in my particular case.
     
  2. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Can you use the material raycast?

    I can't find the documentation, but it's the same cast that happens when you drag a material over a scene object (so although your object may start with a box collider, it may have been removed in the PRU scene)
     
    Last edited: Mar 15, 2019
  3. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Also, how are you adding objects to the PRU? If I recall correctly,the stock unity functions strip out all Components, but renderers, mesh filters and transforms
     
  4. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    I'm not sure, I cannot find a reference to this anywhere.
     
  5. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Objects are added via PreviewRenderUtility.InstantiatePrefabInScene, which just uses PrefabUtility.InstantiatePrefab under the hood. I checked to make sure after this call the collider is still there, and it does appear to be.

    Thanks for the suggestions though, I appreciate it!
     
  6. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
  7. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    I actually just stumbled across that method and it does indeed work, however I am getting the following error when I use it:

    Code (CSharp):
    1. GUI Window tried to begin rendering while something else had not finished rendering! Either you have a recursive OnGUI rendering, or the previous OnGUI did not clean up properly.
     
  8. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    Well I'm glad that method works :p

    When are calling it? Inside OnGUI? I think this call was designed to exist outside of OnGUI - with unity using it on drag and drop callbacks
     
  9. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    Yes, I am using it in OnGUI. I really hope there is a workaround, if not I will need to use a custom solution. The method API does not mention that it can only be used in specific scenarios, which leads me to believe this is a bug.

    I found this post on Unity Answers: https://answers.unity.com/questions/537049/gui-error-when-using-handleutilitypickgameobject.html

    The user recommends only calling the method during a MouseMove event, however for some reason this event is never triggered with my editor.
     
    BinaryCats likes this.
  10. BinaryCats

    BinaryCats

    Joined:
    Feb 8, 2016
    Posts:
    317
    yeah that sounds more familiar to other patterns I have found when doing things like this in OnGui, although my checks are usually exclusively wanting to do stuff on a repaint, or not on a repaint.

    have you tried only doing it when its not a repaint?
    Code (CSharp):
    1.         if (Event.current.type != EventType.Repaint)
    2.  
     
  11. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    The issue with that is, I need to process the mouse position whenever it is moved, but an event is not fired on mouse move (even though I believe it should be). It is only fired on Repaint, Layout, or when a mouse button is pressed.

    I tried processing things during the Layout event and I get the same error, so it appears you can't call this method during a Repaint or Layout event. Any other ideas?

    Edit: Okay, it looks like you have to set EditorWindow.wantsMouseMove to true in order for your editor window to trigger MouseMove events. It's working now! Thanks! I should have looked at the docs for the MouseMove EventType sooner!
     
    Last edited: Mar 17, 2019
    BinaryCats likes this.