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
0
Question by Tekksin · Sep 02, 2014 at 09:34 PM · javascriptshadermaterialruntime

Check What Material is Being Used?

Hey guys, I've been trying to do something which should've been simple, but apparently I'm missing something.

So I was using, "if (renderer.materail.mainTexture == myTextureVar)" to see if a texture is being used, to do something. Which worked fine. But it brought my draw calls from 13 to 24. I'm still in alpha with this project, so I'm afraid if I "allow" that to be, things will get out of hand by the time I say it's finished.

Anyways, so I figured out that changing the texture at runtime causes a new draw call, so I switched to the shader/material, and that kept my calls low.

But when I do "if (renderer.material == myMaterialVar)" then it doesn't work.

I even debug the material used, and here's where it gets weird. Instead of just debugging the "Material used is: myMaterialVar", it writes, "Material used is: myMaterialVar (Instance) (UnityEngine.Material)".

What do I have to script to check what material is being used if unity makes an instance of it and all other kinds of nonsense? And is there any documentation on this strange issue?

P.S. I write in javascipt

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

Answer by Eric5h5 · Sep 02, 2014 at 10:01 PM

Use sharedMaterial instead. Accessing material creates an instance. This is done for the sake of making things easier to use, where you can do stuff like, say, "renderer.material.color = Color.blue" to change the color of one specific object to blue, without having to create and manage material instances yourself. The downside is that you lose control over exactly what's going on. (Also, you're more likely to have misunderstandings about how Unity and materials actually work.)

So to maintain complete control you should never touch material, and only use sharedMaterial. It can be more work, but you can still do everything that you could with material.

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 Tekksin · Sep 02, 2014 at 10:04 PM 0
Share

Thanks so much. I will research where it is best to use sharedmaterial ins$$anonymous$$d of material.

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

23 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

Related Questions

Building a Unity 5 Standard Material at runtime 3 Answers

Material doesn't have a color property '_Color' 4 Answers

Multi materials for one mesh 0 Answers

material Instance only updates after changes, when the Material Component is expanded or folded 0 Answers

How to change value from shader 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