Search Unity

Saving string as .anim

Discussion in 'Animation' started by adehm, Jun 16, 2021.

  1. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    I need to change bones for animations. I can manually open the .anim file in notepad++ and change the bones then save it and it works. I built a class to handle these name changes for me but I cannot figure out how to save it as a .anim for use. Anyone know how to save a string as FileName.anim?
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    System.IO.File.WriteAllText.

    I also made a tool to Remap Animation Bindings using a proper GUI. Buying Animancer Pro isn't really worth it for just that tool, but you might be interested in using it as your animation system.
     
    adehm likes this.
  3. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    That did the trick, thanks.
     
  4. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    When using StreamWriter I would get an error.
    Unfortunately with WriteAllText I get no error but even when making no edits I lose all properties at 'root : Scale' and below.
     
  5. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Have you disposed the StreamReader you used to load the file?

    I'm not sure what would cause you to lose data. My system modifies the actual animation data within Unity rather than parsing it as text.
     
  6. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Maybe that is my problem; I thought I could just modify the text because that is all I did when manually changing animation names in notepad++ and that test being successful. But the act of opening and saving is causing data loss somehow.

    Code (CSharp):
    1.      
    2. string path = "Assets/Development/Characters/Types/Animations/Stance/Stance_Idle.anim";
    3. StreamReader reader = new StreamReader(path);
    4. string data = reader.ReadToEnd();
    5. reader.Close();
    6. File.WriteAllText("Assets/Testing.anim", data);
    7. Debug.Log("Complete");
    8.  
    I'll give a look at your source code to try it a different way but be a shame as I spent an entire day working around all the inconsistencys to get the names replaced correctly.


    Update:

    I deleted the old Testing.anim and then it fixed the properties issue when creating it. So maybe I just can't trust it to be overwritten properly. I think I just need it to reimport, maybe using AssetDatabase.ForceReserializeAssets, but can't seem to figure out how to target a single asset as documentation sounds like it should be 'AssetDatabase.ForceReserializeAssets(path)' but I'm missing something.
     
    Last edited: Jun 17, 2021
  7. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    StreamReaders are mostly useful if you want to read things line by line or character by character to avoid needing to allocate memory for the whole thing then chop it up repeatedly. But if you aren't doing that then you might as well use File.ReadAllText instead.

    Also try calling AssetDatabase.Refresh to see if that lets Unity detect it was modified properly. If not, try AssetDatabase.ImportAsset.
     
  8. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Got it with
    Code (CSharp):
    1.   AssetDatabase.ImportAsset("Assets/Testing.anim");