context: using UnityEngine;
  using System.Collections;
   
  /// <summary>
  /// Provides functionality to use static sprites (images) to your scenes.
  /// </summary>
  public class OTSprite : OTObject
  {		
      //-----------------------------------------------------------------------------
      // Editor settings
      //-----------------------------------------------------------------------------
      
      public bool _flipHorizontal = false;
      
      public bool _flipVertical = false;
      
      public bool _transparent = true;
      
      public bool _additive = false;
      
      public string _materialReference = "transparent";
  
  	public Color _tintColor = Color.white;
      
      public float _alpha = 1.0f;
      
      public Texture _image = null;
   
  	/// <summary>
  	/// The size images for 'in-memory' multi resolution support
  	/// </summary>
  	public OTContainerSizeTexture[] sizeImages;
  		
      public int _frameIndex = 0;
  	
      public string _frameName = "";
      
      public OTContainer _spriteContainer;	
  
      //-----------------------------------------------------------------------------
      // public attributes (get/set)
      //-----------------------------------------------------------------------------
  
      /// <summary>
      /// Flips sprite image horizontally
      /// </summary>
      public bool flipHorizontal
      {
          get
          {
              return _flipHorizontal;
          }
          set
          {
  			if (value!=_flipHorizontal)
  			{
  	            _flipHorizontal = value;
  	            meshDirty = true;
  	            _flipHorizontal_ = _flipHorizontal;
  	            Update();
  			}
          }
      }
  
      /// <summary>
      /// Flips sprite image verically
      /// </summary>
      public bool flipVertical
      {
          get
          {
              return _flipVertical;
          }
          set
          {
  			if (value!=_flipVertical)
  			{
  	            _flipVertical = value;
  	            meshDirty = true;
  	            _flipVertical_ = _flipVertical;
  	            Update();
  			}
          }
      }
  	
      /// <summary>
      /// Sprite needs transparency support
      /// </summary>
      public bool transparent
      {
          get
          {
              return _transparent;
          }
          set
          {
              _transparent = value;
  			Clean();
          }
      }
      /// <summary>
      /// Sprite needs additive transparency support
      /// </summary>
      public bool additive
      {
          get
          {
              return _additive;
          }
          set
          {
              _additive = value;
  			Clean();
          }
      }
  
      /// <summary>
      /// Current texture of the sprite (image or spriteContainer)
      /// </summary>
      public Texture texture
      {
          get
          {
              if (spriteContainer != null)
                  return spriteContainer.GetTexture();
              else
                  return image;
          }
  		set
  		{
  			material.mainTexture = value;
  			_image = value;
  			_image_ = _image;
  		}
      }
  
      /// <summary>
      /// Default image texture for this sprite.
      /// </summary>
      public Texture image
      {
          get
          {
              return _image;
          }
          set
          {
  			if (_image!=value)
  			{
  	            _image = value;
  				_spriteContainer = null;
  				_defaultImage = null;
  				CheckSettings();
  				Clean();
  				Invoke("Reset",0.1f);
  			}
          }
      }
  	
      /// <summary>
      /// Sprite Container that will provide image information for this sprite
      /// </summary>
      public OTContainer spriteContainer
      {
          get
          {
              return _spriteContainer;
          }
          set
          {
  			if (_spriteContainer!=value)
  			{
  	            _spriteContainer = value;
  				_containerName = "";
  				Clean();
  				Invoke("Reset",0.1f);
  			}
          }
      }
  
  	/// <summary>
      /// Index of the frame that is used to get image information from the Sprite Container
      /// </summary>
      public int frameIndex
      {
          get
          {
              return _frameIndex;
          }
          set
          {
  			if (_frameIndex!=value)
  			{
  	            _frameIndex = value;
  				_frameName = "";
  				_frameName_ = "";
  				Clean();
  			}
          }
      }
  	
  	/// <summary>
      /// name of the frame that is used to get image information from the Sprite Container
      /// </summary>
      public string frameName
      {
          get
          {
              return _frameName;
          }
          set
          {			
              _frameName = value;
  			Clean();
          }
      }
  
      
      public Material material
      {
          get
          {
  			if (otRenderer == null)
  				otRenderer = renderer;
  			
              if (Application.isPlaying)
                  return otRenderer.material;
              else
                  return otRenderer.sharedMaterial;
          }
          set
          {
  			if (otRenderer == null)
  				otRenderer = renderer;
  			
              assignedMaterial = true;
              if (Application.isPlaying)
                  otRenderer.material = value;
              else
                  otRenderer.sharedMaterial = value;
          }
      }
  
      /// <summary>
      /// Reference name of material for this sprite
      /// </summary>
      public string materialReference
      {
          get
          {
              return _materialReference;
          }
          set
          {
              _materialReference = value;
  			Clean();
          }
      }
  
      /// <summary>
      /// Tinting color of this sprite.
      /// </summary>
      /// <remarks>
      /// This setting will only work if this sprite's materialReference can work with color tinting.
      /// </remarks>
      public Color tintColor
      {
          get
          {
              return _tintColor;
          }
          set
          {
              _tintColor = value;
  			Clean();
          }
      }
      /// <summary>
      /// Alpha channel for this sprite.
      /// </summary>
      /// <remarks>
      /// This setting will only work if this sprite's materialReference can work with alpha channels/color.
      /// </remarks>
      public float alpha
      {
          get
          {
              return _alpha;
          }
          set
          {
              _alpha = value;
  			Clean();
          }
      }
  	
  	
  	[HideInInspector]
  	public string _containerName = "";
  	[HideInInspector]
  	public bool _newSprite = true;
  	
      //-----------------------------------------------------------------------------
      // protected and private  fields
      //-----------------------------------------------------------------------------
      protected OTContainer _spriteContainer_ = null;
      int _frameIndex_ = 0;
      string _frameName_ = "";
      bool _flipHorizontal_ = false;
      bool _flipVertical_ = false;
      protected Texture _image_ = null;
      bool _transparent_ = true;
      Color _tintColor_ = Color.white;
      float _alpha_ = 1;
      bool _additive_ = false;
      string _materialReference_ = "transparent";
      string lastMatName = "";
      Material lastMat = null;
      OTMatRef mr;
      bool assignedMaterial = false;
  	bool spriteInvalid  = false;
  	
      //-----------------------------------------------------------------------------
      // public methods
      //-----------------------------------------------------------------------------
  
      /// <summary>
      /// Retrieve frame data of the sprite's current frame. This data will include the
      /// texture scale, texture offset and uv coordinates that are needed to get the
      /// current frame's image.
      /// </summary>
      /// <returns>frame data of sprite's current frame</returns>
      public OTContainer.Frame CurrentFrame()
      {
  
gt:         if (spriteContainer != null && spriteContainer.isReady)