Last active
January 19, 2018 02:27
-
-
Save kimsama/7b81fffb0f153b7cbd71 to your computer and use it in GitHub Desktop.
NGUI Tip: A way to apply a shader on one sprite in an atlas. Find UISprite.material property in the UISprite.cs then sustitue it with the following code. It works on NGUI 3.7.0 or higher version.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //public override Material material | |
| //{ | |
| // get { | |
| // return (mAtlas != null) ? mAtlas.spriteMaterial : null; | |
| // } | |
| //} | |
| [HideInInspector][SerializeField] | |
| Shader mShader; | |
| Material mMaterial; | |
| // cache materials | |
| static Dictionary<int, Dictionary<int, Material>> mCachedAtlasMaterials = new Dictionary<int, Dictionary<int, Material>>(); | |
| /// <summary> | |
| /// Substitute UISprite's material to dynamically change a shader of the given atlas material. | |
| /// </summary> | |
| public override Material material | |
| { | |
| get | |
| { | |
| if (mShader != null) | |
| { | |
| if (mMaterial == null || mChanged) | |
| { | |
| Dictionary<int, Material> shaderMaterials; | |
| if (!mCachedAtlasMaterials.TryGetValue(atlas.GetInstanceID(), out shaderMaterials)) | |
| mCachedAtlasMaterials[atlas.GetInstanceID()] = shaderMaterials = new Dictionary<int, Material>(); | |
| if (!shaderMaterials.TryGetValue(mShader.GetInstanceID(), out mMaterial) || mMaterial == null) | |
| { | |
| if (mAtlas != null) | |
| shaderMaterials[mShader.GetInstanceID()] = mMaterial = new Material(mAtlas.spriteMaterial) { shader = mShader}; | |
| } | |
| } | |
| return mMaterial; | |
| } | |
| return (mAtlas != null) ? mAtlas.spriteMaterial : null; | |
| } | |
| } | |
| /// <summary> | |
| /// Specified sprite to change material with the given shader so enables a shader on each of a sprite. | |
| /// Pass null if it needs to be restored and NGUI handles a material. | |
| /// </summary> | |
| public void SetShader (Shader shd) | |
| { | |
| mShader = shd; | |
| panel.RebuildAllDrawCalls(); // need to force redraw to prevent not applying the shader on the sprite. | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// It may need to add the following line the shader to be shown on the inspector view. | |
| /// </summary> | |
| protected override bool ShouldDrawProperties () | |
| { | |
| // add this line | |
| ShaderMenuUtility.ShaderField("Shader", serializedObject, "mShader", GUILayout.MinWidth(20f)); | |
| GUILayout.BeginHorizontal(); | |
| ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment