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 kamil.slawicki · Jun 30, 2011 at 08:55 AM · shadereditorguilayoutimage-effects

Assigning default shader to a script

Hi guys. I wanted to ask how to assign a default shader to a script. The grayscale effect always uses the grayscale shader. The user does not have to set it anywhere. Every time grayscale is applied to the camera it uses the right shader. Now I have my custom Image Effect script that uses my custom shader. But every time I want to apply my shader to the camera I have to manually select an appropriate shader to the script. It sounds fine so far but I actually need to made some changes to the EditorGUILayout so I'm overwriting the editor with my own stuff. It would be perfect if I could make the shader be set automatically and the sellection box hidden from the user. If this is not possible how do I add shader selection box to my Editor GUI. Mind that I don't have a variable called shader in my script. Here's the script code :

 using UnityEngine;

 [ExecuteInEditMode]
 [AddComponentMenu("Image Effects/HSL")]
 public class HSLControl : ImageEffectBase
 {
     public float hue = 0.0f;
     public float saturation = 0.0f;
     public float lightness = 0.0f;
 
     public float redSaturation = 0.0f;
     public float greenSaturation = 0.0f;
     public float blueSaturation = 0.0f;
 
 
     // Called by camera to apply image effect
     void OnRenderImage(RenderTexture source, RenderTexture destination)
     {
         material.SetFloat("_Hue", hue);
         material.SetFloat("_Saturation", saturation);
         material.SetFloat("_Lightness", lightness);
         material.SetFloat("_RedSaturation", redSaturation);
         material.SetFloat("_GreenSaturation", greenSaturation);
         material.SetFloat("_BlueSaturation", blueSaturation);
         Graphics.Blit(source, destination, material);
     }
 }


And here is the editor code:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomEditor(typeof(HSLControl))]
 public class HSLControlEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         //base.OnInspectorGUI();
         ((HSLControl)target).hue = EditorGUILayout.Slider("Hue", ((HSLControl)target).hue, 0.0f, 1.0f);
         ((HSLControl)target).saturation = EditorGUILayout.Slider("Saturation", ((HSLControl)target).saturation, -1.0f, 1.0f);
         ((HSLControl)target).lightness = EditorGUILayout.Slider("Lightness", ((HSLControl)target).lightness, -1.0f, 1.0f);
         ((HSLControl)target).redSaturation = EditorGUILayout.Slider("R Saturation", ((HSLControl)target).redSaturation, -1.0f, 1.0f);
         ((HSLControl)target).greenSaturation = EditorGUILayout.Slider("G Saturation", ((HSLControl)target).greenSaturation, -1.0f, 1.0f);
         ((HSLControl)target).blueSaturation = EditorGUILayout.Slider("B Saturation", ((HSLControl)target).blueSaturation, -1.0f, 1.0f);
 
 
         if (GUI.changed)
         {
             EditorUtility.SetDirty((HSLControl)target);
         }
     }
 }



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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Waz · Jun 30, 2011 at 01:42 PM

You can just refer to it by name:

 material.shader = Shader.Find("Effect/HSL");

But if you do, make sure you put it in a Resource folder, because the above does not count as a reference for build purposes.

Comment
Add comment · 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
2

Answer by kpatelPro · Oct 02, 2015 at 06:13 PM

You can specify default shaders for components via the editor.

  1. Declare a serialized variable for the shader in your component script code, i.e.: public Shader m_Shader;

  2. Select your script in Unity Editor Project Panel

  3. Specify your default shader in Unity Editor Inspector Panel

Notes:

  • Your default will only be applied to new copies of the component. If you have pre-existing copies of the component you will need to fix them up manually.

  • An extra bonus of doing this is that the shader you choose will automatically be included in builds without having to put it in the Resources folder (assuming the component itself is actually used somewhere).

  • If you are using Version Control you will need to have your script's meta file open for edit or the shader field will be greyed out in the Inspector

Comment
Add comment · 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
0

Answer by Waz · Jun 30, 2011 at 01:47 PM

But since you have a custom editor (and so don't have to show it if you don't want to), why not just add the variable and set it on the script in the Project view?

Comment
Add comment · Show 3 · 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 kamil.slawicki · Jun 30, 2011 at 01:56 PM 0
Share

well I did it differently I think but I'm not sure if it affects the performance at all... In my script, OnRenderImage, I call "shader = Shader.Find("Hidden/HSB");" and it does the job. I looked at the profiler and there is no performance difference comparing to setting the shader manually id debug mode. You said it has to be in the Resource folder, do you mean the physical location of the shader file? Now it's sitting in my Shaders folder and seams to be working fine. I'm new to unity so excuse me if I'm not fully fallowing your idea :)

avatar image Waz · Jun 30, 2011 at 02:39 PM 0
Share

Does it still work when you build an exe?

avatar image kamil.slawicki · Jun 30, 2011 at 03:01 PM 0
Share

I actually don't know... We haven't built .exe yet. I'll get back to this question after we do.

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 does the editor know what shader to use when adding an ImageEffect script? 1 Answer

Edge detect color 2 Answers

Documentation on the bloom filter 0 Answers

Different between tex2D and SAMPLE_TEXTURE2D (Post Processing effect) 0 Answers

Image-Effect Shading the GUI? 1 Answer


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