Search Unity

[Solved]Control dissolve timer ?

Discussion in 'Scripting' started by Quast, Jan 12, 2019.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Hi.
    I'm trying to access my shader material to my script to control dissolve timer.


    https://docs.unity3d.com/ScriptReference/Shader.SetGlobalFloat.html

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class uuyyy : MonoBehaviour
    7. {
    8.     public Shader myMeshShader;
    9.     public float shadertime = 0; // dessolve value
    10.    
    11.     void Start()
    12.     {
    13.  
    14.         myMeshShader = gameObject.GetComponent<Shader>();
    15.         shadertime = myMeshShader.set ............. // setGlobalFloat !!
    16.                
    17.     }
    18.    
    19. }
    20.  
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Hey Dude,

    Look at this doc for further reference..
    https://docs.unity3d.com/ScriptReference/Material.SetFloat.html

    You want the Material.SetFloat() command.
    If you click the little cog icon in the inspector for the shader and go edit, you will get a list of editable values and somewhere you will find the dissolve value. May look something like _DesolveValue
     
  3. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Let me know if that does not work.
     
  4. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Thank you your respond. actually, you right about the SetFloat. I should use it. Any way Here is the full script.
    Code (CSharp):
    1.    public Material _shader;
    2.     public float timer = 0;
    3.  
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.  
    8.      //   _shader = gameObject.GetComponent<Renderer>().material; // this line will duplicate the mate !! I don't know why ? So, no need to use it
    9.         _shader.SetFloat("Vector1_F92326F1", timer); // How I get the name from shader file..look at image down.
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.        
    17.         if (Input.GetKeyDown(KeyCode.Space))
    18.         {
    19.             timer += 0.01f;
    20.             _shader.SetFloat("Vector1_F92326F1", timer);
    21.         }
    22.     }


    Hope this will help more people like me.
     
  5. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352

    Great!!! Glad you got it to work.

    just to add to it, you can avoid possible errors by adding a clamp to the value. As I would assume the dissovle value is a range of 0-1

    Code (CSharp):
    1. timer += 0.01f;
    2. timer = mathF.Clamp(timer, 0, 1); //Stops the number from going passed 1
    3. _shader.SetFloat("Vector1_F92326F1", timer);
     
    Quast likes this.
  6. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Cool. I will put this in my mind.
    Thank you so much.
     
  7. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Note that you can expose variables from the Shader Graphs, and it's possible to assign different name for them instead of randomized hash.