• 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 /
avatar image
Question by ivaylo5ev · Aug 27, 2017 at 02:29 PM · editorprefabeditor-scriptingassetdatabaseloadassetatpath

Could not load copied asset via AssetDatabase.LoadAssetAtPath

I am using AssetDatabase.CopyAsset method to create a copy of a prefab as part of an AssetPostProcessor implementation:

 AssetDatabase.CopyAsset(sourcePrefabLocation, targetPrefabLocation);
 AssetDatabase.ImportAsset(
     targetPrefabLocation, 
     ImportAssetOptions.DontDownloadFromCacheServer|ImportAssetOptions.ForceUpdate);
 AssetDatabase.SaveAssets();

T$$anonymous$$s works fine, the asset is copied. However, later in the same post-processor, I want to load the copied asset, so I am using:

 GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(targetPrefabLocation);

Unfortunately, the prefab variable is always null. I used the following code to list all prefab assets, just to find out that the copied prefab is not enlisted. That would actually explain why the LoadAssetAtPath fails to load the asset.

 AssetDatabase.GetAllAssetPaths().Where(x => x.Contains(".prefab")).Aggregate(new StringBuilder().AppendLine(), (sb, s) => sb.AppendLine(s))

So, what should I do to make the AssetDatabase include the copied asset?

Comment

People who like this

0 Show 0
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

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Unity-Wenchao · Oct 30, 2017 at 08:30 AM

@ivaylo5ev

I found a solution to t$$anonymous$$s problem: string oldpath = {XXXX}; string newpath = {XXX}; AssetDatabase.CopyAsset(oldpath, newpath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); AssetDatabase.ImportAsset(newpath); AssetDatabase.LoadXXX // It can get the right asset now

Comment
Bunny83
ivaylo5ev
dbdenny

People who like this

3 Show 2 · 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
avatar image ivaylo5ev · Feb 06, 2018 at 06:50 PM 0
Share

Thank you for providing an answer. I have used a similar approach, so I assume yours works as well. Anyway, I have moved on to using the experimental asset importers that came with Unity 2017. They too have their quirks, but are closer to what I initially wanted to achieve.

avatar image dbdenny · Nov 02, 2020 at 04:06 AM 0
Share

Thx so much, save my life!!!

avatar image

Answer by ArturoSR · Aug 31, 2017 at 06:22 PM

@ivaylo5ev Hello there, probably you need to "Refresh" the project so the new asset shows up, there is a method to do that, cheers.

Comment
Bunny83

People who like this

1 Show 1 · 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
avatar image ivaylo5ev · Sep 02, 2017 at 03:00 PM 0
Share

Hello @ArturoSR. I am doing this inside a script, which is an AssetPostProcessor. I create the asset in the post-processor and I want to load it within the same execution flow. I tried calling AssetDatabase.Refresh before my AssetDatabase.LoadAssetAtPath but to no avail. So the flow is:

  • AssetDatabase.CopyAsset

  • AssetDatabase.ImportAsset

  • AssetDatabase.SaveAssets

  • AssetDatabase.Refresh

  • AssetDatabase.LoadAssetAtPath

Am I missing something?

avatar image

Answer by x4637x · Oct 30, 2017 at 08:54 AM

Had the same problem once. Then I changed to used GameObject prefab = AssetDatabase.LoadAssetAtPath(targetPrefabLocation,typeof(GameObject)) as GameObject; and it works for me.

Comment

People who like this

0 Show 2 · 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
avatar image Bunny83 · Oct 30, 2017 at 09:51 AM 0
Share

This certainly doesn't solve anything. The generic version he's using just calls this method:

 public static T LoadAssetAtPath<T>(string assetPath) where T : Object
 {
     return (T)((object)AssetDatabase.LoadAssetAtPath(assetPath, typeof(T)));
 }

If any of the two methods is more reliable it's the generic one as it doesn't use an as cast which could silently fail.

avatar image x4637x Bunny83 · Oct 30, 2017 at 10:09 AM 0
Share

I just quickly check my old code again, noticed you are right. Another difference though is I am using where T : ScriptableObjectinstead of Object. Will it be possible that this is a deserialized problem? Also, need to mention I am not calling the AssetDatabase.Refresh()anywhere in my code.

avatar image

Answer by SpookyCat · Dec 13, 2017 at 05:47 PM

Did you find a solution for t$$anonymous$$s, I have the same problem, I copy texture files into the project but it seems Unity waits till after the OnPostProcessAllAssets completes before it actually does import the textures into the asset database. Even using Refresh etc. makes no difference, I can't see a way to force Unity to import the textures now and update the database so I can make changes to them.

Comment

People who like this

0 Show 1 · 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
avatar image ivaylo5ev · Feb 06, 2018 at 06:52 PM 0
Share

Hello @SpookyCat. I did not find anything appealing - I got this working by a similar to @Unity-Wenchao' s solution for a short time. Anyway, I have moved on to using the experimental asset importers that came with Unity 2017. They too have their quirks, but are way closer to what I initially wanted to achieve.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

90 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

Related Questions

Load an asset right as it is imported? 0 Answers

How to create custom asset icons for ScriptableObject instances 2 Answers

Change material with EditorScript 3 Answers

How to execute a script each time I add a certain prefab to the scene? 2 Answers

reconnect programmatically instantiated prefab 1 Answer


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