context: using UnityEngine;
  using System.Collections;
  
  [AddComponentMenu("2D Toolkit/Sprite/tk2dSlicedSprite")]
  [RequireComponent(typeof(MeshRenderer))]
  [RequireComponent(typeof(MeshFilter))]
  [ExecuteInEditMode]
  /// <summary>
  /// Sprite implementation that implements 9-slice scaling.
  /// </summary>
  public class tk2dSlicedSprite : tk2dBaseSprite
  {
  	Mesh mesh;
  	Vector2[] meshUvs;
  	Vector3[] meshVertices;
  	Color32[] meshColors;
  	Vector3[] meshNormals = null;
  	Vector4[] meshTangents = null;
  	int[] meshIndices;
  	
  	[SerializeField]
  	Vector2 _dimensions = new Vector2(50.0f, 50.0f);
  	[SerializeField]
  	Anchor _anchor = Anchor.LowerLeft;
  	[SerializeField]
  	bool _borderOnly = false;
  
  	[SerializeField]
  	bool legacyMode = false; // purely for fixup in 2D Toolkit 2.0
  	
  	/// <summary>
  	/// Gets or sets border only. When true, the quad in the middle of the
  	/// sliced sprite is omitted, thus only drawing a border and saving fillrate
  	/// </summary>
  	public bool BorderOnly
  	{ 
  		get { return _borderOnly; } 
  		set
  		{
  			if (value != _borderOnly)
  			{
  				_borderOnly = value;
  				UpdateIndices();
  			}
  		}
  	}
  
  
  	/// <summary>
  	/// Gets or sets the dimensions.
  	/// </summary>
  	/// <value>
  	/// Use this to change the dimensions of the sliced sprite in pixel units
  	/// </value>
  	public Vector2 dimensions
  	{ 
  		get { return _dimensions; } 
  		set
  		{
  			if (value != _dimensions)
  			{
  				_dimensions = value;
  				UpdateVertices();
  				UpdateCollider();
  			}
  		}
  	}
  	
  	/// <summary>
  	/// The anchor position for this sliced sprite
  	/// </summary>
  	public Anchor anchor
  	{
  		get { return _anchor; }
  		set
  		{
  			if (value != _anchor)
  			{
  				_anchor = value;
  				UpdateVertices();
  				UpdateCollider();
  			}
  		}
  	}
  	
  	/// <summary>
  	/// Top border in sprite fraction (0 - Top, 1 - Bottom)
  	/// </summary>
  	public float borderTop = 0.2f;
  	/// <summary>
  	/// Bottom border in sprite fraction (0 - Bottom, 1 - Top)
  	/// </summary>
  	public float borderBottom = 0.2f;
  	/// <summary>
  	/// Left border in sprite fraction (0 - Left, 1 - Right)
  	/// </summary>
  	public float borderLeft = 0.2f;
  	/// <summary>
  	/// Right border in sprite fraction (1 - Right, 0 - Left)
  	/// </summary>
  	public float borderRight = 0.2f;
  
  	public void SetBorder(float left, float bottom, float right, float top) {
  		if (borderLeft != left || borderBottom != bottom || borderRight != right || borderTop != top) {
  			borderLeft = left;
  			borderBottom = bottom;
  			borderRight = right;
  			borderTop = top;
  
  			UpdateVertices();
  		}
  	}
  	
  
  	[SerializeField]
  	protected bool _createBoxCollider = false;
  
  	/// <summary>
  	/// Create a trimmed box collider for this sprite
  	/// </summary>
  	public bool CreateBoxCollider {
  		get { return _createBoxCollider; }
  		set {
  			if (_createBoxCollider != value) {
  				_createBoxCollider = value;
  				UpdateCollider();
  			}
  		}
  	}
  
  	new void Awake()
  	{
  		base.Awake();
  		
  		// Create mesh, independently to everything else
  		mesh = new Mesh();
  		mesh.hideFlags = HideFlags.DontSave;
  		GetComponent<MeshFilter>().mesh = mesh;
  	
  		// Cache box collider		
  		if (boxCollider == null) {
  			boxCollider = GetComponent<BoxCollider>();
  		}
  #if !(UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2)
  		if (boxCollider2D == null) {
  			boxCollider2D = GetComponent<BoxCollider2D>();
  		}
  #endif
  
  		// This will not be set when instantiating in code
  		// In that case, Build will need to be called
  		if (Collection)
  		{
  			// reset spriteId if outside bounds
  			// this is when the sprite collection data is corrupt
  			if (_spriteId < 0 || _spriteId >= Collection.Count)
  				_spriteId = 0;
  			
  			Build();
  		}
  	}
  	
  	protected void OnDestroy()
  	{
  		if (mesh)
  		{
  #if UNITY_EDITOR
  			DestroyImmediate(mesh);
  #else
  			Destroy(mesh);
  #endif
  		}
  	}
  
  	new protected void SetColors(Color32[] dest)
  	{
  		tk2dSpriteGeomGen.SetSpriteColors (dest, 0, 16, _color, collectionInst.premultipliedAlpha);
  	}
  	
  	// Calculated center and extents
  	Vector3 boundsCenter = Vector3.zero, boundsExtents = Vector3.zero;
  	
  	protected void SetGeometry(Vector3[] vertices, Vector2[] uvs)
  	{
  		var sprite = CurrentSprite;
  
  		float colliderOffsetZ = ( boxCollider != null ) ? ( boxCollider.center.z ) : 0.0f;
  		float colliderExtentZ = ( boxCollider != null ) ? ( boxCollider.size.z * 0.5f ) : 0.5f;
  		tk2dSpriteGeomGen.SetSlicedSpriteGeom(meshVertices, meshUvs, 0, out boundsCenter, out boundsExtents, sprite, _scale, dimensions, new Vector2(borderLeft, borderBottom), new Vector2(borderRight, borderTop), anchor, colliderOffsetZ, colliderExtentZ);
  
  		if (meshNormals.Length > 0 || meshTangents.Length > 0) {
  			tk2dSpriteGeomGen.SetSpriteVertexNormals(meshVertices, meshVertices[0], meshVertices[15], sprite.normals, sprite.tangents, meshNormals, meshTangents);
  		}
  
  		if (sprite.positions.Length != 4 || sprite.complexGeometry)
  		{
  			for (int i = 0; i < vertices.Length; ++i)
  				vertices[i] = Vector3.zero;
  		}
  	}
  	
  	void SetIndices()
  	{
  
gt: 		int n = _borderOnly ? (8 * 6) : (9 * 6);