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
1
Question by Olter · May 15, 2013 at 12:25 PM · c#prefab

Filling a scene with prefabs programmatically

Greetings, everyone.

I'm developing a 2d-game with my team. The point is that our game levels are created with a different program, which is much easier for a designer to create 2d level compared to unity. The generated level is saved as an xml-file. The task is to convert that level into a unity scene.

As a c# programmer I can easily parse this xml-file, using linq2xml, into a list of objects. Then I suppose, I can create a set of prefabs, from the given list using command like:

 //some loop using List<>
  Instantiate(Resources.Load("PrefabName"), new Vector3(x,y,z), Quaternion.something);
  

That will fill the scene with a set of prefabs, right?

But I don't know Unity quite well yet, so my question is: what'll be the best approach to fill the scene with prefabs?

1) Is there a way, to do this in editor without launching a game? Or maybe create some test GUI, that will allow to launch a converting script, that will fill a scene with objects and save it?

2) If not, so I suppose, the level can be generated in runtime. Is there something like OnCreate event in Scene? Perhaps I should use the OnLevelWasLoaded method, but the description in manual looks quite strange for me, so I can't understand how to use it.

Any help would be much appreciated.

Comment
Add comment · Show 2
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 Owen-Reynolds · May 15, 2013 at 12:35 PM 0
Share

For method #2, the code would just go into Start (or Awake) in any script. Often a script that does only that, placed on an empty or on the Camera.

There should be a way to have an editor run the same code on a button click or something (which would be better, since you don't parse the file every time you run.)

avatar image CHPedersen · May 15, 2013 at 12:40 PM 0
Share

While it's true that creating the scene in the editor saves the CPU the trouble of parsing the X$$anonymous$$L every time the program is run, it also means the scene gets encoded statically in the built stand-alone. If he moves the scene-creation code to runtime, his game becomes extensible. It would be able to load new levels created by his designers after he published it, without having to rebuild the game.

2 Replies

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

Answer by CHPedersen · May 15, 2013 at 12:37 PM

The answer depends on when and where it is important for you to have access to the scene. Is it necessary for you to view and edit the scene at editor time? If so, you need to create an Editor script that creates instances of prefabs for you to fill the scene at editor time. There is a tutorial on how to extend the editor this way here:

http://docs.unity3d.com/Documentation/Components/gui-ExtendingEditor.html

If you don't need to view and edit the scene at editor time, but just need to import the XML file during game load, then you're already on the right track with what you suggested. OnlevelWasLoaded is a callback that gets called when a scene was loaded. Its integer parameter is the number (probably array index) of the scene as it appears in your File->Build Settings. You can provide an implementation of OnLevelWasLoaded in a script and attach it to a gameobject, and then test against this integer to see if the level that was just loaded is the correct scene you wish to fill with data from that XML file.

Alternatively, you could just start creating prefabs from the XML in the Start or Awake method of a script and attach it to a gameobject (called LevelLoaded, maybe) which is present only in the scene you wish to fill with prefabs from the XML. That's all up to you how you wish to organize it.

To help you select which callback works best, it might be an idea to consult the execution order of the callbacks supported by MonoBehavior:

http://docs.unity3d.com/Documentation/Manual/ExecutionOrder.html

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 Olter · May 16, 2013 at 12:45 PM 0
Share

Thanks, that really helped me.

avatar image
0

Answer by raimon.massanet · May 15, 2013 at 01:51 PM

You can use

 PrefabUtility.InstantiatePrefab

to instantiate prefabs from the editor.

http://docs.unity3d.com/Documentation/ScriptReference/PrefabUtility.html

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Is there a way to set components in a prefab before it is Instantiated? 0 Answers

How to associate prefab with a variable in C#? 1 Answer

Only instantiate if clear space beside prefab 1 Answer

How to create new tail in a simple 3D Snake 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