Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Karim-Largelabs · Sep 14, 2020 at 03:59 PM · scriptableobjecttileassetdatabaseworkflowassetpostprocessor

LoadAssetAtPath doesn't seem to work for ScriptableObject asset in AssetPostProcessor

Hello there !

I have been trying to implement a very simple AssetPostProcessor to force the collider type on Tile assets during import, depending on the path of the asset. We are starting to have hundreds of tiles and something like this would really help.

When I reimport my tile asset, the asset path is logged successfully but LoadAssetAtPath always returns null. I've been looking for solutions and alternatives for a while now but I really can't figure out what's is wrong in this code. Any tips ?

Edit : I'm on Unity 2019.4.10f1, this script is in an Editor assembly. I also tried LoadMainAssetAtPath and tried to log values in the assetImporter (name, userData are all empty...)

Thanks a lot

 class TileAssetImporter : AssetPostprocessor
     {
         void OnPreprocessAsset()
         {
             if (assetPath.Contains("Collider_"))
             {
                 Tile tile = AssetDatabase.LoadAssetAtPath<Tile>(assetPath);
 
                 Debug.Log(assetPath);
 
                 if (null != tile)
                 {
                     if (assetPath.Contains("None"))
                     {
                         tile.colliderType = Tile.ColliderType.None;
                     }
                     else if (assetPath.Contains("Sprite"))
                     {
                         tile.colliderType = Tile.ColliderType.Sprite;
                     }
                     else
                     {
                         tile.colliderType = Tile.ColliderType.Grid;
                     }
 
                     EditorUtility.SetDirty(tile);
                     AssetDatabase.SaveAssets();
                 }
                 else
                 {
                     Debug.LogError("Could not load Tile asset");
                 }
             }
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Karim-Largelabs · Sep 20, 2020 at 03:59 PM

Update for those who my be facing a similar issue : I wasn't able to make it work using OnPreProcessAsset. I suspect a regression because it used to work in Unity 2018.3 (I'm now using 2019.4.10f1)

However, I can load my ScriptableObject (here, a Tile) during post process and do what I want that way. I don't consider this to be the best solution, it looks quite unelegant and I still don't understand what was going on with the previous code, but hey, it works now. The dirty flags are here so that I won't enter an infinite loop, since AssetDatabase.SaveAssets() will end up reimporting and this code will therefore be called again. Source code :

     class TileAssetImporter : AssetPostprocessor
     {
         static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
         {
             bool hasDirtyAssets = false;
 
             foreach (string path in importedAssets)
             {
                 if (path.Contains("Collider_"))
                 {
                     Tile tile = AssetDatabase.LoadAssetAtPath<Tile>(path);
 
                     bool isDirty = false;
 
                     if (null != tile)
                     {
                         if (path.Contains("None"))
                         {
                             if(tile.colliderType != Tile.ColliderType.None)
                             {
                                 tile.colliderType = Tile.ColliderType.None;
                                 isDirty = true;
                             }
                         }
                         else if (path.Contains("Sprite"))
                         {
                             if (tile.colliderType != Tile.ColliderType.Sprite)
                             {
                                 tile.colliderType = Tile.ColliderType.Sprite;
                                 isDirty = true;
                             }
                         }
                         else
                         {
                             if (tile.colliderType != Tile.ColliderType.Grid)
                             {
                                 tile.colliderType = Tile.ColliderType.Grid;
                                 isDirty = true;
                             }
                         }
 
                         if(true == isDirty)
                         {
                             hasDirtyAssets = true;
                             Debug.Log("TILE IMPORTER - changed tile collider to " + tile.colliderType.ToString() + " for " + path);
                             EditorUtility.SetDirty(tile);
                         }
                     }
                 }
             }
 
             if(true == hasDirtyAssets)
             {
                 AssetDatabase.SaveAssets();
             }
         }
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

213 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get asset file from scriptableobject (Nested Scriptableobjects) 2 Answers

How can I get a sub asset? 1 Answer

ScriptableObject Instances Referencing One Set of Prefabs - Changing One SO Changes The same prefab 1 Answer

AssetDatabase.LoadAssetAtPath Not finding assets 0 Answers

How to "Lock" scriptable objects from losing their [SerializeField] values 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges