- Home /
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);
}
}
}
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.
Answer by kpatelPro · Oct 02, 2015 at 06:13 PM
You can specify default shaders for components via the editor.
Declare a serialized variable for the shader in your component script code, i.e.: public Shader m_Shader;
Select your script in Unity Editor Project Panel
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
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?
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 :)
I actually don't know... We haven't built .exe yet. I'll get back to this question after we do.