SHARE
TWEET

SpriteMaskHelper.cs

Powerhoof Oct 6th, 2016 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using ToJ;
  4.  
  5. public class SpriteMaskHelper : MonoBehaviour
  6. {
  7.     static readonly string SHADER_ALPHA_MASK_SPRITE = "Alpha Masked/Sprites Alpha Masked - World Coords";
  8.     static readonly string SHADER_VAR_USEALPHACHANNEL = "_UseAlphaChannel";
  9.     static readonly string SHADER_VAR_ALPHATEX = "_AlphaTex";
  10.  
  11.     [SerializeField, Tooltip("Set a specific mask, or leave empty to find mask automatically")] Mask m_mask = null;
  12.  
  13.     static Shader s_alphaMaskShader = null;
  14.  
  15.     /// Sets up the sprite to  be masked, and moves it under the sprite object
  16.     public void AddSprite(SpriteRenderer sprite)
  17.     {
  18.         if ( m_mask == null )
  19.             m_mask = GetComponentInChildren<Mask>();
  20.  
  21.         if ( m_mask == null || sprite == null)
  22.             return;
  23.  
  24.         if ( s_alphaMaskShader == null )
  25.             s_alphaMaskShader = Shader.Find(SHADER_ALPHA_MASK_SPRITE);
  26.        
  27.         sprite.material.shader = s_alphaMaskShader;
  28.         m_mask.SetMaskBoolValueInMaterial(SHADER_VAR_USEALPHACHANNEL,true,sprite.material);
  29.         m_mask.SetMaskMappingAxisInMaterial(Mask.MappingAxis.Z,sprite.material);
  30.         sprite.material.SetTexture(SHADER_VAR_ALPHATEX, m_mask.GetComponent<MeshRenderer>().materials[0].mainTexture);
  31.         sprite.transform.SetParent( m_mask.transform.parent, true);    
  32.     }
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top