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
4
Question by Michael 25 · Mar 22, 2011 at 11:00 PM · prefab

Can a prefab be written in code?

I don't mean instantiated, but can you actually create a (say, C#, although I'm not fussy :-) class that represents a Prefab? And if so, what would that class look like?

All the explanations that I've seen involve creating prefabs using the IDE.

Thanks for any insights!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Statement · Mar 22, 2011 at 11:23 PM

Well, you can certainly do this in code. Consider this psuedo code for example:

public GameObject CreateMilkBottle()
{
    var prefab = new GameObject("Milkbottle");
    var milk = prefab.AddComponent<MilkContainer>();
    milk.Capacity = 300;
    var meshFilter = prefab.AddComponet<MeshFilter>();
    meshFilter.mesh = loadmesh();
    prefab.AddComponent<MeshRenderer>();
    prefab.AddComponent<BoxCollider>();
    // etc
    return prefab;
}
Comment
Add comment · Show 2 · 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 Michael 25 · Mar 23, 2011 at 12:08 AM 0
Share

I guess I meant something like ...

public class $$anonymous$$yPrefab : GameObject {

}

If (and that's a big if :-) I understand this page -- http://unity3d.com/support/documentation/$$anonymous$$anual/Instantiating%20Prefabs.html -- correctly, then Create$$anonymous$$ilkBottle() isn't really creating a prefab, but just individual unrelated GameObjects. Otherwise it would be using Instantiate() ... I think ...

Having said that, what you wrote was really instructive as it solved another problem that I hadn't even asked yet. How did you do that? :-)

avatar image Statement · Mar 23, 2011 at 01:37 AM 0
Share

No, you can't create subclasses of GameObject because the class is sealed, meaning the designers of the class didn't want/expect people to subclass it. The creation of the prefab is just in the code above, although it might not seem that clear. A prefab is basically a serialized instance of a game object. It serialize scripts and values. Prefab just means "prefabricated". It's there to save you doing just what the above code does. What is the intent of this? Perhaps there exist another feature in Unity that does the job? Instantiate just create a game object from a preset like the code.

avatar image
1

Answer by DaveA · Mar 23, 2011 at 12:47 AM

If you really want it on disk, try this: http://unity3d.com/support/documentation/ScriptReference/AssetDatabase.CreateAsset.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
avatar image
0

Answer by MountDoomTeam · Sep 22, 2012 at 02:54 PM

have a look at PrefabUtility.CreateEmptyPrefab that creates things like prefabs programatically. :)

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

1 Person is following this question.

avatar image

Related Questions

Monster Spawner 3 Answers

How to spawn Prefab to a certain position. 2 Answers

How can I attach a gameobject to a prefab in code? 1 Answer

Combine GameObjects into one prefab via code at runtime 0 Answers

Creating prefab by script and using it crashes unity 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