Unity-Technologies / NativeRenderingPlugin

C++ Rendering Plugin example for Unity
https://docs.unity3d.com/Manual/NativePluginInterface.html
MIT License
815 stars 163 forks source link

Question on CreateRenderAPI #6

Closed podborski closed 4 years ago

podborski commented 4 years ago

Sorry for bothering you guys, but I really would like to understand one thing.

Can somebody explain to me why RegisterPlugin() is called only for WEBGL? https://github.com/Unity-Technologies/NativeRenderingPlugin/blob/2b8ca29bb59cdf981b8a9b669cb334bd09f6b44f/UnityProject/Assets/UseRenderingPlugin.cs#L56-L57

I don't quite understand from the code how OnGraphicsDeviceEvent (which creates an instance of the RenderAPI) gets called.

In other words, when not using UNITY_WEBGL how does CreateRenderAPI gets called? https://github.com/Unity-Technologies/NativeRenderingPlugin/blob/2b8ca29bb59cdf981b8a9b669cb334bd09f6b44f/PluginSource/source/RenderingPlugin.cpp#L144

I know that I am missing something, since the code seems to work just fine.

Thank you very much.

samjacobson commented 4 years ago

I'm not affiliated with Unity.

Unity calls UnityPluginLoad in the plugin dll when the plugin is first accessed. This calls OnGraphicsDeviceEvent directly (at the end of the function), and also registers that function with Unity here: https://github.com/Unity-Technologies/NativeRenderingPlugin/blob/2b8ca29bb59cdf981b8a9b669cb334bd09f6b44f/PluginSource/source/RenderingPlugin.cpp#L98 so that it will be called when a graphics device event occurs (e.g. shutdown).

podborski commented 4 years ago

@samjacobson Thank you :) This explains it. I just tested it and indeed, UnityPluginLoad gets called automatically. Is there any documentation on it that I have missed?

samjacobson commented 4 years ago

https://docs.unity3d.com/Manual/NativePluginInterface.html mentions the UnityPluginLoad.

podborski commented 4 years ago

Wow, how could I miss that? Thank you again!