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 Jasior0000 · May 15, 2014 at 10:34 PM · materialdynamicsharedmaterial

How to check whether a material is unique?

Hello everyone!

Hopefully this one will be quite easy. On game start I instantiate a bunch of objects on the scene. They are ready prefabs with materials. Then I would like to differentiate them a bit by drawing additional textures to them. I started with something like this:

void Update () {

         if (!Input.GetMouseButton (0))
             return;
 
         RaycastHit hit;
         if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition),out hit))
             return;
 
         Renderer renderer = hit.collider.renderer;
         MeshCollider meshCollider = (MeshCollider)hit.collider;
         if (renderer == null || renderer.sharedMaterial == null ||
             renderer.sharedMaterial.mainTexture == null || meshCollider == null)
             return;
 
         //if( rendered.material is not unique (was not cloned yet))
         {
             renderer.material.mainTexture = (Texture2D)Instantiate(renderer.material.mainTexture);
         }
 
         Texture2D tex = (Texture2D)renderer.material.mainTexture;
         Vector2 pixelUV = hit.textureCoord;
         pixelUV.x *= tex.width;
         pixelUV.y *= tex.height;
         Color [] c = new Color[100];
 
         for(int i=0; i<100; i++)    {    c[i] = Color.red;    }
 
         tex.SetPixels((int)pixelUV.x-5,(int)pixelUV.y-5, 10,10, c);
         tex.Apply();
     }

This script is pretty straightforward, but what happens is that it keeps on instantiating new textures every frame. I need a way to know that certain material has already been unlinked from the shared materials and is free to be manipulated individualy.

Do you guys have any ideas? Thanks in advance!

John

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
0

Answer by robertbu · May 15, 2014 at 10:37 PM

You don't need to Instantiate() a new material. Any time you make any material change (at runtime), Unity replaces the shared material with a new material instance. So change away and you will be okay.

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 Jasior0000 · May 15, 2014 at 10:41 PM 0
Share

Does the texture get instantiated as well? $$anonymous$$y problem is that when I paint over the texture, all the object instances are being changed as their actual texture file is being modified (tex.Apply()). So even though the material gets cloned the texture itself is still assigned to the same file. Isn't that correct? And if it isn't then could you propose a simple solution to this problem? Would be greatly appreciated!

avatar image arutyunef · Sep 19, 2019 at 01:23 PM 0
Share

This is untrue! Though it is true for Renderer.shared$$anonymous$$aterials , not for Renderer.materials cuz this one gets instantiated.

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

21 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

Related Questions

Why isn't (.)sharedMaterial sharing this change to "_EmissionColor"? 0 Answers

Projector.material = sharedMaterial ? 2 Answers

How to dynamically create a cube and color it? 2 Answers

Dynamic Batching Question 1 Answer

Keep replaced materials of a Game Object even after ending the Play Mode C# 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