Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Expose shader properties to animator

Discussion in 'Shaders' started by tmcthee, May 10, 2019.

  1. tmcthee

    tmcthee

    Joined:
    Mar 8, 2013
    Posts:
    82
    Hi,
    I have a bunch of custom shaders that I want to be able to control using an animator controller and animation clips. Is this possible?

    I've tried adding an animation controller/clip to the object with the shader/material applied to it, but when I go to the animation dope sheet and try to add a shader property to the animation it doesn't seem to be available.

    For example I have a shader with a blur effect that is controlled by a slider called "magnitude". When I go to the animation window, I can see the "standard" properties to add, like colour, for example. But my own properties are missing.

    Is there something I need to add to the properties to make them exposed to the animator?
    Thanks
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,736
    You may create custom script for that with properties you want.
    Code (CSharp):
    1.  
    2. public float myCustomFloat {
    3.    get { return GetComponent<MeshRenderer>().sharedMaterial.getFloat("myCustomFloat"); }
    4.    set { GetComponent<MeshRenderer>().sharedMaterial.setFloat("myCustomFloat", value); }
    5. }
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    8,975
    What version of Unity are you using? What kind of component are you looking to animate? How are you creating the animation?

    In 2018.2 if I select a game object with a mesh renderer component, go to the Animation tab and create a new clip, I can see all of the properties of the material listed in the Properties block of the shader. Just for an example, here's a custom circular bar shader with a lot of non-standard property names. They all show up.
    upload_2019-5-13_8-32-25.png
    This works in 2019, and worked in 2017 and all versions of Unity 5.
     
    palex-nx likes this.
  4. xVictor

    xVictor

    Joined:
    Jul 3, 2014
    Posts:
    2
    What about shaders assigned to UI elements? They don't show in animator, apparently.
     
  5. Pixelome

    Pixelome

    Joined:
    Sep 30, 2014
    Posts:
    11
    I'm running into the same issue as @xVictor.

    For a UI element the material properties are not showing up in the animator. Even if I right click on a property I don't get the "add keyframe" option.

    As a workaround I'll trigger an animation event and animate from there, but that breaks the preview, and complicates the animation process.
     
  6. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    865
    Yes it doesn't work for UI :s

    Anyone has a solution?
     
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,736
  8. steril

    steril

    Joined:
    Nov 30, 2014
    Posts:
    1
  9. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,736
    For animations use this code

    Code (CSharp):
    1. [SerializeField] private float _myCustomFloat;
    2.  
    3. private float __myCustomFloatActualValue;
    4.  
    5. private void Awake() {
    6.     __myCustomFloatActualValue = GetComponent<MeshRenderer>().sharedMaterial.getFloat("myCustomFloat");
    7. }
    8.  
    9. private void LateUpdate() {
    10.     if(_myCustomFloat != __myCustomFloatActualValue) {
    11.         __myCustomFloatActualValue = _myCustomFloat;
    12.         GetComponent<MeshRenderer>().sharedMaterial.setFloat("myCustomFloat", __myCustomFloatActualValue)  
    13.     }
    14. }
    Do not forget to add [ExecuteAlway] to make your script work in edit mode too.
     
unityunity