Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Shiolle · May 26, 2011 at 02:31 PM · prefabeditor-scriptingassetdatabase

Editing and saving assets

I have an editor script that currently loads assets in certain directory, modifies them and then saves the changes. I use the following code:

 curPrefabs = AssetDatabase.LoadAllAssetsAtPath(pfPath);
 
 foreach(UnityEngine.Object obj in curPrefabs)
 {
     if(obj != null)
     if(obj.GetType() == typeof(GameObject))
     {
         target = (GameObject)Instantiate(obj);
         ...............modify target..............................
         target.name = obj.name;
 
         UnityEngine.Object newPref = EditorUtility.CreateEmptyPrefab("Assets" + 
             path + obj.name + ".prefab");
         EditorUtility.ReplacePrefab(target, newPref);
         GameObject.DestroyImmediate(target);
    }
    AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);

}

1st. If I try to use LoadAssetAtPath(pfPath, typeof(GameObject)) or LoadMainAssetAtPAth(pfPath) I always receive null, even when LoadAllAssetsAtPath return GameObject (among other types).

2nd. If I try to replace an existing prefab with EditorUtility.ReplacePrefab(target, obj); instead of creating a new one like in the script above, Unity seems to save the file (I know this because I can see the changes made when I run the script two times in a row), but it never updates in the editor and is overwritten with unmodified version when I close Unity.

3rd. If I use this code to replace some of the materials on a prefab, the link to materials is partially lost. I.e. if I make changes to materials they are only applied to GameObjects after I close and reopen the project. I've traced this to a separate issue: http://answers.unity3d.com/questions/122861/retaining-material-connections.html?viewedQuestions=122213&viewedQuestions=12934

What causes all these issues? What am I doing wrong?

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

0 Replies

· Add your reply
  • Sort: 

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

1 Person is following this question.

avatar image

Related Questions

Could not load copied asset via AssetDatabase.LoadAssetAtPath 4 Answers

How to provide undo for EditorScript changes to a prefab? 1 Answer

How do I programmatically assign a GameObject to a prefab? 6 Answers

Automatically update prefab instance 1 Answer

AssetDatabase.AddObjectToAsset is not persistent? 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