public virtual void ApplyStyle () //re-written
	{	
		//trying to load style if styleSet assigned (usually after scene loading)
		if (!customStyle && styleSet!=null)
		{
			style = styleSet.FindStyle(styleName);
		}
		
		//clearing if no style assigned
		if (style==null) InstantGuiStyle.Clear(this);
		
		else
		{
			//calculating style
			SubStyle subStyle= style.main;
			if (style.disabled.enabled && disabled) subStyle = style.disabled;
			if (style.active.enabled && (check || pressed)) subStyle = style.active;
			
			//calculating pointed blend
			if (style.pointed.enabled && !disabled && !pressed && !check)
			{
				if (pointed) pointedBlend = Mathf.Min(pointedBlend + Time.deltaTime*style.blendSpeed, 1);
				else pointedBlend = Mathf.Max(pointedBlend - Time.deltaTime*style.blendSpeed, 0);
				
				//setting pointed time and blend to zero if in editor
				#if UNITY_EDITOR
				if (!UnityEditor.EditorApplication.isPlaying)
				{
					pointedTime = 0;
					unpointedTime = 0;
					pointedBlend = 0;
				}
				#endif
			}
			
			//apply style
			if (pointedBlend < 0.001f || disabled || pressed || check) { style.Unblend(this); style.Apply(subStyle, this); }
			else if (pointedBlend > 0.999f) { style.Unblend(this); style.Apply(style.pointed, this); }
			else //blended
			{
				style.Apply(subStyle, this);
				style.Blend(style.pointed, this, pointedBlend);
			}
		}
		
		
		
		//recursive
		for (int i=0; i<childElements.Count; i++) childElements[i].ApplyStyle();
	}
	static public Texture GetUsedtexture ( InstantGuiStyle style  ){
		Texture tex=null;
		
		if (style.main.enabled && style.main.texture!=null) tex = style.main.texture;
		if (tex==null && style.pointed.enabled && style.pointed.texture!=null) tex = style.pointed.texture;
		if (tex==null && style.active.enabled && style.active.texture!=null) tex = style.active.texture;
		if (tex==null && style.disabled.enabled && style.disabled.texture!=null) tex = style.disabled.texture;
		
		return tex;
	}
	static public InstantGuiStyle DrawStyle (InstantGuiStyle style, bool drawDefaultPos)
	{
		int indent= EditorGUI.indentLevel;
		EditorGUI.indentLevel=1;

		//EditorGUILayout.HelpBox("Style settings are not available in demo version.", MessageType.Info);

		DrawSubStyle (style.main, "Main");
		DrawSubStyle (style.pointed, "Pointed");
		DrawSubStyle (style.active, "Active");
		DrawSubStyle (style.disabled, "Disabled");
		
		EditorGUILayout.Space();
		
		//texture borders
		EditorGUIUtility.LookLikeControls(15, 15);
		EditorGUI.indentLevel=0;
		Rect rect = GUILayoutUtility.GetRect (50, 44, "TextField");
		rect.x+=indent*indentPixels; rect.width-=indent*indentPixels;
		
		rect.x = 150; rect.width = 75; rect.height = 16;
		style.borders.top = EditorGUI.IntField(rect, "T:", style.borders.top);	
		rect.y+=18; rect.x -=40;
		style.borders.left = EditorGUI.IntField(rect, "L:", style.borders.left);
		rect.x += 80;
		style.borders.right = EditorGUI.IntField(rect, "R:", style.borders.right);
		rect.y+=18; rect.x -=40;
		style.borders.bottom = EditorGUI.IntField(rect, "B:", style.borders.bottom);
		rect.x -=120; rect.y-=36; EditorGUI.LabelField(rect,"Texture"); 
		rect.y+=18; EditorGUI.LabelField(rect,"Borders:");
		rect.y+=18; rect.x=32; rect.width = 60; 
		if (GUI.Button(rect,"Half Size"))
		{
			Texture tex = GetUsedtexture(style);
			if (tex!=null)
			{
				style.borders.top = (int)(tex.height*0.5f);
				style.borders.bottom = (int)(tex.height*0.5f);
				style.borders.left = (int)(tex.width*0.5f);
				style.borders.right = (int)(tex.width*0.5f);
			}
		}
		GUILayoutUtility.GetRect (50, 18, "TextField");
		EditorGUIUtility.LookLikeControls();
		
		//fixed width 
		rect = GUILayoutUtility.GetRect (50, 18, "TextField");
		rect.x+=(indent+1)*indentPixels; rect.width-=(indent+1)*indentPixels;
		EditorGUI.indentLevel = 0;
		int rectwidth= (int)rect.width;
		rect.width = 30;
		style.fixedWidth = EditorGUI.Toggle(rect,style.fixedWidth);
		rect.x+=20; rect.width = rectwidth -45-30;
		style.fixedWidthSize = EditorGUI.IntField(rect, "Fixed Width:", style.fixedWidthSize);
		rect.x += rect.width+5; rect.width = 50;
		if (GUI.Button(rect, "Get"))
		{
			Texture tex = GetUsedtexture(style);
			if (tex!=null) style.fixedWidthSize = tex.width;
		}
		EditorGUI.indentLevel = 1; 
		
		//fixed height 
		rect = GUILayoutUtility.GetRect (50, 18, "TextField");
		rect.x+=(indent+1)*indentPixels; rect.width-=(indent+1)*indentPixels;
		EditorGUI.indentLevel = 0;
		rectwidth = (int)rect.width;
		rect.width = 30;
		style.fixedHeight = EditorGUI.Toggle(rect,style.fixedHeight);
		rect.x+=20; rect.width = rectwidth -45-30;
		style.fixedHeightSize = EditorGUI.IntField(rect, "Fixed Height:", style.fixedHeightSize);
		rect.x += rect.width+5; rect.width = 50;
		if (GUI.Button(rect, "Get"))
		{
			Texture tex = GetUsedtexture(style);
			if (tex!=null) style.fixedHeightSize = tex.height;
		}
		EditorGUI.indentLevel = 1; 

		//proportional
		rect = GUILayoutUtility.GetRect (50, 18, "TextField");
		rect.x+=(indent)*indentPixels; rect.width-=(indent)*indentPixels;
		rect.width -= 100;
		style.proportional = EditorGUI.ToggleLeft(rect, "Proportional", style.proportional);
		rect.x += 100;
		style.proportionalAspect = EditorGUI.FloatField(rect, style.proportionalAspect);

		//pointOffset
		InstantGuiElementPos tmp = new InstantGuiElementPos(style.pointOffset.left, style.pointOffset.right, style.pointOffset.top, style.pointOffset.bottom);
		tmp = InstantGuiInspector.DrawElementPos("ClickOffset:", tmp);
		style.pointOffset = new RectOffset(tmp.left, tmp.right, tmp.top, tmp.bottom);
		
		//other controls
		style.blendSpeed = EditorGUILayout.FloatField("Blend Speed:", style.blendSpeed);
		
		EditorGUILayout.Space();
		style.font = (Font)EditorGUILayout.ObjectField("Font:", style.font, typeof(Font), false);
		style.fontSize = EditorGUILayout.IntField("Font Size:", style.fontSize);
		style.textAligment = (InstantGuiTextAligment)EditorGUILayout.EnumPopup("Text Aligment:", style.textAligment);
		
		Vector2 textOffset = DrawVector2("Text Offset", new Vector2(style.textOffsetX, style.textOffsetY));
		style.textOffsetX = (int)(textOffset.x);
		style.textOffsetY = (int)(textOffset.y);
		
		EditorGUILayout.Space();
		
		//text offset
		if (drawDefaultPos)
		{
			EditorGUILayout.LabelField("Default Placement:");
			InstantGuiInspector.DrawElementPosLabels("", "Left", "Right", "Top", "Bottom");
			style.relative = InstantGuiInspector.DrawElementPos("Relative:", style.relative);
			style.offset = InstantGuiInspector.DrawElementPos("Offset:", style.offset);
			style.layerOffset = DrawLayerOffset("Layer Offset:", style.layerOffset);
		}
		EditorGUILayout.Space();
		
		EditorGUI.indentLevel = indent;

		return style;
	}
	static public void  DrawSaveToStyleButton ( string label ,   InstantGuiElement element ,   bool saveStyle ,    bool savePos  ){
		//if (!saveStyle && savePos && element.customStyle) return;
		
		Rect rect= GUILayoutUtility.GetRect (50, 18, "TextField");
		rect.x=30; rect.width-=15;
		if (GUI.Button(rect, label))
		{
			//finding in set
			bool  foundInSet = false;
			for (int j=0; j<element.styleSet.styles.Length; j++)
				if (element.styleSet.styles[j].name == element.style.name)
			{
				if (saveStyle) element.styleSet.styles[j] = element.style.Clone();
				if (savePos)
				{
					element.styleSet.styles[j].relative = element.relative.Clone();
					element.styleSet.styles[j].offset = element.offset.Clone();
					element.styleSet.styles[j].layerOffset = element.layerOffset;
				}
				foundInSet = true;
			}
			
			//adding new
			if (!foundInSet)
			{
				#if UNITY_EDITOR
				if (!saveStyle) 
				{
					if (EditorUtility.DisplayDialog("No Such Style",
					                                "Cannot find style in Style Set. Do you want to create it?",
					                                "Create", "Cancel")) saveStyle = true;
					else { EditorUtility.SetDirty(element.styleSet); return; }
				}
				#endif
				
				InstantGuiStyle[] newStyles = new InstantGuiStyle[element.styleSet.styles.Length+1];
				for (int j=0; j<element.styleSet.styles.Length; j++) newStyles[j] = element.styleSet.styles[j];
				
				if (saveStyle) newStyles[newStyles.Length-1] = element.style.Clone();
				if (savePos)
				{
					newStyles[newStyles.Length-1].relative = element.relative.Clone();
					newStyles[newStyles.Length-1].offset = element.offset.Clone();
					newStyles[newStyles.Length-1].layerOffset = element.layerOffset;
				}
				element.styleSet.styles = newStyles;
			}
			
			#if UNITY_EDITOR
			EditorUtility.SetDirty(element.styleSet);
			#endif
		}
	}
	public override void  OnInspectorGUI ()
	{

		InstantGuiStyleSet script = (InstantGuiStyleSet)target;
		if (script.styles==null) { script.styles = new InstantGuiStyle[1]; script.styles[0] = new InstantGuiStyle();}

		EditorGUI.BeginChangeCheck();

		Undo.RecordObject(script, "InstantGui Style Change");

		Rect rect=new Rect(0,0,0,0);

		if (script.styles!=null)
			for (int i=1; i<script.styles.Length; i++) 
		{
			//if (script.styles[i]==null) script.styles[i] = new InstantGuiStyle();

			EditorGUI.indentLevel = 0;
			rect= GUILayoutUtility.GetRect (50, 18, "TextField");
			
			//name
			rect.x+=10; rect.width-=90;
			script.styles[i].name = EditorGUI.TextField(rect, script.styles[i].name);
			
			//move up
			rect.x = rect.width+30; rect.width=23;
			if (GUI.Button(rect, "∧ ") && i != 1)
			{
				InstantGuiStyle tmp= script.styles[i-1];
				script.styles[i-1] = script.styles[i];
				script.styles[i] = tmp;
				EditorUtility.SetDirty(script);
			}
			
			//move down
			rect.x += 25;
			if (GUI.Button(rect, "∨ ") && i != script.styles.Length-1)
			{
				InstantGuiStyle tmp = script.styles[i+1];
				script.styles[i+1] = script.styles[i];
				script.styles[i] = tmp;
				EditorUtility.SetDirty(script);
			}
			
			//delete
			rect.x += 25;
			if (GUI.Button(rect, "✕ ") && EditorUtility.DisplayDialog("Remove Style?",
			                                                          "Are you sure you want to remove style?",
			                                                          "OK", "Cancel"))
			{
				InstantGuiStyle[] newStyles = new InstantGuiStyle[script.styles.Length-1];
				int counter = 0;
				for (int j=0; j<script.styles.Length; j++)
				{
					if (j != i)
					{
						newStyles[counter] = script.styles[j];
						counter++;
					}
				}
				script.styles = newStyles;
				EditorUtility.SetDirty(script);
			}
			if (i==script.styles.Length) break;
			
			//foldout
			rect.x=20; rect.width=20;
			script.styles[i].show = EditorGUI.Foldout(rect, script.styles[i].show, "");
			
			
			//display style
			if (script.styles[i].show) InstantGuiInspector.DrawStyle(script.styles[i], true);
		}
		
		//adding
		rect = GUILayoutUtility.GetRect (50, 18, "TextField");
		rect.x=rect.width-70; rect.width=73;
		if (GUI.Button(rect, "Add"))
		{
			InstantGuiStyle[] newStyles = new InstantGuiStyle[script.styles.Length+1];
			for (int j=0; j<script.styles.Length; j++) newStyles[j] = script.styles[j];
			newStyles[newStyles.Length-1] = new InstantGuiStyle();
			script.styles = newStyles;
			EditorUtility.SetDirty(script);
		}
		
		if (EditorGUI.EndChangeCheck ()) 
		{
			EditorUtility.SetDirty (target);
			if (InstantGui.instance!=null) InstantGui.instance.Update();
		}
		//else Undo.ClearUndo(script);
	}