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
0
Question by bawenang · Feb 01, 2016 at 01:31 PM · editorprefabinspectorscriptableobjecteditorwindow

Referencing / linking a .asset / .prefab file in another .asset / .prefab file programmatically.

Hi,

Actually I've asked this on the forum but no one answered. So here goes...

I wanted to make an Editor Window that can automate the whole creation of an asset file from a configuration file. So I can create the asset file from ScriptableObject.CreateInstance and the the AssetDatabase.CreateAsset methods. But how do I reference another .asset file or .prefab file that is in the same Project folder into it (ie. similar with dragging it to the created .asset file) from the code?

I need it because the asset files are actually linked list entries. I need to have, eg. asset1 linked to asset2, asset2 to asset3, etc. So I can iterate between the data stored in the .asset files based on it's chain. And like the creation, the linkage of the asset files should be automated from a configuration files like the asset's creation.

The other reason is, I also need to be able to reference a prefab file in the project's folder automatically as a variable of the .asset file. Which should be searched throughout the project folders automatically. And again, based on a configuration file.

My instinct says it has got to do with the Assemblies. But how exactly do I do it?

Thanks in advance.

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

2 Replies

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

Answer by _dns_ · Feb 01, 2016 at 03:38 PM

To assign a prefab/asset to another object's serializable field, the prefab/asset must be loaded with AssetDatabase.LoadAssetAtPath (or similar methods) and then assigned to the field. Unity will take care of the rest for the serialization of the newly created object. You should also use EditorUtility.SetDirty on the modified object to tell unity that it will need to re-serialize it the next time the scene/project is saved.

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 bawenang · Feb 02, 2016 at 09:13 AM 0
Share

Yay! It worked! thanks a lot dude. :D

avatar image
0

Answer by winxalex · Feb 14, 2018 at 02:27 PM

 MyAssetType myAsset=ScriptableObject.CreateInstance<MyAssetType>();
       AssetDatabase.AddObjectToAsset(prefab, myAsset);
      myAsset.referencing=prefab;

// same with 2 assets, prefab in asset, asset in prefab, as prefab actually is another Asset

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

42 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

Related Questions

Custom Inspector for ScriptableObject 1 Answer

Unity inspector scripting - scriptable objects - card game 0 Answers

Set the Rect of PopupWindowContent 1 Answer

Inspector view changed! Looks totally different. Apply, Revert, ... prefab buttons gone. Help! 1 Answer

Open scene and load 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