Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AFV · Oct 02, 2015 at 11:10 AM · scripting problemscripting beginnerscriptingbasics

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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);
         }
     }
 }



Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AFV · Oct 04, 2015 at 05:52 AM 0
Share

@$$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);
 
     }
 }
 




avatar image Kyle_WG AFV · Oct 04, 2015 at 01:51 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges