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 /
avatar image
4
Question by Ryan Scott · Aug 13, 2010 at 11:23 PM · assetsassetdatabasereplace

AssetDatabase: Replacing an asset, but leaving reference intact

I am creating a mesh via a custom import script. After generating the mesh I am using AssetDatabes.CreateAsset() to write that to disk as a .asset file. All of that works just fine.

However, when this process runs again, CreateAsset() seems to explicitly delete the previous asset prior to putting the newly created one there, thereby invalidating any reference to that asset that is on other components.

Is there anyway to REPLACE the asset instead of creating it anew. Essentially I'm looking for functionality similar to ReplacePrefab()

-ryan

Comment
Add comment · Show 2
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 Julian-Glenn · Aug 13, 2010 at 11:36 PM 0
Share

Not sure if this is relevant to your situation: http://unity3d.com/support/documentation/ScriptReference/AssetDatabase.LoadAssetAtPath.html

avatar image PatHightree · Dec 16, 2010 at 11:50 PM 0
Share

Wondering about that exact question. I tried replacing the asset and replacing the original .meta file, but it didn't do any good.

Please post if you've found an answer, otherwise.... BU$$anonymous$$P

2 Replies

· Add your reply
  • Sort: 
avatar image
14

Answer by Kencho · Jul 20, 2013 at 04:28 AM

I've found this method that apparently may work to clone any UnityEngine.Object asset:

 AnimationClip outputAnimClip = AssetDatabase.LoadMainAssetAtPath (path) as AnimationClip;
 if (outputAnimClip != null) {
   EditorUtility.CopySerialized (animClip, outputAnimClip);
   AssetDatabase.SaveAssets ();
 }
 else {
   outputAnimClip = new AnimationClip ();
   EditorUtility.CopySerialized (animClip, outputAnimClip);
   AssetDatabase.CreateAsset (outputAnimClip, path);
 }

That snippet clones animClip into outputAnimClip. If the target asset doesn't exist at path, it will be created; otherwise, it's copied and replaced, but the links are maintained.

First, it gets a reference to the asset at path. If the reference is not null, the asset already exists and it simply replaces it by copying the contents of animClip into outputAnimClip (which references the existing asset), and saving all the assets. If the reference is null, then the asset doesn't exist, creates a new animation clip, copies the contents, and creates an asset using that object outputAnimClip and the given path.

I guess this will work with any other subclass of UnityEngine.Object, although they would require a different treatment (specially in their instantiation). Maybe this could be rewritten as a generic function requiring the parameter T to be a subclass of UnityEngine.Object and have a parameterless constructor.

This question is fairly old, I know, but as there's yet no official implementation and no generic answer, yet some people may find it useful, I'm leaving this here. Hope it helps! :)

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 huulong · Sep 20, 2015 at 03:24 PM 0
Share

Worked for me! In the else clause, you can directly use:

AssetDatabase.CreateAsset (animClip, path);

and it works as well.

avatar image pep_dj · Jun 06, 2016 at 03:54 PM 2
Share

Based on answer above:

 T CreateOrReplaceAsset<T> (T asset, string path) where T:Object{
          T existingAsset = AssetDatabase.LoadAssetAtPath<T>(path);
          
          if (existingAsset == null){
              AssetDatabase.CreateAsset(asset, path);
              existingAsset = asset;
          }
          else{
              EditorUtility.CopySerialized(asset, existingAsset);
          }
          
          return existingAsset;
     }
avatar image
1

Answer by femi · Dec 29, 2010 at 09:27 PM

This seems to work:

Mesh dummy = (Mesh)AssetDatabase.LoadAssetAtPath(asset_path, typeof(Mesh)); if (!dummy) { dummy = new Mesh(); dummy.name = name; AssetDatabase.CreateAsset(dummy, asset_path); } else dummy.Clear();

// [... import dummy ...]

AssetDatabase.SaveAssets();

(attempting to load/modify mesh then clear it and import from the new version of the resource)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Importing custom assets? 0 Answers

Two different assets have different layers on the same layer number. How to fix 1 Answer

Cannot save a generated texture as part of a bundle 1 Answer

How do I import assets from the Asset Store while offline? 2 Answers

Unity Error removes assets - "Rebuilding Library because asset database could not be found!" 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