Search Unity

Change material on Ui image through code

Discussion in 'UGUI & TextMesh Pro' started by will_brett, Aug 9, 2015.

  1. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    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?
     
  2. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
  3. Freezy

    Freezy

    Joined:
    Jul 15, 2012
    Posts:
    234
    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
     
  4. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Thanks freezy.

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

    Freezy

    Joined:
    Jul 15, 2012
    Posts:
    234
    Code (CSharp):
    1.  
    2.         public Material _material;
    3.         [Range(0.01f,5f)]
    4.         public float blurSpeed = 0.5f;
    5.         //Change the string to the shader parameter
    6.         private int shaderBlurID = Shader.PropertyToID("BlurValue");
    7.      
    8.         public float blurLerp {
    9.             get { return _material.GetFloat(shaderBlurID); }
    10.             set { _material.SetFloat(shaderBlurID, value); }
    11.         }
    12.  
    13.         public System.Collections.IEnumerator currentBlur;
    14.  
    15.         public void StartBlurIn() {
    16.             if (currentBlur != null) {
    17.                 StopCoroutine(currentBlur);
    18.                 currentBlur = null;
    19.             }
    20.             currentBlur = BlurIn();
    21.             StartCoroutine(currentBlur);
    22.         }
    23.  
    24.         public void StartBlurOut() {
    25.             if (currentBlur != null) {
    26.                 StopCoroutine(currentBlur);
    27.                 currentBlur = null;
    28.             }
    29.             currentBlur = BlurOut();
    30.             StartCoroutine(currentBlur);
    31.         }
    32.  
    33.         public System.Collections.IEnumerator BlurIn() {
    34.             float lerpval = blurLerp;
    35.             while (lerpval < 1) {
    36.                 lerpval += blurSpeed * Time.deltaTime;
    37.                 yield return null;
    38.             }
    39.             currentBlur = null;
    40.         }
    41.  
    42.         public System.Collections.IEnumerator BlurOut() {
    43.             float lerpval = blurLerp;
    44.             while (lerpval > 0) {
    45.                 lerpval -= blurSpeed * Time.deltaTime;
    46.                 yield return null;
    47.             }
    48.             currentBlur = null;
    49.         }
     
  6. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    thank you will try it out tonight