- Home /
Is it possible to use AdditionalFiles with a Source Generator in Unity?
I was able to configure a source generator, that just generates .cs files on it's own, but I need to generate them based on a JSON file.
There is a tag called 'AdditionalFiles', that can be used in csproj to make a JSON-file visible to a source generator. But Unity generates it's csproj... I tried to add AdditionalFiles there on my own, but it seems to be not working. Although my Rider IDE generates them without a problem, the compiler in Unity says that it can't find the classes and namespaces I'm generating. So I'm assuming it doesn't find the JSON, because it needs one.
Unity docs on source generators are terribly shallow, so I'm asking for help here! Also there's a mistake about what .net standard version your SG should target - with Unity 2022.1 it should be 2.1, not 2.0.
Please, help, it's hard to Google this thing :) If any Devs are here, please tell if AdditionalFiles is supported, or I should generate cs files in another way, at least for now.
I'm using Unity 2022.1, my generator is targeting .net standard 2.1.
Answer by thepunkoff · May 14 at 09:08 AM
Well, I did my investigation. It's indeed possible to use AdditionalFiles. You should pass a flag to a compiler used by unity - "/additionalfiles: file list". File path is relative to the project root. You can do it in the Edit > Project Settings > Player, and look for "additional compiler arguments". So I passed "/additionalfiles: Assets/myfile.json", and was able to confirm, that it's not throwing any errors (e.g. non existing files make it throw FileNotFoundException during compilation), and I was able to generate a simple file with a static method that prints the json path, taken from context.AnalyzerOptions.AdditionalFiles
, and call it from the Unity script.
However it stops working if I try to parse my JSON with System.Text.Json in the generator source. It just doesn't compile. I tried all 6.0.0-preview versions, that the docs recommend, but none seems to be working. Well, this is a question for another topic.
Your answer
![](https://koobas.hobune.stream/wayback/20220613060528im_/https://answers.unity.com/themes/thub/images/avi.jpg)