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 Devcon4 · Feb 22, 2016 at 12:57 PM · editorinstantiatestatic

How to Instantiate objects while in editor?

I use a lot of auto-generated code when I'm working on a project (procedural terrain, loading objects from json, etc), whitch is great but how its set up I have to run before I can see anything in my scene. This is very annoying so I was wondering if there is a better way to do things. What I end up doing with most of my code is something like this.

 public List<GameObject> myObjects;
 
 public List<GameObjects> CreateObjects(int numberOfObjects) {
     var list;
     for(int i = 0; i < numberOfObjects; i++) {
         var newObj = Instantiate(prefab, pos, rot) as GameObject;
         newObj.transform.parent = transform;
         newObj.getComponent<dataStruct>().data = jsonData;
         list.Add(newObj);
     }
     return list;
 }
 
 void Start() {
     myObjects = CreateObjects(10);
 }

Is there a better way of doing this that will run in the editor? Maybe if I made this function static? But then how would I access transform or other parts that depend on the GameObject?

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

Answer by Dave-Carlile · Feb 22, 2016 at 01:01 PM

The ExecuteInEditMode attribute is a starting point for you. It allows your scripts to run in edit mode which will let you create objects like that in the editor. You can think of Unity as "the game" and so all of the same things you would in a game. But you likely will have to pay attention to Unity's scene serialization and some other interesting object lifetime sort of things.

I don't have much experience in creating rock sold in-editor code, but using that attribute or "custom editor" as a starting point for your research should get you moving in the right direction.

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 Devcon4 · Feb 23, 2016 at 12:24 AM 0
Share

Awesome! This works very well with some of my scripts already. I'll have to look into custom editor more.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Spawning objects on trigger does not work as expected 2 Answers

Static Batching with Asset Bundles 1 Answer

Instantiation working in editor, not in build. 0 Answers

instantiate in the editor (Prefab Problem) 1 Answer

Trying to run 5 instances of a static method at once to do an action, probably a dumb thing to do? 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