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 SlySIy · Feb 18, 2014 at 02:57 PM · material.create a texture

Same material on object without the same texture

Hello,

Is it possible to change a material on an object without changing the original material?

I've got an example. I have multiple objects in my scene using the same material. I want to use this material but without the same texture. I don't want to create 100 materials instances for the different 100 object in my scene.

Is it possible? How I can do that?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by deltamish · Feb 18, 2014 at 03:03 PM

Hi, Thats a quite logical question

Yes thats possible all you have to do is create a simple script that duplicates the materail and assigns it to them

 public Texture TheTexture;
 public string ReferenceField = "_MainTex"; // This is used to set the texture in this case the Main texture.
 
 //Hoping that you are using  
 
 void Start()
 {
   Material mat = new Material(renderer.sharedMaterial);
   mat.SetTexture(ReferenceField,TheTexture);
   renderer.sharedMaterial = mat;
 }

THe ReferenceField is just a string that is used to reference the anme of field declared in the shader

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 Owen-Reynolds · Feb 18, 2014 at 04:06 PM 0
Share

And, implicit in the answer, you have to use code. There's no nice Inspector way.

It's a non-obvious Unity rule that code changes almost always copy the material. You can even use merely renderer.material.mainTexture = hapyCowTex; and Unity will change only your personal material (it actually makes a quick duplicate of it the 1st time you change it.)

Compared to changing the texture in the Inspector, where it's always a link to the single material all 100 objects share. There's no easy Inspector way to say "use material A, but with this change just for me."

avatar image SlySIy · Feb 18, 2014 at 05:18 PM 0
Share

Thanks for your clear answer guyz!

But in my case, it's seems a little bit special.

Like you can see on the picture, I'm using a sprite render. The sprite selected in the sprite renderer will be automaticly set in the first field of my shader without change it (in the shader I use [PerRendererData] for the _$$anonymous$$ainTex).

But If I change the bump(normal map) in my shader, it's gonna change it in the shader, so each game object has his own texture but have the same bump/normal map texture, thing I don't want.

So in this context how I can change the normal map in my object without changing it in the original shader?

alt text

screenshadercompo02.png (51.8 kB)
avatar image Owen-Reynolds · Feb 18, 2014 at 09:39 PM 0
Share

It's not special. Sure, it's co$$anonymous$$g from a SpriteRenderer, ins$$anonymous$$d of a $$anonymous$$eshRenderer. But CustomSpriteShadow is still the same as any other material: It's merely a link in the Inspector, to the real material in your Project folder (even though it looks like your personal copy.) But most changes using code will magically first create a personal copy.

avatar image
0

Answer by SlySIy · Feb 19, 2014 at 01:29 PM

Yep, It's working perfectly! With this I'm able to change the bump texture. Thanks a lot to all of you for your precises answers.

There is my final script:

 using UnityEngine;
 using System.Collections;
 
 public class MaterialManager : MonoBehaviour {
 
     public Texture TheTexture;
     public string ReferenceField = "_BumpMap"; // This is used to set the texture in this case the Bump texture. You can find the correct name in your shader.
 
 //Hoping that you are using
 //Yep, I'm using it deltamish!
 
     void Start()
     {
     Material mat = new Material(renderer.sharedMaterial);
     mat.SetTexture(ReferenceField,TheTexture);
     renderer.sharedMaterial = mat;
     }
 }
Comment
Add comment · 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
0

Answer by castrojoaquin91 · Jun 04, 2021 at 05:04 PM

Hi there! I know this is super old but If you're using a newer version of Unity there are some little changes to make in the Start method:

  void Start()
  {
  Material mat = new Material(GetComponent<Renderer>().sharedMaterial);
  mat.SetTexture(ReferenceField,TheTexture);
  GetComponent<Renderer>().sharedMaterial = mat;
  }

Hope i can help someone :)

Comment
Add comment · 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

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

Get Asset Preview (3.5) 1 Answer

How to partially reveal texture of a plane or object 1 Answer

Good uv mapping tutorial? 1 Answer

Do material instances require manual cleanup? 1 Answer

Textures have dodgy shading 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