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 QKlon · Jan 31, 2018 at 09:15 PM · prefab-instanceeditor scripting

How to instantiate prefabs exactly in the way as drag'n'drop does?

Hello,

I am trying to achieve exactly the same thing that happens when dragging a prefab from assets to the hierarchy. By dragging I get a blue instance with an enabled context menu item 'Select Prefab' and the enabled menu item 'GameObject/Apply Changes To Prefab' after changes have been done.

My code so far is:

 public static class MenuItems
 {
   [MenuItem ("GameObject/UI/PlayerControlBar")]
   public static void CreatePlayerControlBar(MenuCommand menuCommand)
   {
     InstantiatePrefabByUUID<GameObject>("2442d4b1b19674f26959f32c69b751da", menuCommand);
   }
  
   private static T InstantiatePrefabByUUID<T>(string uuid, MenuCommand menuCommand = null) where T:Object
   {
     var prefab = AssetHelper.LoadAssetByGUID<T>(uuid);
     if (!prefab) 
       throw new Exception("Asset with UUID '" +uuid+ "' could not be loaded.");
     
     var obj = Object.Instantiate(prefab, new Vector3(), new Quaternion(), menuCommand.context as Transform);
     if(typeof(T) == typeof(GameObject) && null != menuCommand)
       GameObjectUtility.SetParentAndAlign(obj as GameObject, menuCommand.context as GameObject);
     Undo.RegisterCreatedObjectUndo(obj, "Create " + obj.name);      
     Selection.activeObject = obj;
     return obj;
   }
 }
 
 public static class AssetHelper
 {
   public static T LoadAssetByGUID<T>(string guid) where T:Object
   {
     Debug.Log("LoadAssetByGUID("+guid+") | path: \"" + AssetDatabase.GUIDToAssetPath(guid) + "\"");
     return AssetDatabase.LoadAssetAtPath<T>(AssetDatabase.GUIDToAssetPath(guid));
   }
 }

This works almost as expected, however, the instance in the hierarchie is a clone and not displayed in blue as well as the menus mentioned above are not enabled.

How can I imitate the same behavior like dragging the prefab into the hierarchy by a UnityEditor only script? There is no need to do it in runtime.

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

1 Reply

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

Answer by Bunny83 · Feb 01, 2018 at 01:49 AM

Prefabs are a pure editor feature. At runtime there's no difference between a prefab and a prefab instance beside the fact that prefabs only exist in memory and not in the scene. Instantiate will simply create a clone of an object. If the source object is a GameObject the clone will be added to the scene.


If you want to preserve the prefab connection at edit time you have to use PrefabUtility.InstantiatePrefab. The PrefabUtility is an editor only class.

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 QKlon · Feb 03, 2018 at 09:59 PM 0
Share

Since I only asked for a solution in edit time, the link in your second paragraph does solve my issue. Thanks a lot!

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

75 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

Related Questions

Editor Scripting: change selected objects layer with button press 0 Answers

Custom Hashset inspector Issues 0 Answers

How to solve "The type or namespace name 'MenuItemAttribute' could not be found"? 1 Answer

UnityCar + Realistic FPS Prefab problems 0 Answers

how can I improve my "character select" script? 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