Here is my version of how it could be done. i'm going to use this in my current project.Thank you for your idea.But I have one question is,whether this way will make the drawcall rise?Becase if there are too many sprites in atlas,there will be too many materials.
UIShaderSprite.cs:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class UIShaderSprite : UISprite { Material mMaterial; [HideInInspector][SerializeField] Shader mShader; // cache materials static Dictionary<int, Dictionary<int, Material>> atlasMaterials = new Dictionary<int, Dictionary<int, Material>>(); public override Material material { get { if (mShader != null) { if (mMaterial == null || mChanged) { Dictionary<int, Material> shaderMaterials; if (!atlasMaterials.TryGetValue(atlas.GetInstanceID(), out shaderMaterials)) if (!shaderMaterials.TryGetValue(mShader.GetInstanceID(), out mMaterial) || mMaterial == null) shaderMaterials[mShader.GetInstanceID()] = mMaterial = new Material(base.material) {shader = mShader}; } return mMaterial; } return base.material; } } }
Editor/UIShaderSpriteInspector.cs:
using UnityEngine; using UnityEditor; using System.Collections; [CanEditMultipleObjects] public class UIShaderSpriteInspector : UISpriteInspector { protected override bool ShouldDrawProperties() { NGUIEditorTools.DrawProperty("Shader", serializedObject, "mShader", GUILayout.MinWidth(20f)); return base.ShouldDrawProperties(); } }
See UIShaderSprite for the working copy of this thread.Awesome,Thank you so much.
You can find it on the following github repository:
https://github.com/kimsama/Unity-NGUIExtension
As my experiments, it needs to call panel.RebuildAllDrawCalls() to correctly apply a new shader which is differenct to what NGUI applies in UIDrawCall.UpdateMaterial() call.