Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

access a shader property from C#

Discussion in 'Scripting' started by sebako, May 2, 2011.

  1. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    Hey guys,

    can anyone tell me how i can access these from C#?:

    Code (csharp):
    1. Properties {
    2.         _Seed ("Seed", Range(0.0,1.0)) = 0.5
    3.        
    4.         _NoiseScale1 ("Noise Scale", Float) = 1
    5.         _NoiseTexture1 ("Noise Texture", 2D) = "white" {}
    6.         _RampTexture ("Ramp Texture", 2D) = "white" {}
    7.         _BandFilter ("Band Filter", Range(0.0,1.0)) = 0.5
    8.         _BandWidth ("Band Width", Range(0.0,1.0)) = 0.5
    9. }
    I want to access the "Seed" properties 'n such.
    any hints are welcome.

    thanks :)
     
    aparajithsairam likes this.
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
  3. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    Code (csharp):
    1. Debug.Log(planet.renderer.material.GetFloat("Seed"));
    returns an error, even if i log HasProperty("Seed") i get false, for some wierd reason,

    but if i log planet.renderer.material.name, i get the right name of the shader.
    Any ideas?
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    what about _Seed?
     
  5. sebako

    sebako

    Joined:
    Jun 27, 2009
    Posts:
    301
    arghs - thank you :)
     
  6. Rispat-Momit

    Rispat-Momit

    Joined:
    Feb 14, 2013
    Posts:
    261
    hahaha that was funny :)
     
    bakinto and AugmentedMarketer like this.
  7. bakinto

    bakinto

    Joined:
    Sep 14, 2018
    Posts:
    9
    logged in just say that was funny and thanks for the answer too :)
     
    Rispat-Momit likes this.
  8. Vaupell

    Vaupell

    Joined:
    Dec 2, 2013
    Posts:
    301
    SOLVED - See next post..

    Hi All, struggling a little with updating the "Clip" value at run time.

    I am trying to change clip value over time, however i am not getting any issues except the clip value never changes.
    i watch the debug of the float change fine as expected, but not updating the shader.

    for sure i am doing something wrong, i went to the script reference to re-check that the enum property.clip is correct and it takes a float, and it does, but somehow it is never passed on.

    If i manually drag the slider back and forth it does what i expect.

    Any ideas.


    Screenshot:

    upload_2023-2-7_19-35-18.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using AmazingAssets.AdvancedDissolve;
    5. using UnityEngine.Rendering;
    6.  
    7. public class DissolveThisStuff : MonoBehaviour
    8. {
    9.     private float time;
    10.     public float targetTimeDissolve = 10f;
    11.     public float targetValue = 1f;
    12.     private Material LocalMaterial;
    13.  
    14.  
    15.     private void OnEnable()
    16.     {
    17.         LocalMaterial = GetComponent<Material>();
    18.         AdvancedDissolveKeywords.SetKeyword(LocalMaterial, AdvancedDissolveKeywords.CutoutStandardSource.TwoCustomMaps, true);
    19.     }
    20.  
    21.     private void Update()
    22.     {
    23.         time += Time.deltaTime;
    24.         float value = Mathf.Lerp(0, targetValue, time / targetTimeDissolve);
    25.         Debug.Log(value);
    26.  
    27.         AdvancedDissolveProperties.Cutout.Standard.UpdateLocalProperty(
    28.             LocalMaterial,
    29.             AdvancedDissolveProperties.Cutout.Standard.Property.Clip,
    30.             value);    
    31.     }
    32.  
    33. }
     
    Last edited: Feb 7, 2023
  9. Vaupell

    Vaupell

    Joined:
    Dec 2, 2013
    Posts:
    301
    Okey... UPDATE.. got it working as i wanted to..

    Changed script from above.. :)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using AmazingAssets.AdvancedDissolve;
    5. using UnityEngine.Rendering;
    6.  
    7. public class DissolveThisStuff : MonoBehaviour
    8. {
    9.     private float time;
    10.     public float targetTimeDissolve = 10f;
    11.     public float targetValue = 1f;
    12.     private Material LocalMaterial;
    13.     private Renderer thisRender;
    14.  
    15.  
    16.     private void OnEnable()
    17.     {
    18.         thisRender = GetComponent<Renderer>();
    19.         LocalMaterial = thisRender.material;
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         time += Time.deltaTime;
    25.         float value = Mathf.Lerp(0, targetValue, time / targetTimeDissolve);
    26.         Debug.Log(value);
    27.  
    28.         AmazingAssets.AdvancedDissolve.AdvancedDissolveProperties.Cutout.Standard.UpdateLocalProperty(
    29.             LocalMaterial,
    30.             AdvancedDissolveProperties.Cutout.Standard.Property.Clip,
    31.             value);    
    32.     }
    33.