Change material on Ui image through code

Hi there,

Im sure this is really simple but I cant get it to work and its driving me mad.

I have a blur shader that I want to adjust the blur amount through code using a lerp of some description. Either just lerp the float value or do a material lerp. The blur shader does work with unity UI.

Problem is I can’t figure out how to do it on a UI image. I want to adjust blur amount on a UI image over a set amount of time.

Alternatively I would like to be able to adjust the blur amount through unitys animation system but I cant get that to key frame at all.

Any ideas on how I can do this?

bump?

  1. does the blur work on the ui Image?

If yes:
2. if you have the element (UI Image), assign a material (copy for a local blur or use a single material if the blur is shared)
then simply update the float by using the setFloat of the material

Thanks freezy.

How would I go about lerping the float value of the material though? That’s where i have been getting stuck

        public Material _material;
        [Range(0.01f,5f)]
        public float blurSpeed = 0.5f;
        //Change the string to the shader parameter
        private int shaderBlurID = Shader.PropertyToID("BlurValue");
      
        public float blurLerp {
            get { return _material.GetFloat(shaderBlurID); }
            set { _material.SetFloat(shaderBlurID, value); }
        }

        public System.Collections.IEnumerator currentBlur;

        public void StartBlurIn() {
            if (currentBlur != null) {
                StopCoroutine(currentBlur);
                currentBlur = null;
            }
            currentBlur = BlurIn();
            StartCoroutine(currentBlur);
        }

        public void StartBlurOut() {
            if (currentBlur != null) {
                StopCoroutine(currentBlur);
                currentBlur = null;
            }
            currentBlur = BlurOut();
            StartCoroutine(currentBlur);
        }

        public System.Collections.IEnumerator BlurIn() {
            float lerpval = blurLerp;
            while (lerpval < 1) {
                lerpval += blurSpeed * Time.deltaTime;
                yield return null;
            }
            currentBlur = null;
        }

        public System.Collections.IEnumerator BlurOut() {
            float lerpval = blurLerp;
            while (lerpval > 0) {
                lerpval -= blurSpeed * Time.deltaTime;
                yield return null;
            }
            currentBlur = null;
        }

thank you will try it out tonight