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. Dismiss Notice

Question Embedding resources in assemblies

Discussion in 'Editor & General Support' started by CelesteMarina, Oct 3, 2023.

  1. CelesteMarina

    CelesteMarina

    Joined:
    Jul 28, 2013
    Posts:
    11
    I spent a while yesterday writing a post-processor for unity's csproj files to insert includes for embedded resx files, however no matter what I've tried, the compiled dll does not contain any embedded resources.

    Is there something within unity stripping / ignoring embedded resources? The documentation notes that the WebGL has a useEmbeddedResources option, which seems to indicate that it's possible. Does anyone have any insight on what might be the issue?

    The generated includes are:

    Code (CSharp):
    1.     <EmbeddedResource Include="Assets/Scenes/Management/Resources/SceneExceptions.resx">
    2.       <Generator>ResXFileCodeGenerator</Generator>
    3.       <LastGenOutput>SceneExceptions.Designer.cs</LastGenOutput>
    4.     </EmbeddedResource>
    and

    Code (CSharp):
    1.     <Compile Include="Assets/Scenes/Management/Resources/SceneExceptions.Designer.cs">
    2.       <DesignTime>True</DesignTime>
    3.       <AutoGen>True</AutoGen>
    4.       <DependentUpon>SceneExceptions.resx</DependentUpon>
    5.     </Compile>
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Never heard of anyone using resx in a Unity project. I think that‘s a Visual Studio concept that isn‘t supported by Unity. The Designer part indicates the GUI design tool that comes with VS which is definitely not compatible.

    All resources (assets) need to be in the AssetDatabase or in special folders which means the files need to be in the Assets folder or subfolder. If you need files included verbatim „as is“ you would have to place them in the StreamingAssets folder.

    What do you need this for?
     
  3. CelesteMarina

    CelesteMarina

    Joined:
    Jul 28, 2013
    Posts:
    11
    resx and designer are part of the .NET localisation and System.Resources, not Visual Studio. The designer file is code generated by .NET tooling providing an API to access the defined resources. Both of these files *are* in the assets tree, the issue is that the resx must be embedded within the assembly for it to work correctly.

    Generally, I use Resx files to define strings, as adding localised variants is extremely easy. I'm not opposed to a different solution, but I'd like to be able to use the .NET concept within Unity.

    MSBuild with those project elements should be embedding the file within the assembly ordinarily. I can only assume that Unity has some process stopping or undoing said embed.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    This is the only mention of .resx that indicates it might be possible: https://forum.unity.com/threads/resx-files.725558/

    Whether it works or not largely depends on whether Unity supports and implements the necessary System.Resources namespace classes. These may or may not (all) be available in the .NET version Unity uses which is based on Mono or .NET Standard (you can change which the project uses in Player settings).

    Unity has its own localization package: https://docs.unity3d.com/Packages/com.unity.localization@1.4/manual/index.html
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    ^ ^ ^ This... and also remember a lot of stuff is simply NOT implemented in IL2CPP, such as tons of the Process namespace. IL2CPP is is required for both iOS and Android and WebGL work.

    Generally, stay within Unity for Unity stuff. While using outside "circuits" to get stuff into your Unity binary may technically be possible, you are committing to an endless future stream of bugs and incompatibilities for something that (in the case of localization) is just super-ultra-trivial: replace this string with that string.
     
  6. CelesteMarina

    CelesteMarina

    Joined:
    Jul 28, 2013
    Posts:
    11
    It's more of an academic interest at this point, but the core of what I'm getting at is *why* unity is stripping the files. Unity can handle embedded data as there are several examples I've found on this forum of people embedded data into externally compiled libraries and using them within Unity. I have not, however, found an example of anyone successfully embedded data into a unity-compiled assembly.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Unity is the compiler. It's the one making these assemblies. Any modifications you make to them will always be overridden whenever your project recompiles or is built.
     
  8. CelesteMarina

    CelesteMarina

    Joined:
    Jul 28, 2013
    Posts:
    11
    Unity uses csc to compile .NET Framework >=4.0. Unity itself is not a compiler.
    I'm not making modifications to assemblies. I'm using AssetPostProcessor.OnGeneratedCSProject to inject the xml into the csproj. The comments on the method indicate this is persistent ("This callback is used by C# code editors to modify the .csproj files. This function is undocumented").

    The purpose of the csproj is to inform the compiler options, thus having the correct xml tags in the csproj *should* result in the assembly having embedded data. For whatever reason, this is not the case. Unity is doing *something* that breaks that process.
     
  9. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Currently, in Unity the purpose of csproj files are to inform the IDEs about your project (for code completion and highlight and whatnot to work properly). AFAIK the compilation won't use them in the form they are on the disk.

    I'm not the most well-versed in the build process though. I'll summon (if he doesn't mind it) @JoshPeterson, who is on the forums more from the actual Unity team and he has far more knowledge about these things, he just more is in another topic of the forum.
     
    Last edited: Oct 5, 2023
    CelesteMarina likes this.