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
0
Question by zKici · Feb 14, 2014 at 10:15 PM · guieditorinspector

Unity Editor Inspector creating gameObjects in world

Hi there,

I've tried and failed many times to get a working script for this.

I basically want to be able to select prefabs and drag them into the Inspector, I have about 8 objects that need to be dragged into the inspector, once that is done, I need to select the starting point of where to instantiate obj1 (Ex: -5, 0, -5)

Then on I will figure out where the rest of the objects should go depending on where the 1st obj was created, I will do the math, however I cannot get the inspector to work with my own "First object: (Drag and drop here/prefab)" for some reason I can do labels etc. But can never get an object to drag into the inspector...

How do I go about this please help.

Thanks

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
2
Best Answer

Answer by C.Malcolm · Feb 17, 2014 at 11:21 AM

I actually wrote an Editor script which allows the user to drag and drop a prefab into a selector, then input its position, give it a name and place it. Here is the code for that:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 public class CustomWindow : EditorWindow {
 
     GameObject obj = null;
     Vector3 vec3 = new Vector3(0,0,0);
     string objname = "";
     GameObject tempobj = null;
 
     [MenuItem ("Window/My Custom Window")]
     static void ShowWindow () {
         EditorWindow.GetWindow(typeof(CustomWindow));
     }
 
     void OnInspectorUpdate () {
         Repaint ();
     }
     void OnGUI()
     {
         obj = EditorGUILayout.ObjectField("Select game object to place",obj, typeof(GameObject), true) as GameObject;
         vec3 = EditorGUILayout.Vector3Field("placement position:", vec3);
         objname = EditorGUILayout.TextField("Name for new object: ", objname);
 
         if (obj && objname != "")    {
             if (GUILayout.Button( "Add to scene"))
             {
                 tempobj = (GameObject)Instantiate(obj, vec3, Quaternion.identity);
                 tempobj.name = objname;
             }
         } else {
             EditorGUILayout.LabelField ( "You must select an object and give it a name first");
         }
     }
 }

That's a good place to get started, then I suggest reading our 'Extending the Editor' docs page:

http://docs.unity3d.com/Documentation/Components/ExtendingTheEditor.html

You should be able to add some code that then places the rest of the objects in the order you choose within the OnGUI() function.

I hope this answers your question somewhat!

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 zKici · Feb 17, 2014 at 01:29 PM 0
Share

Thanks thats a start!

avatar image
0

Answer by MakeCodeNow · Feb 15, 2014 at 08:15 PM

I assume you already have a custom Editor subclass? If not, you'll need one. From there, take a look at the DragAndDrop docs. You'll need to check for Drag events in OnInspectorGUI and handle them correctly when the drag/drop is over your inspector.

You can also just make a List of GameObject's in your MonoBehavior, in which case Unity will do all of the editor drag-and-drop for you in the default inspector.

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

20 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

Related Questions

Adding an item to a Generic List copies the old one 1 Answer

How to know if something is selected in Hierarchy or in project. 5 Answers

Is there an event being fired off when the Inspector is being resized? 1 Answer

Modify inspector 2 Answers

Modifying default Inspector GUI creator 3 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