Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
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();

This 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
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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

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

@ivaylo5ev

I found a solution to this 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
Add comment · 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
1

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
Add comment · 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 @$$anonymous$$oSR. 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
0

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
Add comment · 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 : ScriptableObjectins$$anonymous$$d 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
0

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

Did you find a solution for this, 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
Add comment · 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.

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

Mark gameobject field as changed from prefab 2 Answers

Editing and saving assets 0 Answers

Automatically update prefab instance 1 Answer

Display changes in the editor made to a prefab 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