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 Patrick2607 · May 22, 2017 at 04:03 PM · unity 5prefaboptimizationresources.loadbest practice

Best practice instantiating objects from code

I'm currently developing a RTS which includes a self made random map generator. Every prefab must be loaded from code, there is nothing in my scene except a Camera. What is the best way to load all the prefabs?

Let's say I have several buildings and unit types. I don't want to use Resource.Load every time I create a building or a unit, that's not good for performance.

Do I create a static class with all references to every prefab and load them at the start of the game? Do I create a database or something likewise?

I can't seem to find a solution on the internet. Also, almost everyone uses the editor. All help is appreciated.

Edit: After some more research I'm actually looking for object pooling with resources loaded from code. Anyone got this working?

Comment
Add comment · Show 1
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 RobAnthem · May 22, 2017 at 10:12 PM 0
Share

Well my suggestion would be to create a static management class with a Dictionary that has the prefabs and an enum for each Unit type. Then use Resources to load them as needed, and through the life of a game, you will likely end up with all prefabs loaded, but without the cost of loading them all at once. An example would be...

 public static class Unit$$anonymous$$anager
 {
     public enum UnitType { Soldier, Archer, Catapult }
     private static Dictionary<UnitType, GameObject> units;
     public static GameObject GetUnit(UnitType unitType)
     {
         if (units == null)
         {
             units = new Dictionary<UnitType, GameObject>();
         }
         if (!units.Contains$$anonymous$$ey(unitType)
         {
             units.Add(unitType, Resources.Load<GameObject>("units/" + unitType.ToString()));
         }
         return units[unitType];
     }
 }

then you just need to name your prefabs the exact same names as the UnitType enums.

The instantiation process can occur elsewhere in your pooling system. This is just how I would handle the resources side, as far as pooling goes, pooling is pretty simple, you can still use the enum too, like this.

 public static Dictionary<UnitType, List<GameObject> existingUnits;
 public static GameObject GetExistingUnit(UnitType unitType)
 {
     if (existingUnits == null)
     {
         existingUnits = new Dictionary<UnitType, List<GameObject>();
     }
     if (existingUnits.Contains$$anonymous$$ey(unitType))
     {
         if (existingUnits[unitType].Count > 0)
         {
             GameObject unit = existingUnits[unitType][0];
             existingUnits[unitType].Remove(unit);
             return unit;
         }
     }
     return GetUnit(unitType);
 }
 public static void AddUnit(GameObject unit, UnitType unitType)
 {
     if (existingUnits == null)
     {
         existingUnits = new Dictionary<UnitType, List<GameObject>();
     }
     if (!existingUnits.Contains$$anonymous$$ey(unitType))
     {
         existingUnits.Add(unitType, new List<GameObject>());
     }
     existingUnits[unitType].Add(unit);
 }

Hope this helps, and makes sense :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tMahon · May 22, 2017 at 04:53 PM

Use the prefabs and reference them using a serialized field, then use GameObject.Instantiate() to create them. Check out the overloads for Instantiate in the documentation, you can set whatever you need as you create it. Beyond that, there's a really good tutorial for procedural tile creation if you look at the first 2 parts of the RogueLike Tutorial found here.

Comment
Add comment · Show 3 · 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 Patrick2607 · May 22, 2017 at 07:16 PM 0
Share

I know how to instantiate objects, I just want the prefabs loaded in memory. I'm looking for an efficient way to do this. I don't want to "get" the prefab by using Resources.Load every time I instantiate a new object.

avatar image bobisgod234 Patrick2607 · May 23, 2017 at 01:47 AM 0
Share

You only need to load a resource once, then you can instantiate it as often as you want.

avatar image Bonfire-Boy Patrick2607 · May 23, 2017 at 09:49 AM 0
Share

bobisgod is correct, but actually t$$anonymous$$ahon's solution does not require you to use Resources.Load at all.

You'll probably want to have some kind of factory or manager object handling the map creation, which will have fields that are references to the prefabs.

If this is a scene object then you can make those fields public and just hook up the prefabs to those fields in the inspector. Then you can instantiate them without using Resources.Load. (t$$anonymous$$ahon's solution)

OR if you don't want to hook them up in the inspector, you can load them using Resources.Load and as bob says you only need to do it once. One way to handle this would be to just load them all in the factory object's constructor (or Awake function of course, if it's a GameObject). Or if you want to be sure that you only load the prefabs you're using, you could use a "lazy" approach; checking each prefab field for null before use and loading it if required.

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

137 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 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

Reducing draw calls unity simple randomly generated world? 0 Answers

About to gut my use of the Resources folder. What are the best practices going forward? 1 Answer

What is the best way to load an instance of a gameObject when loading a saved game? 0 Answers

Do bulk prefabs cause performance issues? 2 Answers

Layers, Prefabs and Build Settings have been deleted 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