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 instruct9r · Dec 19, 2012 at 07:49 PM · texturematerialloaddynamically

One material for many objects with different textures

Hello.

I am trying something on the project i work on and it works, but i am not sure if it's going to help or not for the performance.

It's a puzzle game and let's say 90% of the objects on the screen can use the same material. So what i am doing is this:

  1. I apply 1 material to all of these objects

  2. Add a script to all of these objects which looks like this:

    // Public Variables public var textureToLoad : Texture2D; // The texture that will be displayed

      // Private Variables
         private var trans                    : Transform;        // this.transform.
         
         function Start () 
         {
             trans = this.transform;
             trans.renderer.material.mainTexture = textureToLoad;    
         }
     
    
    
    
       
    
    
  3. Add the different textures for each object to the custom created Texture2D field in the inspector

So when the game starts, all of the objects are getting their own textures dynamically at start...

This is specifically for mobile game and i am wondering if this is going to make the game run faster, because 90% of the game objects can use only one shader...

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 Alec Thilenius · Dec 24, 2012 at 06:34 PM 0
Share

Please mark your question as answered unless you have more questions? :)

2 Replies

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

Answer by Alec Thilenius · Dec 20, 2012 at 05:17 AM

This will ruin your performance, not improve it. A material is more than just an organizational tool in game engines. It is an entire state that is saved in GPU memory. The HLSL shaders used to render the textures, buffers, the texture itself... Swapping the texture like that will result in graphics memory IO if unity does what I think they do with their materials. This will murder your performance.

If you don’t have a frame rate problem, then do not start playing with things like this. The boys who wrote the rendering code know what they are doing. Trust that they have thought of stuff like this.

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 instruct9r · Dec 20, 2012 at 01:34 PM 0
Share

Thanks for the info.. I'll use separate materials for the different objects then... I have a lot of static objects (Set as Static) and i guess, if i turn on "Static Batching", that will help in some way...

avatar image Alec Thilenius · Dec 20, 2012 at 05:07 PM 0
Share

As far as performance goes in computer program$$anonymous$$g, a problem does not exist until you diagnose one (With a profiler). Unless you need to pull more frame rates out of your game to meet the required $$anonymous$$imum target platform, then don't worry about any of it.

avatar image instruct9r · Dec 21, 2012 at 12:23 AM 0
Share

I understand.. Thanks for the help :)

avatar image
2

Answer by Dom3D · Dec 20, 2012 at 05:00 AM

I think under the hood you still have a different material per object. It would be only one material when accessing Renderer.sharedMaterial. See docs for material duplication info: http://docs.unity3d.com/Documentation/ScriptReference/Renderer-material.html?from=Material

In addition to that using a different texture is a so called renderstate change and has a performance hit. Therefore using a texture atlas instead would be a solution that allows you to only use one material and one texture and thus save performance. That is, if the textures are not tiled on both directions.

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 instruct9r · Dec 20, 2012 at 01:32 PM 0
Share

I'm trying to figure out how the texture atlas works to use it.. Thanks :)

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

11 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

Related Questions

Changing GameObject texture? 4 Answers

Environment Material Disappears after ingame level restart 0 Answers

Blending one material/texture/shader to another 1 Answer

Material Resource will not load when Resources.Load is called 2 Answers

Load Material to GameObject 2 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