- Home /
How to check if variable defined in script is present in shader?
Hi,
I have two different shaders for water, first one with MainTexture and BumpMap property, another with MainTexture, BumpMap and DistortionMap property (basically two/three different textures for each shader to create water scene).
I am trying to alter the speed of each of these texture properties through script. The problem is I am trying the script to check if the selected shader has DistortionMap property (which is only present in the second shader) or not and display the option to alter the DistortionMap Speed (in script) accordingly.
I'll be glad if anyone can help here...
Thanks in advance.
Answer by Kyle_WG · Oct 02, 2015 at 11:44 AM
material.HasProperty("_Color");
Usually does the trick. Just replace the string "_Color" with your distortion map variable name.
// Example of checking shader properties.
void SetDistortionSpeedExample(Material _water, float _amount)
{
if(_water != null && _water.HasProperty("_DistortionMap"))
{
if (_water.HasProperty("_DistortionSpeed"))
{
_water.SetFloat("_DistortionSpeed", _amount);
}
}
}
@$$anonymous$$yle_WG Thanks. It works however part of the problem still exists. What I am trying to achieve is if the distortion map property is not present in shader, I want to hide the option to change the speed in script as well.
I am using multiple shaders, each having additional variables which I want to control through script. Above example will check if the variable property is present in shader or not but I am not able to hide the option from script as well since all variable are defined as public variables.
Here's the code;
using UnityEngine;
using System.Collections;
[ExecuteInEdit$$anonymous$$ode]
public class Water : $$anonymous$$onoBehaviour {
#region public data
public Vector2 WaterTextureSpeed = new Vector2(0.01F, 0.01F);
public Vector2 WaterBumpmapSpeed = new Vector2(0.01F, 0.01F);
public Vector2 WaterDistortionmapSpeed = new Vector2(0.01F, 0.01F);
private Vector2 textureUV;
private Vector2 bump$$anonymous$$apUV;
private Vector2 distorion$$anonymous$$apUV;
#endregion
void Start() {
}
void Update () {
textureUV.x = Time.time * WaterTextureSpeed.x;
textureUV.y = Time.time * WaterTextureSpeed.y;
bump$$anonymous$$apUV.x = Time.time * WaterBumpmapSpeed.x;
bump$$anonymous$$apUV.y = Time.time * WaterBumpmapSpeed.y;
distortion$$anonymous$$apUV.x = Time.time * WaterDistortionmapSpeed.x;
distortion$$anonymous$$apUV.y = Time.time * WaterDistortionmapSpeed.y;
GetComponent().shared$$anonymous$$aterial.SetTextureOffset("_$$anonymous$$ainTex", textureUV);
GetComponent().shared$$anonymous$$aterial.SetTextureOffset("_Bump$$anonymous$$apTex", bump$$anonymous$$apUV);
if (renderer.shared$$anonymous$$aterial.HasProperty("_Distortion$$anonymous$$ap"))
GetComponent<Renderer>().shared$$anonymous$$aterial.SetTextureOffset("_Distortion$$anonymous$$ap", distortion$$anonymous$$apUV);
}
}
You'll need a custom editor to tell it that you want to display the GUI control under a certain condition. $$anonymous$$ark the public variables you want to manually display with [HideInInspector].
This tells Unity not to display the default control for the property.
[CustomEditor(typeof(Water))]
public class WaterEditor : Editor
{
override void OnInspectorGUI()
{
// mixed pseudo
Water water = target as Water;
// check if water has the material property
if (water.material.HasProperty())
{
water.distortionSpeed = EditorGUILayout.Slider(water.distortionSpeed,$$anonymous$$, max);
}
if (GUI.changed)
{
EditorUtility.SetDirty(water);
}
}
Your answer
Follow this Question
Related Questions
how to allow the key to only open 1 door rather than all of them? 0 Answers
Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer
store many GameObjects 2 Answers
creating and using list of prefabs 2 Answers
Is there a way to see what scripts are no longer being used in a Unity2D project? 0 Answers