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
3
Question by yatagarasu · Dec 23, 2013 at 10:37 AM · editorprefabextension

Is it possible to edit prefab in editor mode by script?

Is it possible to write script which will open prefab selected in Project tree (where all resources are available) do something with it's content (find and change GameObjects) and than save it.

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

3 Replies

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

Answer by Statement · Dec 23, 2013 at 10:46 AM

Yes, it's entirely possible.

I don't know exactly what you had in mind. Look at my answer at Generating a prefab from Audio Clip via script if you want to see one real example of what can be done.

  • The UnityEditor namespace contains API for writing editor scripts.

  • Your Editor scripts should be placed in a folder that you create, called `Editor`.

If you are new to Editor Scripting, I'd suggest you start reading Extending the Editor. Then start looking at the Scripting Reference for all kinds of editor APIs that are available, to build yourself a rough sense of what is available. Not all of it will make sense to you at first glance, but it helps to have it in the back of your head as a vague imprint. If you look on the left side, you can set the dropdown to Editor Classes and the list below will populate with the documented API.

  • **PrefabUtility** is a class that has prefab helper methods.

  • **Selection** is a class that gives you easy access to whatever you have selected in the editor. See the link for details.

  • **AssetDatabase** is the main entry point to accessing assets in the project.

Comment
Add comment · Show 3 · 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 yatagarasu · Dec 23, 2013 at 11:07 AM -1
Share

Thanx. I was to lazy to check myself.

I want to write a refactoring script which will update GameObjects with my components after I change some RequireComponent attributes.

avatar image yatagarasu · Dec 23, 2013 at 11:10 AM 0
Share

I wonder if it is also possible to add some Components which will be omitted in final build (during publish) to store some metadata.

avatar image Statement · Dec 23, 2013 at 11:12 AM 2
Share

You could create a dummy game object that holds components (maybe put it as a child) and set the tag of that child object to "EditorOnly". Objects with this special tag are stripped of build.

This, in conjunction with hideflags so it's invisible to the level designers is a pretty neat way of ensuring scene-based metadata to persist, but not leak out into the final builds.

Otherwise you'd have to create a script that goes through your scenes, possibly creates a copy of them and on the copy you can safely mass delete stuff of a certain kind without risking ruining the real scene.

avatar image
5

Answer by bansheesoft · Jan 05, 2017 at 06:00 PM

EditorUtility.SetDirty (GameObject); Did it for me in a script editing node editor. May help someone.

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
avatar image
3

Answer by JamieFristrom · Nov 19, 2015 at 02:08 AM

It actually took me a couple of hours to figure out how to do it from the resources provided above, so here's what actually worked for me - this little example shows editing a Version prefab from script as a way of automating updating version numbers for a build script:

 // get a reference to the prefab itself, not a clone or instantiation: 
 GameObject versionObject = AssetDatabase.LoadAssetAtPath("Assets/Resources/Version.prefab", 
      typeof(GameObject)) as GameObject; 
 
 // Edit it
 versionObject.GetComponent().versionNumber = versionNumber;
     








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 ActionFG · Jan 19, 2018 at 09:06 AM 1
Share

It's perfect by Combineing @bansheesoft answer

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

23 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

Related Questions

How do I get the default scene GUI for a CustomEditor for RectTransform? 1 Answer

Hiding editor extension scripts 2 Answers

Why do prefabbed meshes go missing whenever I pull an update from Unity Collab? 0 Answers

Change angle of camera in prefab preview 1 Answer

How Mark Prefab Dirty? 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