How to assign a texture to a material through code in unity - unity3d

Here is my code:
root_level1.GetComponent<Renderer>().material.EnableKeyword ("_NORMALMAP");
Texture tex = Resources.Load("book-cover-1-glitched-23-11-2020-8-35-56-pm") as Texture;
root_level1.GetComponent<Renderer>().material.mainTexture = tex;

To change material texture in runtime you should change it by accessing shader properties. First of all you gonna check these properties in shader code or shader properties in unity inspector. If you want to check shader code, your goal is "properties" block:
Or you can select shader in Unity Editor and check Inspector Window:
So to change Base RGB Texture we should change property "_MainTex" and to change Color we have to change property "_Color".
I your example its gonna be like this:
root_level1.GetComponent<Renderer>().material.SetTexture("_MainTex", tex);

Related

How to get values Shader in Unity

I have material with Shader in unity.
How to get a value in code?
For example, i need change value EdgeWidth in code, how can i do it?
According to the Unity documentation, you need to find your material or your shader, in your script, and you have a lot of functions to change the value of a material, like .SetFloat().
Renderer rend = GetComponent<Renderer> ();
rend.material.SetFloat("EdgeWidth", -1.18f);

Change line renderer alpha unity

Unity's line renderer component has got a material to attach, so that the line renderer uses it to show the line renderer in the scene accordingly:
The problem is that if you change this material color it works, also in runtime it can be changed in the editor itself. But not so with the alpha.
It does not respond neither in the edit nor in the code.
I tried:
Color targetColor = new Color(0,0,0,a);
_unityEngineLineRender.sharedMaterial.SetColor("Color", targetColor);
and:
Color targetColor = new Color(0,0,0,a);
_unityEngineLineRender.sharedMaterial.color = targetColor;
and it doesn't work.
Can thisn simple thing be done? if so, how?
Thanks in advance
The shader for your material probably does not support alpha, that's why you're not getting any transparency.
The workaround is to write your own shader or use other that's built into Unity.
Create a new material for your line renderer and select shader "Particles/Standard Unlit", set "Rendering mode" to "Fade". Then use it on Line Renderer instead of the default material.
You can access the color using:
renderer.material.SetColor("_Color", new Color(1f, 1f, 1f, 0.3f));

Unity Shader - Access Property

Background:
I created a dissolve Shader using the lightweight render pipiline, in Shader Graph by drag & drop. My object "dissolves" over time & fades in again. (Youtube "Brackeys Dissolve Shader")
Goal:
I want to change the _edgecolor as soon the object has been dissolved.
I can change the color via:
gameObject.GetComponent<Renderer>().material.SetColor("_edgecolor", Color.blue);
Problem:
How do i access the "Alpha" which seems to be modified by the shader, and would be a good indicator if the object is dissolved, right ?!
I tried to get the Alpha
Color c = gameObject.GetComponent<Renderer>().material.GetColor("_Color");
c.a = ....
won't work, because -> Material doesn't have a color property '_Color'
Here's a Screenshot of my ShaderGraph

How to set maps (apart from albedo, normal and metallic) at runtime in Unity?

I'm trying to dynamically set textures for my game object.
It works like this:
var skin = _skinTable[index] as Hashtable;
var renderer = CurrentShip.GetComponentInChildren<Renderer>();
renderer.material.SetTexture("_MainTex", skin["albedo"] as Texture);
renderer.material.SetTexture("_BumpMap", skin["normal"] as Texture);
renderer.material.SetTexture("_MetallicGlossMap", skin["metallic"] as Texture);
Where can I find names (first argument of SetTexture) for other maps, like Height, Occlusion etc?
(I'm talking about Standard Shader)
You can get the available map channels if you select your shader in the inspector
For a full standard shader you can check the source
https://github.com/TwoTailsGames/Unity-Built-in-Shaders/blob/master/DefaultResourcesExtra/Standard.shader

Unity Changing cubemap slot in materials

I have a material with its shader set to Skybox/Cubemap and I have cubemap1 assigned to the slot. Now I want to replace cubemap1 with cubemap2 and I found a script for this, but i can't seem to get it working.
any thoughts?
#pragma strict
var cubemap2 : Cubemap; //Change texture for cubemap
var shaderCubemap : Material; //Shader with cubemapslot
function Start () {
shaderCubemap.SetTexture("_Cube",cubemap2);
}
shaderCubemap.SetTexture("_Tex",cubemap2);
When you are not sure about a texture name in a shader, set it to debug in the top right corner of the inspector. It will display the shader and all its parameters with the naming actually used in it.