Search Unity

Why are Unity path separators so inconsistent? Am I doing something wrong?

Discussion in 'General Discussion' started by unity_mxTWsrmALoWtFQ, Jun 19, 2022.

Thread Status:
Not open for further replies.
  1. unity_mxTWsrmALoWtFQ

    unity_mxTWsrmALoWtFQ

    Joined:
    Aug 25, 2018
    Posts:
    11
    While trying to solve another unrelated Problem, I stumbled over a very weird behavior in Unity: Different functions use (and expect) completely different path separators (either "/" or "\") and it all doesn't seem to be consistent.

    Not sure if this fits the general discussion, but I don't think this would be a bug report either, since it seems to be intended behavior?

    For example:
    Code (CSharp):
    1. Application.dataPath;
    2. // Returns "C:/Projects/Pathtest/Assets", using "/"
    3.  
    4. Path.GetDirectoryName( Application.dataPath );
    5. // Returns "C:\Projects\Pathtest", using "\", flipping all "/" to "\"
    6.  
    7. AssetDatabase.GetAssetPath( object );
    8. // Returns "Assets/Test/test.anim", using "/"
    9.  
    10. Path.Combine( Application.dataPath, AssetDatabase.GetAssetPath( object ) );
    11. // Returns "C:/Projects/Pathtest/Assets\Assets/Test/test.anim" (using "\", and not flipping the "/")
    What gives? Every other function seems to just flip flop between "/" and "\" - am I doing something wrong, or missing a certain configuration or setting to normalize this? This shouldn't even happen - is this just some underlying mess that hasn't been cleaned up yet in Unity?

    I haven't tested any further than that, but I would expect the separators returned by functions to at least be consistent across the board.

    OS: Windows 10, Unity: 2021.3.1f1.32
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    Application.dataPath and AssetDatabase.GetAssetPath are implemented by unity. AssetPath, technically is not necessarily a filename.

    Path.GetDirectoryName, however, is implemented by Mono and is dependent on operating system. Same goes with Path.Combine. They use "Path.PathSeparator" to join paths together.

    That's why they return different results.

    Personally, I'd just use forward slashes everywhere ( "/")
     
  3. unity_mxTWsrmALoWtFQ

    unity_mxTWsrmALoWtFQ

    Joined:
    Aug 25, 2018
    Posts:
    11
    You're right, that must be it. I guess this is why I prefer to avoid working on Windows, I usually stick to linux and don't have to worry about this specific problem :) Its a shame, it doesn't seem to be configurable... I guess I'll just extend Path then.

    Excuse my inexperience with C#.
     
  4. I work on Windows and I have never ever worried about these things. Just use the built-in paths and functions like you listed in your OP and the rest and you'll be fine without problems. Those only appear when people think they are smarter than everyone else and they start to handle these things themselves.
     
    DragonCoder likes this.
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    "Linux" separators work fine on windows (at least since windows 7), it is just any path combining function insists on trying to insert backslashes at every opportunity.
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,981
    To clean up a path it helps to run Path.GetFullPath(path) in case mixed slashes bother you or some other system.
     
    AqueuseAkaLLO likes this.
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Unity's development environment was originally written for Apple Mac computers, so I would guess that all of their early stuff used OS X's path conventions. And when the editors for Windows and later Linux appeared I assume anything passed from the OS ends up with local conventions.

    That said, I don't recall the difference between forward/back slashes being a concern often, if at all. Unity and Mono have both been cross platform from the start, having to operate agnosticly between systems with different path conventions. Are there cases where this causes issues?

    (I've possibly had one case, but I can't remember well enough to say for sure.)
     
  8. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    backslash is a valid filename character on unix-likes. So if you manage to put a backslash into a filename, working with that on windows will be a ton of fun.

    Speaking of which, unix-likes are also case-sensitive, and I've already seen a MacOS project that has "Scenes" and "scenes" in the same directory. Again, lots of fun if you check it out on windows machine, because those get merged.
     
Thread Status:
Not open for further replies.