Search Unity

Question about using a shader with texture/sprite property and sprite renderer

Discussion in 'General Graphics' started by NMatanski, Jan 25, 2020.

  1. NMatanski

    NMatanski

    Joined:
    Jul 2, 2019
    Posts:
    6
    First, I'm really sorry if I'm not explaining it properly, but English is not my native language and I'm new to graphics, shaders, shader graph and materials.

    A little introduction to the problem: I'm making a 2D game and I needed a shader which makes an outline around the sprite when hovering/clicking over it with mouse. So I've found the right shader (shader graph), then I made my own custom script to change the shader's properties on mouse hover/click (detecting with 2D ray).
    In the shader graph I have a property Texture2D (sprite).

    Now what? I've created a material with a sprite for the property of the shader. Everything works more than fine. But if I need to apply the same shader to another 2D Sprite, I have to create ANOTHER material with the same shader, but different Sprite on the Texture2D property of the shader. So I've created 3 materials for 3 different objects and the script I've made and everything else works perfectly fine.

    Is this the right workflow? Can I use the same material for different sprites and to change only the Material's Shader's Texture2D sprite property to be the same as the SpriteRenderer.sprite? But the material's texture will change in runtime and I won't be able to see the right "image" of the gameobject in the inspector (due to the different sprites in the spriterenderer and the default material's texture2d sprite). What's the way to work with Shader's Texture2D property - changing it to be the same as the SpriteRenderer's sprite in runtime, or creating different material with the same shader but with different Texture2D (sprite) on it for every selectable sprite (object) in my game?

    Thank you in advance! I want to learn the right workflow for working with materials.