Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem with FileUtil.MoveFileOrDirectory and material

Discussion in 'Scripting' started by mrtkhosravi, Sep 24, 2017.

  1. mrtkhosravi

    mrtkhosravi

    Joined:
    Nov 9, 2014
    Posts:
    198
    I want to move assets from a folder to another through editor script. Consider this sample code:

    Code (CSharp):
    1. FileUtil.MoveFileOrDirectory("Assets/ZZZ1", "Assets/ZZZ2");
    2. AssetDatabase.Refresh();
    It renames the folder ZZZ1 to ZZZ2. It normally has no problem but if the folder contains a material, the editor console shows an exception:

     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    This seems to be related more specifically to that custom editor code that goes along with that terrain shader addon you're using. It seems to be doing something that is locking access to the material in-between processing, likely some sort of onvalidate function. Try it with just a regular Standard shader material and see if it still happens.
     
    mrtkhosravi likes this.
  3. mrtkhosravi

    mrtkhosravi

    Joined:
    Nov 9, 2014
    Posts:
    198
    Thanks for the answer. I did it with a new material and happened again. Only materials have this problem. Shaders and textures move just fine. Even materials are moved. The problem is it outputs an error in console and clears material properties. The InstanceId of the material remains the same. I know nothing is going on with the terrain extension because I created it. Anyone could test these two lines in a clear project and the result would be the same. It seems like a bug to me. The version is 5.4.2.f1.
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Yeah, confirmed the issue on 2017.1.1p3. I can send in an error report project properly setup to reproduce it if you haven't done so yet.
     
    mrtkhosravi likes this.
  5. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Found a solution to the issue, and Unity actually gave a warning in a fresh project that tipped me off to it. When moving a folder that has assets in it, you need to move/rename the .meta file as well, so your code should look like:
    Code (CSharp):
    1. FileUtil.MoveFileOrDirectory("Assets/ZZZ1", "Assets/ZZZ2");
    2. FileUtil.MoveFileOrDirectory("Assets/ZZZ1.meta", "Assets/ZZZ2.meta");
    3. AssetDatabase.Refresh();
    Ideally you could make a wrapping method for this that does that for you automatically.
     
  6. mrtkhosravi

    mrtkhosravi

    Joined:
    Nov 9, 2014
    Posts:
    198
    Thanks for testing it. That would be very nice if you send a report for it.

    Oh Thank you very much. Yes that's a great idea.
     
  7. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    After further testing, this isn't actually a bug really, it's just improper file moving. Using FileUtil is like renaming a folder outside of Unity, and if you do that without renaming the .meta file as well, that can screw things up in general.
    What you actually want to use is "AssetDatabase.MoveAsset(from, to);'. Even though it says "asset", it actually works on folders as well, and will throw no errors when used the first way you intended.
     
    nirvanajie and mrtkhosravi like this.
  8. mrtkhosravi

    mrtkhosravi

    Joined:
    Nov 9, 2014
    Posts:
    198
    This is fantastic. Before FileUtil I used MoveAsset to move each file individually. It did move the files but I could not remove the original folder. It gave me every kind of headache because of the async nature of MoveAssets. Moving folders with MoveAsset is working perfect and unity documentation fails to mention that. Nice catch and thank you very much for finding such a painless solution.
     
  9. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    mrtkhosravi likes this.
  10. mrtkhosravi

    mrtkhosravi

    Joined:
    Nov 9, 2014
    Posts:
    198