• RuntimesUnity
  • Can materials and animator controllers be moved to its own folders in Unity

Hi, I work on a project in Unity that is shared between multiple users, I have a simple spine animate asset and due to the project structure, we follow a
MainAsset

  • Materials
    -Textures
    -Controllers
    -Prefabs
    format. When I follow a similar format, a second user, on a different machine gets new material assets generated because of the assetpostprocessor. Is there any way to stop this regeneration if an existing material exists?

Sorry if this is a dupe. Thank you.

  • Harald ответили на это сообщение.
    Related Discussions
    ...

    To prevent the regeneration of material assets in Unity when using Spine, you can implement a custom AssetPostprocessor to handle the assets as needed. This approach allows you to control the behavior of asset processing and avoid the automatic generation of new materials if they already exist.

    Here's a basic example of how you can create a custom AssetPostprocessor:

    using UnityEditor;
    using UnityEngine;
    
    public class CustomSpineAssetPostprocessor : AssetPostprocessor
    {
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            foreach (string asset in importedAssets)
            {
                if (asset.EndsWith(".mat"))
                {
                    // Check if the material already exists in the desired folder
                    string targetPath = "Assets/YourTargetFolder/" + System.IO.Path.GetFileName(asset);
                    if (System.IO.File.Exists(targetPath))
                    {
                        // If the material exists, move it to the desired folder
                        AssetDatabase.MoveAsset(asset, targetPath);
                    }
                    else
                    {
                        // If the material does not exist, you can handle it accordingly
                        // For example, you can delete the newly generated material
                        AssetDatabase.DeleteAsset(asset);
                    }
                }
            }
        }
    }

    In this script:

    • The OnPostprocessAllAssets method is overridden to handle the post-processing of assets.
    • It checks if the imported asset is a material (.mat file).
    • If the material already exists in the target folder, it moves the new material to the desired folder.
    • If the material does not exist, it deletes the newly generated material to prevent duplication.

    This way, you can ensure that materials are not regenerated unnecessarily and are organized according to your project's structure.

    For more detailed control, you can expand this script to handle other asset types like textures and animator controllers similarly.

    belovedquestioningly When I follow a similar format, a second user, on a different machine gets new material assets generated because of the assetpostprocessor. Is there any way to stop this regeneration if an existing material exists?

    While the description by Spinebot is valid, this should not be necessary at all.

    Are you sure that the assets have been added to version control? If the Materials already exist or settings have been modified, they will not be overwritten.

    Which exact version of the spine-unity runtime are you using (name of the unitypackage, also listed in Assets/Spine/version.txt, or version listed in the Unity Package Manager)?


    Hi Harold,
    My version is 4.2-2024-06-17
    Is there anything I need to set in the Spine Preferences within the Preferences?

    • Harald ответили на это сообщение.

      belovedquestioningly Is there anything I need to set in the Spine Preferences within the Preferences?

      No.

      My previous question remains:

      Are you sure that the assets have been added to version control? If the Materials already exist or settings have been modified, they will not be overwritten.

      Yes materials have been added to version control.

      @belovedquestioningly Thanks for the info, this is strange then. Could you perhaps share some screenshots or even better a short video showing how new materials are generated?

      Alternatively, if you can create a minimal Unity reproduction project with a description of reproduction steps which make this issue occur? You can send it as a zip package to contact@esotericsoftware.com, briefly mentioning this forum thread URL so that we know the context.

      Hi,

      I have created a small package that I have sent as email. This is a simple folder setup I did using the example spine file available here: https://en.esotericsoftware.com/spine-examples-spineboy
      I have packed it as an atlas.txt and exported as skel.bytes(The same as our project). I have noticed that the problem occurs if anything in the project causes a refresh to trigger the assetimporter, for example if there is an editor script that was deleted and unity was launched, or another example, if the spine assets were not first present but when a second user gets latest and the assets are now present in the project, unity triggers the asset post processor that deletes the original material file and creates a new file in the SpineData folder.

      I have tracked it down to these few lines in the AssetUtility lines 664 - 674
      `Material material = (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material));

      			if (material == null) {
      				Shader defaultShader = GetDefaultShader();
      				material = defaultShader != null ? new Material(defaultShader) : null;
      				if (material) {
      					ApplyPMAOrStraightAlphaSettings(material, SpineEditorUtilities.Preferences.textureSettingsReference);
      					if (texture != null)
      						material.mainTexture = texture;
      					AssetDatabase.CreateAsset(material, materialPath);
      				}`

      Line 668 returns null as the material is now not present in the path formatted but instead moved to a different location, hence a new material is generated(I think this is what is happening).

      I also see this same issue when I import the package I have created for this post, as you can see when the import window comes in there is a material in the material folder

      but after the import is done, the folder is empty.

      This is an empty project that has spine installed. No custom scripts or any other file.

      • Изменено

      @belovedquestioningly Thanks for sending the reproduction project, we could reproduce the issue.

      The problem seems to only occur when the Material asset is moved to a separate directory, where the Spine importer does not recognize it as associated with the skeleton.

      We've created an issue ticket for it here:
      EsotericSoftware/spine-runtimes2560
      We will have a look at it and get back to you here on the forum once we have a solution to offer.

      The simplest solution until this can be resolved properly would be to not move the Material to a separate directory, or at least moving the atlas assets name.atlas.txt and name_Atlas.asset to the same directory.

        @belovedquestioningly We have just released a fix for the issue on the 4.2 branch, moved Materials should now be recognized upon (re-) import and no longer deleted and re-created.

        A new spine-unity 4.2 unitypackage is available for download:
        https://esotericsoftware.com/spine-unity-download
        Please let us know if this resolves the issue on your end as well. Thanks for reporting!

        Harald Sounds good, will update you soon.

        Hi Harald,
        Materials no longer are regenerated but

        Which when connected to a version system, (we used P4V), it marks the files _Atlas.asset and _Material.mat as modified even though there are no visible changes. Is there a way to avoid this? Thanks.

        @belovedquestioningly Unfortunately I could not reproduce this issue. The steps I've taken are:
        1) imported your MaterialsControllersIssue.unitypackage, let the materials and assets be generated.
        2) add the top-directory of the assets to git.
        3) deleted the directory locally, switch to Unity to let it update it's DB.
        4) restored the directory to it's previous state again via git checkout .
        5) switched to Unity to let it import the assets once more.
        6) observe that git status shows no changes after the same assets were imported a second time.

        What are the changes you see, can you share a diff? In general please always describe everything with as much info as possible.

        • Harald ответили на это сообщение.

          The steps on my end is

          1. Submit the assets to a server;
            with all the material issues, for now I have left them in the same folder as spinedata for testing:

            p.s. I have tried it even with materials moved to material folder
          2. A second user, when the user who did not have this folder before, gets latest, on opening unity, the files are being checked out even though there are no changes or modifications by user (this happens as soon as unity opens)
            Screenshots from second user:


          • Harald ответили на это сообщение.

            belovedquestioningly Submit the assets to a server;

            Please always describe any steps as precisely as possible. "the assets" is very vague, did you add all assets of the folder to version control? "submit to a server" is very vague as well. What version control software you're using, how did you submit the assets?

            I have to repeat the question of my previous posting, since you haven't answered it:

            Harald What are the changes you see, can you share a diff? In general please always describe everything with as much info as possible.

            месяц спустя

            Hi Harald,

            Apologies for my late response, I did not have access to my machine for some time. Answering your questions, by assets I meant the files in the picture attached which is the
            All three files generated by spine export
            .atlas.txt
            .png
            .skel.bytes

            Files generated in unity
            the material file generate
            skeletondata asset
            animatorcontroller

            Can you describe what "What are the changes you see, can you share a diff?". Would you like to see the diff for these three files?

            • Harald ответили на это сообщение.

              belovedquestioningly Can you describe what "What are the changes you see, can you share a diff?". Would you like to see the diff for these three files?

              Yes. It would be interesting to see a diff of the files that you find problematic that they changed. You posted the following text earlier:

              Which when connected to a version system, (we used P4V), it marks the files _Atlas.asset and _Material.mat as modified even though there are no visible changes. Is there a way to avoid this? Thanks.

              Since there are no "visible changes" that you see, it is necessary to see a diff of these files.

              In Unity what I see,

              Diff in perforce looks like the two assets are unmodified, if I choose ignore line endings:

              If I chose recognize line endings and whitespace:

              • Harald ответили на это сообщение.

                belovedquestioningly Thanks for the additional info. Then it's highly likely that you've configured your version control system to always checkout files with a different line ending (e.g. lf instead of crlf) than the one that Unity produces when writing asset files on your machine. So when you checkout your files, you receive one line ending and when Unity just touches the file it's of a different line ending. You thus need to make sure both line ending styles match to avoid undesired diffs.