Search Unity

Unity touches files that are not modified with explicit intent

Discussion in 'Editor & General Support' started by Qbit86, Mar 21, 2017.

  1. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    It's hard to work with version control system like Git, because Unity constantly modifies random files here and there, provoking garbage in diffs. For example, I edit some script, and then some material is listed as unstaged. The line `second: {r:0, g: 0, b: 0, a: 0}` is changed to `second: {r: 1. g: 0.99999994, b: 0, a: 1}`. Every time different assets are touched: prefabs, scenes, materials, metas, etc.

    Why? How to make Unity stop silently modify random files in background for no reason?

    P.S. Authentication to forum is still broken. Please fix it.
     
    Xarbrough likes this.
  2. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Please take a look at this screenshot from SourceTree (popular Git client):

    Do you know what `OutlineMaterial.mat` is? Neither do I. Because it's some stuff from our artists, no one interested in, probably except the artists themselves. Why this material is spontaneously changed after running the game in Editor or modifying some assets? How to prevent Unity from making these arbitrary changes to files I never manually edit nor even bother about their existence?
     
    lkj_dpc likes this.
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    There's a bunch of way this happens.

    One is for version upgrades. You upgrade Unity's version, they change how stuff is serialized on the back-end, and BAM, no every single time you touch an asset, it changes it's representation. This was very noticable going from 5.4 to 5.5, where every line of text in a .prefab or .unity (scene) that represented a Component attached to a GameObject changed.

    A different way is the UI. The way unity's UI system works is that the different layout components sets the values of the rect transform components in their children. Which means that if you open a scene on a different resolution than the last time, lucky you, all of the rect transforms show up changed in the git log.

    Finally it's what you have - the same value changing on a single object. That usually means that some script is changing the object at edit time - either because of ExecuteInEditMode or something. It's really, really hard to track down why the value is changing, since there's no way to attach logging or a debugger. the best bet is probably to delete the material, run the game, and see what nullrefs.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,406
    I've noticed that for me it happens when
    - modify material settings (color) using script in playmode (not in any editor scripts)
    - then when you hit save scene after stopped playmode, .mat in project folder appears as changed..

    *that material is referenced in script field from project folder, skybox mat in my case..
     
    Qbit86 likes this.
  5. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    C# can't store exact values of certain floating point numbers, because of how C# stores them, so X.0f often gets turned into values like you saw 0.99999994f. So you'll see the value change to that. And yeah, if you change a material in code while running in the editor, that will touch the original material even though it doesn't change it permanently.
     
  6. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Thanks; I'm aware of floating point representation. But Editor's behavior is not consistent even in this sense.

    Sounds like a bug: it's both unexpected and undesired. This change is useless, discarding doesn't breaks anything.
     
  7. JHibbins

    JHibbins

    Joined:
    Apr 25, 2014
    Posts:
    9
    We too find this unexpected and undesired, saving runtime colour change values (which is common at runtime) seems totally wrong when the core prefab has not changed outside of runtime.
     
  8. rajiv-c

    rajiv-c

    Joined:
    Aug 4, 2015
    Posts:
    2
    I am also facing this exact issues. not able to resolve it yet. any solution?
     
    Qbit86 likes this.
  9. GusViegas

    GusViegas

    Joined:
    Aug 7, 2017
    Posts:
    3
    One year later, still have the same exact problem happening :\
     
    umbrella-crash and Qbit86 like this.
  10. io-games

    io-games

    Joined:
    Jun 2, 2016
    Posts:
    104
    One year later... same issue
     
  11. abeldantas

    abeldantas

    Joined:
    Jul 28, 2013
    Posts:
    19
    This is very annoying.

    One solution is not using .asset files and serializing/deserializing everything from json.
     
  12. Nope, that's a very bad "solution".

    If it's a code asset, then [NonSerialized], if it's a material, then probably the [HideInInspector] works, although I'm not sure about it and right now I'm not in the front of my Unity-dev computer.
    If it's something else, write a destructor and write the dynamic parameters over with a predefined data on exit play mode. So you always will have a defined data in those parameters.
     
  13. victorhugolafuente

    victorhugolafuente

    Joined:
    Dec 22, 2017
    Posts:
    6
    One thing you could do is use Sublime Merge and use Stage Lines or Stage Hunk in conjunction with Discard Hunk or Discard Lines, that prove somewhat productive for me.
     
  14. dapken

    dapken

    Joined:
    Oct 21, 2017
    Posts:
    2
    For preventing materials assets being changed during runtime you could try this solution:
    https://answers.unity.com/questions/487908/modifying-material-modifies-the-actual-mat-file.html

    Maybe with the help of a script file that is applied to the gameobjects in question. Something like:

    Code (CSharp):
    1.     private void Start()
    2.     {
    3. #if UNITY_EDITOR
    4.         Renderer r = GetComponent<Renderer>();
    5.         r.sharedMaterial = new Material(r.sharedMaterial);
    6. #endif
    7.     }
    Cheers and good luck
     
  15. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    I found that by adding .gitattribute, these changes that are generated by Unity will be gone. We can find some examples of .gitattribute for Unity on GitHub.
     
  16. io-games

    io-games

    Joined:
    Jun 2, 2016
    Posts:
    104
    can you share gitattribute file?
     
  17. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    Last edited: Jul 6, 2020
    diesoftgames likes this.
  18. io-games

    io-games

    Joined:
    Jun 2, 2016
    Posts:
    104
    thx, do you know how it technically works? I mean why it solves issue with numbers
     
  19. better_walk_away

    better_walk_away

    Joined:
    Jul 12, 2016
    Posts:
    291
    .gitattribute is a file that is used by Git LFS, Git LFS is a tool that deals with large files and binary files in the version control. By default, the version control system treats binary files as if they are normal text files, which is very inefficient.
    The noises that we got are mostly binary files. I don't know the actual technical detail, I got this workaround through trial and error. I guess maybe it is because Git isn't very good at handling binary, and Git LFS is designed to cope with large files and binary files specifically.
     
    io-games likes this.
  20. olejuer

    olejuer

    Joined:
    Dec 1, 2014
    Posts:
    211
    I found a similar issue in caused by the UI, specifically BaseShaderGUI which changes the color values of _SpecColor in the last digit every time the material is inspected in the editor. I sent a bug report for this, as it messes up version control.

    Confirmed in Unity 2020.1.17f1 and Unity 2021.1.7f1