-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Source generator for Unity 2021 #15
Comments
Rosalina uses an I don't know much about source generators, but if it's a compile-time (or build) action, this will not fit into the code generation process since the binding script should be always synchronized with the Of course, you can "force" the binding script generation through the |
It's really nice that this is automatic on asset change! Did not notice that. One upside with Source Generators is that their output is not actual files in the project, but just compiled classes. And when using frameworks with "code behind" generation, the classes are indeed reflected on asset change. However, as Unity is a special little snowflake, maybe this approach would not work very well, as one does not "build" Unity in order to compile it. |
Indeed, if it was a "pure" C# project, this would be a nice solution. |
That's great @blepmlem ! Would love to see how you achieve to implement source generators within Unity. Feel free to open a PR and contribute 😄 Reopening the issue for better tracking. |
My fork is located at https://github.com/blepmlem/Rosalina/tree/source-generator |
The gist of it is that I implement an ISyntaxReceiver for finding the relevant scripts to create partial classes for, then pass the information to the RosalinaBindingsGenerator, it does its thing as usual, and the output code is added to the compilation process. |
Wow, that is awesome! I'll certainly try to get this capability into my fork (unless it can be added to this repo somehow). |
So, I've been doing some researches and tests around the source generator feature and it seems that it works great in a "native" C# environment. Let me explain. According to the Unity documentation (https://docs.unity3d.com/Manual/roslyn-analyzers.html), to create a Roslyn analyzer or a Source Generator, you'll need to create a .NET library targeting In our use case, everytime we make a change to a UXML file, the While I believe all of the steps I mentioned above are possible using an Unity script ; but while I was testing I noticed that when I regenerate the DLL with new generated code and import it into Unity again (overriding existing dll), changes were not visible, even if when forcing the reimport process using "Right Click -> "Reimport" on the asset dll. Thus, there is a "risk" that the users using Rosalina experiment some weird behaviors. What I can suggest for now, let's keep this issue active and see if Unity intend to "natively" support Source Geneartors inside Unity itself using the .NET APIs in the future. To avoid having the generated files next to the UXML files, in the mean time, we could follow issue #13 and put all generated files into a |
Are you sure this is the intended meaning of the docs page? From what I can read, yes, the source generator needs to be a DLL with a specific label, but it requires no more modification. In my mind what you need to do, is trigger a recompile ( But perhaps you attempted this, and it does not work this way? |
Well I just followed the official documentation and they say:
With the current Rosalina/Editor/Scripts/RosalinaAssetProcessor.cs Lines 8 to 12 in 2d9bffd
At the end of the process the AssetDatabase is refreshed:
With this solution, we actually see the generated code into your asset files, but with source generators, the source code is directly "injected" into the final assembly (in the use case described in the Unity official documentation, a |
I have been doing more research on source generators and it seems that Unity is working on source generators according to the "Engineering" roadmap: https://unity.com/roadmap/unity-platform/engineering I don't know if it's going to be integrated like a |
Unity 2021 supports Source Generators:
Instead of having to manually click "generate", this would just generate the code when compiling, meaning your assets would always be in sync with your generated code.
The text was updated successfully, but these errors were encountered: