Skip to content

Instantly share code, notes, and snippets.

@JohannesMP
Created April 7, 2017 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohannesMP/93c351390678a0aa33d86b099e35c6f6 to your computer and use it in GitHub Desktop.
Save JohannesMP/93c351390678a0aa33d86b099e35c6f6 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
[ExecuteInEditMode]
#endif
[RequireComponent(typeof(Text), typeof(SpriteMaskingComponent))]
public class ForceUITextMask : MonoBehaviour
{
Text text;
SpriteMaskingComponent link;
private Material mat;
const string SHADER_UI_UNLIT_TEXT = "UI/Unlit/Text";
const string STR_STENCIL_ID = "_Stencil";
const string STR_STENCIL_COMPARISON = "_StencilComp";
private int propertyStencilID;
private int propertyStencilComp;
public int curOwnerInstanceID { get; private set; }
public int curStencilID { get; private set; }
public int curStencilComp { get; private set; }
public int curRenderQueue { get; private set; }
void Reset()
{
mat = null;
curOwnerInstanceID = 0;
}
// Use this for initialization
void Start ()
{
text = GetComponent<Text>();
link = GetComponent<SpriteMaskingComponent>();
var temp = GetComponent<CanvasRenderer>();
propertyStencilID = Shader.PropertyToID(STR_STENCIL_ID);
propertyStencilComp = Shader.PropertyToID(STR_STENCIL_COMPARISON);
updateMaterial();
}
public void OnRenderObject()
{
updateMaterial();
}
public void updateMaterial()
{
// Don't do anything if we have no SpriteMask to refer to
if(link == null || link.owner == null)
{
mat = null;
return;
}
// Reset if owner changed
if(curOwnerInstanceID != link.owner.GetInstanceID())
{
curOwnerInstanceID = link.owner.GetInstanceID();
mat = null;
}
// Make sure we have an updated material
if (mat != text.material)
{
mat = new Material(Shader.Find(SHADER_UI_UNLIT_TEXT));
mat.name += " OWNER_ID: " + curOwnerInstanceID;
text.material = mat;
}
curStencilID = link.owner.stencilId;
mat.SetInt(propertyStencilID, curStencilID);
curStencilComp = 3;
mat.SetInt(propertyStencilComp, curStencilComp);
curRenderQueue = link.owner.maskRenderQueue;
mat.renderQueue = curRenderQueue;
}
void Update () {
}
void OnDestroy()
{
if(mat == text.material)
{
text.material = null;
}
}
#if UNITY_EDITOR
#endif
}
#if UNITY_EDITOR
[CustomEditor(typeof(ForceUITextMask))]
public class ForceUITextMaskEditor : Editor
{
public override void OnInspectorGUI()
{
ForceUITextMask force = (ForceUITextMask)target;
force.updateMaterial();
base.OnInspectorGUI();
string msg = "Instance ID: " + force.curOwnerInstanceID + "\n"
+ "Stencil ID: " + force.curStencilID + "\n"
+ "Stencil Comp: " + force.curStencilComp + "\n"
+ "Render Queue: " + force.curRenderQueue;
EditorGUILayout.HelpBox(msg, MessageType.None);
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment