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 hrgchris · Sep 05, 2019 at 02:00 PM · graphicsmaterialsrenderer

How to check if a Renderer has instantiated materials without instantiating them?

Hey there

I'm writing a system in which I need to 'backup' the state of all the renderers before swapping out their materials for something else. (fyi, this is a workaround for the camera replacement shaders not working on some android platforms).

However, in order to make this work I need to check if a renderer has any shared materials (fine), but also if it has instantiated materials (i.e. those created by accessing the Renderer.materials property). Unfortunately I can't find any way to check if a renderer does have them without accessing that property, which automatically instantiates them!

Has anybody a solution for this? It seems like an obvious problem but I've had no luck on Google or with Unity docs.

Cheers

Chris

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 hrgchris · Sep 05, 2019 at 02:05 PM 1
Share

ps - I'm happy to be told there's a better way, or the solution to getting the replacement shader stuff working on Quest. Either works for me :)

avatar image Pangamini hrgchris · Sep 05, 2019 at 02:10 PM 0
Share

That's why you should also formulate your original problem - rendering the scene with a replacement shader is definitely easier (and error-prone) than replacing all the materials in it.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Pangamini · Sep 05, 2019 at 02:08 PM

If you access the sharedMaterial property, it will return whatever the material is using to render. And AFAIK it will return the instantiated material, IF it was instantiated before (but I'd check that first). SO you can get the material the renderer is using... I don't think you can find out if the sharedMaterial was instantiated by the renderer itself (without, as you said, instantiating one) BUT you can check if the material was instantiated at runtime, by checking its GetInstanceID() value - this is, afaik, an undocumented feature - but the value is negative for runtime instantiated objects.

Comment
Add comment · Show 4 · 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 hrgchris · Sep 05, 2019 at 02:22 PM 0
Share

Ah - so you think the 'shared$$anonymous$$aterial' property always reflects the materials the renderer is currently using, regardless of whether they have been instantiated via an access of the material/materials properties?

avatar image Pangamini hrgchris · Sep 05, 2019 at 02:27 PM 1
Share

I actually never use the .material property ( I can instantiate my own materials when I really need them ) but I assume that. I don't think that a renderer stores two separate values. Also, setter of the material and shared$$anonymous$$aterial points to the same internal method (accoring to ILSpy)

But you can easily test that yourself

avatar image hrgchris Pangamini · Sep 05, 2019 at 02:33 PM 0
Share

I think I shall look into this stuff as it seems worth knowing exactly what it is doing. I typically avoid using 'material' myself, but I'd like this to be a catch-all solution.

However... it turns out I was being an idiot with the RenderWithShader function on the camera, and the shader wasn't in the build. Annoyingly unity simply didn't replace the shader rather than report an error, but working now nonetheless!

Show more comments

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

127 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

Related Questions

Shading with GL.TRIANGLE_STRIP 2 Answers

Combine materials out of renderer.materials 1 Answer

Same texture across two materials, is the texture loaded twice? 0 Answers

Change the texture on a material at runtime without creating an instance 1 Answer

Where to call Graphics.DrawMesh to successfully draw during editor pause? 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