static void CreateMapMagic () 
		{
			if (FindObjectOfType<MapMagic>() != null)
			{
				Debug.LogError("Could not create new Map Magic instance, it already exists in scene.");
				return;
			}

			GameObject go = new GameObject();
			go.name = "Map Magic";
			MapMagic.instance = go.AddComponent<MapMagic>();

			//new terrains
			MapMagic.instance.chunks = new ChunkGrid<Chunk>();
			MapMagic.instance.seed=12345; MapMagic.instance.terrainSize=1000; MapMagic.instance.terrainHeight=300; MapMagic.instance.resolution=512;
			MapMagic.instance.chunks.Create(new Coord(0,0), MapMagic.instance, pin:true);
			//MapMagic.instance.terrains.maxCount = 5;

			//creating initial generators
			MapMagic.instance.gens = GeneratorsAsset.Default();
			//MapMagic.instance.guiGens = MapMagic.instance.gens;

			//registering undo
			MapMagic.instance.gens.OnBeforeSerialize();
			Undo.RegisterCreatedObjectUndo (go, "MapMagic Create");
			EditorUtility.SetDirty(MapMagic.instance);

			MapMagicWindow.Show(MapMagic.instance.gens, MapMagic.instance, forceOpen:false, asBiome:false);

			/*HeightOutput heightOut =  new HeightOutput();
			heightOut.guiRect = new Rect(43,76,200,20);
			MapMagic.instance.generators.array[1] = heightOut;
			heightOut.input.Link(noiseGen.output, noiseGen);*/
			
		}
		public override void  OnInspectorGUI ()
		{
			script = (MapMagic)target;
			if (MapMagic.instance == null) MapMagic.instance = script;
			script.terrains.CheckEmpty();
			
			if (layout == null) layout = new Layout();
			layout.margin = 0;
			layout.field = Layout.GetInspectorRect();
			layout.cursor = new Rect();
			layout.undoObject = script;
			layout.undoName =  "MapMagic settings change";

			layout.Par(20); bool modeNailing = layout.CheckButton(selectionMode == SelectionMode.nailing, "Pin Terrain", rect:layout.Inset(0.5f), icon:"MapMagic_PinIcon");
				if (layout.lastChange && modeNailing) selectionMode = SelectionMode.nailing;
				if (layout.lastChange && !modeNailing) selectionMode = SelectionMode.none;
			bool modeLocking = layout.CheckButton(selectionMode == SelectionMode.locking, "Lock Terrain", rect:layout.Inset(0.5f), icon:"MapMagic_LockIcon");
				if (layout.lastChange && modeLocking) selectionMode = SelectionMode.locking;
				if (layout.lastChange && !modeLocking) selectionMode = SelectionMode.none;

			layout.Par(4);
			layout.Par(24); if (layout.Button("Show Editor", rect:layout.Inset(), icon:"MapMagic_EditorIcon"))
			{
				MapMagicWindow window = (MapMagicWindow)EditorWindow.GetWindow (typeof (MapMagicWindow));
				//SceneMagicWindow window = EditorWindow.GetWindow<VoxelandCreate>();
				//window.script = script;
				window.Show();
//				window.FocusOnOutput();
			}

//			layout.ComplexField(ref MapMagic.instance.seed, "Seed");
//			layout.ComplexSlider(script.terrains.terrainSize, "Terrain Size", max:2048, quadratic:true);
//			layout.ComplexSlider(ref script.terrainHeight, "Terrain Height", max:2048, quadratic:true);

//			layout.Par();

			
//			layout.Par(); if (layout.Button("Generate")) { MapMagic.instance.terrains.start = true; script.ProcessThreads(); }
//			layout.Par(); if (layout.Button("Clear")) MapMagic.instance.generators.ClearGenerators();

//			Undo.RecordObject (script, "MapMagic settings change");
			layout.margin =10;

			layout.fieldSize = 0.4f;
			layout.Par(5); layout.Foldout(ref script.guiSettings, "General Settings");
			if (script.guiSettings)
			{
				MapMagic.instance.resolution = (int)layout.Field((Pots)MapMagic.instance.resolution, "Resolution");
				if (layout.lastChange && MapMagic.instance.instantGenerate) MapMagic.instance.ForceGenerate();
				layout.Field(ref MapMagic.instance.terrainSize, "Terrain Size");
				if (layout.lastChange) MapMagic.instance.terrains.Reset();
				layout.Field(ref MapMagic.instance.terrainHeight, "Terrain Height");
				if (layout.lastChange) MapMagic.instance.terrains.Reset();

				layout.Par(5);
				layout.Field(ref MapMagic.instance.generateInfinite, "Generate Infinite Terrain");
				if (MapMagic.instance.generateInfinite)
				{
					layout.Field(ref MapMagic.instance.generateRange, "Generate Range");
					layout.Field(ref MapMagic.instance.removeRange, "Remove Range", min:MapMagic.instance.generateRange);
					layout.Field(ref MapMagic.instance.enableRange, "Enable Range");
					//layout.Field(ref MapMagic.instance.terrains.enableRange, "Low Detail Range");
					//layout.Field(ref MapMagic.instance.terrains.detailRange, "Full Detail Range");
				}

				layout.Par(5);
				layout.Field(ref script.multiThreaded, "Multithreaded");
				if (script.multiThreaded) layout.Field(ref script.maxThreads, "Max Threads");
				layout.Field(ref script.instantGenerate, "Instant Generate");
				layout.Field(ref script.saveIntermediate, "Save Intermediate Results");

				layout.Par(5);
				layout.Field(ref script.heightWeldMargins, "Height Weld Margins", max:100);
				layout.Field(ref script.splatsWeldMargins, "Splats Weld Margins", max:100);
				//layout.ComplexField(ref script.hideWireframe, "Hide Wireframe");
				
				layout.Par(5);
				layout.Toggle(ref script.hideFarTerrains, "Hide Out-of-Range Terrains");
				//layout.Toggle(ref script.useAllCameras, "Generate around All Cameras");
				layout.Toggle(ref script.copyLayersTags, "Copy Layers and Tags to Terrains");
				layout.Toggle(ref script.copyComponents, "Copy Components to Terrains");

				layout.Par(5);
				layout.Label("Generate Terrain Markers:");
				layout.Field(ref script.genAroundMainCam, "Around Main Camera");
				
				layout.Par(); layout.Field(ref script.genAroundObjsTag, "Around Objects Tagged", rect:layout.Inset());
				int tagFieldWidth = (int)(layout.field.width*layout.fieldSize - 25);
				layout.cursor.x -= tagFieldWidth;
				script.genAroundTag = EditorGUI.TagField(layout.Inset(tagFieldWidth), script.genAroundTag);

				layout.Par(10);
				layout.Par(); layout.Label("Data", layout.Inset(0.2f));
				GeneratorsAsset newGens = layout.Field<GeneratorsAsset>(script.gens, rect:layout.Inset(0.5f));
				if (newGens != script.gens) { script.gens = newGens; script.guiGens = newGens; }
				if (layout.lastChange) script.gens.OnAfterDeserialize();
				if (script.gens == null)
				{
					if (layout.Button("Create", layout.Inset(0.3f)))
					{
						MapMagic.instance.gens = GeneratorsAsset.Default();
						MapMagic.instance.guiGens = MapMagic.instance.gens;
						EditorUtility.SetDirty(MapMagic.instance); 
					}
				}
                else if (!AssetDatabase.Contains(script.gens))
				{
					if (layout.Button("Save", layout.Inset(0.3f))) 
					{
						MapMagic.instance.gens.SaveAsset();
						EditorUtility.SetDirty(MapMagic.instance); 
					}
				}
				else 
				{
					if (layout.Button("Release", layout.Inset(0.3f))) 
					{ 
						MapMagic.instance.gens = MapMagic.instance.gens.ReleaseAsset(); 
						MapMagic.instance.guiGens = MapMagic.instance.gens;
						EditorUtility.SetDirty(MapMagic.instance); 
					}
				}

				layout.Par(5);
				//layout.Field(ref script.guiGeneratorWidth, "Node Width");
				layout.Toggle(ref script.guiDebug, "Debug");
			}

			layout.fieldSize = 0.5f; layout.sliderSize = 0.6f;
			layout.Par(5); layout.Foldout(ref script.guiTerrainSettings, "Terrain Settings");
			if (script.guiTerrainSettings)
			{
				layout.Field(ref script.pixelError, "Pixel Error", min:0, max:200, slider:true);
				layout.Field(ref script.baseMapDist, "Base Map Dist.", min:0, max:2000, slider:true);
				layout.Field(ref script.castShadows, "Cast Shadows");

				layout.Field(ref script.terrainMaterialType, "Material Type", disabled:script.previewGenerator!=null);
				layout.Field(ref script.customTerrainMaterial, "Custom Material", disabled:script.terrainMaterialType!=MapMagic.TerrainMaterialType.Custom);
				if (script.previewGenerator!=null) layout.Label("Terrain Material is disabled in preview mode", helpbox: true);
			}

			layout.Par(5); layout.Foldout(ref script.guiTreesGrassSettings, "Trees, Details and Grass Settings");
			if (script.guiTreesGrassSettings)
			{
				layout.Field(ref script.detailDraw, "Draw");
				layout.Field(ref script.detailDistance, "Detail Distance", min:0, max:250, slider:true);
				layout.Field(ref script.detailDensity, "Detail Density", min:0, max:1, slider:true);
				layout.Field(ref script.treeDistance, "Tree Distance", min:0, max:5000, slider:true);
				layout.Field(ref script.treeBillboardStart, "Billboard Start", min:0, max:2000, slider:true);
				layout.Field(ref script.treeFadeLength, "Fade Length", min:0, max:200, slider:true);
				layout.Field(ref script.treeFullLod, "Max Full LOD Trees", min:0, max:10000, slider:true);

				layout.Par(5);
				layout.Field(ref script.windSpeed, "Wind Amount", min:0, max:1, slider:true);
				layout.Field(ref script.windSize, "Wind Bending", min:0, max:1, slider:true);
				layout.Field(ref script.windBending, "Wind Speed", min:0, max:1, slider:true); //there's no mistake here. Variable names are swapped in unity
				layout.Field(ref script.grassTint, "Grass Tint");
			}

			if (layout.change) 
				foreach (Chunk tw in MapMagic.instance.terrains.Objects()) tw.SetSettings();


			#region About
			layout.Par(5); layout.Foldout(ref script.guiAbout, "About");
			if (script.guiAbout)
			{
				Rect savedCursor = layout.cursor;
				
				layout.Par(100, padding:0);
				layout.Icon("MapMagicAbout", layout.Inset(100,padding:0));

				layout.cursor = savedCursor;
				layout.margin = 115;

				layout.Label("MapMagic " + (int)(MapMagic.version/10f) + "." + (MapMagic.version - (int)(MapMagic.version/10f)*10));
				layout.Label("by Denis Pahunov");
				
				layout.Par(10);
				layout.Label(" - Online Documentation", url:"https://docs.google.com/document/d/1OX7zYOrPz9qOFNAfO0qawhB7T3M6tLG9VgZSEntRJTA/edit?usp=sharing");
				layout.Label(" - Video Tutorials", url:"https://www.youtube.com/playlist?list=PL8fjbXLqBxvZb5yqXwp_bn4keyzyg5e0R");
				layout.Label(" - Forum Thread", url:"http://forum.unity3d.com/threads/map-magic-a-node-based-procedural-and-infinite-map-generator-for-asset-store.344440/");

				//layout.Par(10);
				//layout.Par(); layout.Label("Review or rating vote on");
				//layout.Par(); layout.Label("Asset Store", url:"--");
				//layout.Par(); layout.Label("would be appreciated.");

				layout.Par(10);
				layout.Label("On any issues related");
				layout.Label("with plugin functioning ");
				layout.Label("you can contact the");
				layout.Label("author by mail:");
				layout.Label("*****@*****.**", url:"mailto:[email protected]");
			}
			#endregion

			Layout.SetInspectorRect(layout.field);
		}