How to change shader params by code ?



  • Hello,

    In tutorial and examples you show how to change a shader params using animation, but how achieve same thing through code ?

    I tried to get a ref to my SpriteRenderer, but I cannot found on its material any of parameters that are defined on material's shader and that are visible into inspector.

    Thank for your help.



  • I also would like to know it if this is possible because i wanted to buy the Asset.

    I hope the Support is still active here



  • Hello,

    I think that exposed parameters can be get & set through Material API (SetFloat, SetVector, SetInt ..).



  • Hi I tried to use the material API to change a float value and I get a shader error every time? It works with generic materials but I can not get it to work with Shadero? Can you or the developer confirm this is possible? And what I might be doing wrong? Thanks



  • Actually just figured it. Instead of using the materials tutorial in unity followed this Amplify tutorial and works great for Shadero.
    http://wiki.amplify.pt/index.php?title=Unity_Products:Amplify_Shader_Editor/Tutorials/Scripting



  • hi, i wrote an custom animation utils for all UI elements that inspector can't seen in editor.

    public static class UIShaderAnimatorUtils
    {
    public class AnimationRunner
    {
    private Coroutine animation;
    private MonoBehaviour behaviour;

            private string propertyName;
            private float startvalue;
            private float targetValue;
            private float speed;
            private float delay;
            private Material material;
            private Action completionAnimation;
    
            internal AnimationRunner(MonoBehaviour behaviour, Material material, string propertyName)
            {
                this.material = material;
                this.behaviour = behaviour;
                this.propertyName = propertyName;
                startvalue = material.GetFloat(propertyName);
            }
    
            internal void Start(float newValue, float speed, float delay = 0, Action completionAnimation = null)
            {
                this.delay = delay;
                this.speed = speed;
                this.completionAnimation = completionAnimation;
                targetValue = newValue;
                animation = behaviour?.StartCoroutine(AnimationProcess());
            }
    
            internal void Stop()
            {
                behaviour?.StopCoroutine(animation);
            }
    
            private IEnumerator AnimationProcess()
            {
                if (delay > 0f)
                    yield return new WaitForSeconds(delay);
    
                bool isAdditive = startvalue < targetValue;
                while (isAdditive ? (startvalue < targetValue) : (startvalue > targetValue))
                {
                    if (isAdditive)
                    {
                        float newValue = startvalue + speed;
                        startvalue = newValue > targetValue ? targetValue : newValue; 
                    }
                    else
                    {
                        float newValue = startvalue - speed;
                        startvalue = newValue < targetValue ? targetValue : newValue;
                    }
                    material.SetFloat(propertyName, startvalue);
                    yield return new WaitForSeconds(0.02f);
                }
                completionAnimation?.Invoke();
            }
        }
    
        public static AnimationRunner BuildAnimation(MonoBehaviour behaviour, Material material, string propertyName)
        {
            return material == null
                ? null 
                : new AnimationRunner(behaviour, material, propertyName);
        }
    }
    

    you can call that in this way:

    UIShaderAnimatorUtils
    .BuildAnimation(this, card.GetComponentInChildren<Image>().material, "_Burn_Value_1")
    .Start(1f, 0.05f, 0, () => /*do something after animation complete */);

    This my utils support only float params but can be extended for other purpuose :)

    If unity inspector can see property field and not only variables would have been better. in this way it was enough to animate the property from the editor of unity which in turn changed the variable of the material, and instead we are here to reinvent the wheel


 

Looks like your connection to Vetasoft Assets was lost, please wait while we try to reconnect.