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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

is there any way to prevent unity updating *. csproj files in Unity 2020?

Discussion in 'Scripting' started by Gold_Rush, Nov 28, 2020.

  1. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    Is there any way to prevent unity updating *. csproj files?

    For example im modify *.csproj files in my project. And when im open any script, Unity automatically update *.csproj files and broke my modifications.

    How can I forbid Unity doing this?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Try looking under Preferences -> External Tools

    But note that *.csproj files are purely for the benefit of your code editor; Unity does not use them to track files or to make builds.
     
    Joe-Censored and Bunny83 like this.
  3. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    Hello!

    I looked at the settings, there is only one check mark:
    Generate all .csproj files.
    https://docs.unity3d.com/2020.1/Documentation/Manual/Preferences.html

    If I change *. csproj in Notepad. And after i open one of the *.cs scripts from Assets, Unity will automatically overwrite *. csproj

    I'm sure Unity tracks *. csproj files and write paths for self includes.
     
    Last edited: Nov 28, 2020
  4. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    Perhaps...

    Unity\Editor\Data\Tools\Roslyn\VBCSCompiler.exe do that.

    or/and

    Unity\Editor\Data\Tools\Roslyn\csc.exe do that.
     
    Last edited: Nov 28, 2020
  5. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    Checked with FileSystemWathcer thats true, overwriting occurs when opening a project and when opening * .cs scripts
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,539
    Yes, but that's still irrelevant. Unity does not use the sln or the csproj files when it actually compiles the scripts. Unity just generates those files for editing purposes. You should not change anything in those files as it will have no effect. If you want to specify custom compiler options or add external assembly references, you need to use an mcs.rcp file as descibed here. or it's mentioned in the documentation here at the bottom.
     
    Joe-Censored likes this.
  7. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    Hello!

    In principle, it is not difficult for me to intercept these interventions and automatically fix *. csproj. But maybe it can be somehow disabled using UnittPrefs?

    https://docs.unity3d.com/ScriptReference/EditorPrefs.html

    I'm open regedit and look HKEY_CURRENT_USER\Software\Unity Technologies\Unity Editor 5.x

    Maybe who know what do kAutoRefresh = 1 flag?
     
  8. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    Why do you want to modify *.csproj files by yourself? What do you want to achieve? What do you change when editing *.csproj files in Notepad?
     
  9. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    For compatibility with one editor that I'm used to. For comfortable work.
     
  10. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    got it
    you can implement AssetPostprocess and add static OnGeneratedCSProject method to it.
    Unity will call it each time csproj file was regenerated.

    Code (CSharp):
    1. public class CSProjPostprocessor : AssetPostprocessor
    2. {
    3.     private static string OnGeneratedCSProject(string path, string contents)
    4. {
    5. //do something with content and return new content
    6. }
    7. }
     
    Last edited: Dec 1, 2020
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,539
    And again something that slipped by ^^. When did they add this callback? I remember the time when the AssetPostProcessor had exactly 4 callbacks ^^

    The documentation doesn't seem to include the callback you mentioned. May I ask where have you found that information ? Have you found it in the reference assembly?
     
  12. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    Last edited: Dec 2, 2020
  13. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    Thanks that interest method.
     
    Last edited: Dec 2, 2020
  14. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    But this no results in Unty 2020.1.10

    Code (CSharp):
    1.  
    2. public class CSProjPostprocessor : AssetPostprocessor
    3. {
    4.     private static string OnGeneratedCSProject(string path, string content)
    5.     {
    6.         Debug.Log ("!!!" + path + "\r\n" + content);
    7.         return content;
    8.     }
    9. }
    10.  
    Im try to rename content into contents... no results too.
     
  15. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,539
    I just copied your example into my test project (Unity 2019.2.14f1) and I actually get two calls of the post processor when I add, remove or rename a script in Unity since that will cause a rebuild of the project files. I get one call for
    Assembly-CSharp.csproj
    and one call for
    Assembly-CSharp-Editor.csproj
    .

    Note that just editing an existing script does not cause a regeneration of the project files since the number of files or their locations do not change. That's what the project files are about.

    ps: the names of your method parameters are irrelevant as they are not part of a method signature. Only the parameter types are relevant.
     
    Last edited: Dec 2, 2020
  16. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    I'm thik this feature turn off in feature relases...
     
  17. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    Just tried in Unity 2020.1.11 and it prints a debug message when csproj generated for the first time or I add/remove some script.
     
    Gold_Rush and Bunny83 like this.
  18. Gold_Rush

    Gold_Rush

    Joined:
    Jan 10, 2018
    Posts:
    64
    I will try again in 2020.1.15 all work fine!
    Perhaps there was some kind of failure.