Example #1
0
		public void Update()
		{
			wantsMouseMove = true;
			
			//Need to force recreate the preview window if transitioning from
			//game to editor or back
			if( _isPlayingInEditor != EditorApplication.isPlaying )
			{
				DisablePreview();
				_shouldOpenPreviewWindow = true;
				_isPlayingInEditor = EditorApplication.isPlaying;
			}
			
			if( _shouldOpenPreviewWindow && PreviewSupported() )
			{
				//Find the preview window
				var types = AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes());
				var previewWindowType = types.FirstOrDefault( x => x.FullName == "StrumpyPreviewWindow" );
				if( previewWindowType != null )
				{
					_previewWindow = GetWindow( previewWindowType, true, "Preview" ) as PreviewWindowInternal;
					if (_previewWindow != null)
					{
						_previewWindow.Initialize(this);
						_previewWindow.wantsMouseMove = true;
						_previewWindow.ShouldUpdate = true;
						_shouldOpenPreviewWindow = false;
					}
				}
			}
			
			//Update preview
			if (_shouldUpdateShader)
			{
				if (_selectedGraph.IsGraphValid())
				{
					File.WriteAllText(_tempShaderPathFull, _selectedGraph.GetShader(_shaderTemplatePath, true));
					var currentShader = Resources.Load(_internalTempUnityPath + TempShaderName) as Shader;
					var path = AssetDatabase.GetAssetPath(currentShader);
					AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
					
					if( _previewWindow != null )
						_previewWindow.PreviewMaterial = new Material(currentShader);
					
					
					_graphNeedsUpdate = false;
					InstructionCounter.CountInstructions(); // Update the instruction count
					CacheCount(); // Solve the tooltip (Cached)
				}
				else
				{
					EditorUtility.DisplayDialog("Save Shader Error", "Cannot update shader, there are errors in the graph", "Ok");
				}
				_shouldUpdateShader = false;
			}
			
			//Save graph
			if (_shouldSaveGraph)
			{
				var path = _quickSaving ? _lastGraphPath : EditorUtility.SaveFilePanel("Save shader graph to file...", _graphsDir, "shader.sgraph", "sgraph");
				if (!string.IsNullOrEmpty(path))
				{
					var writer = new FileStream(path, FileMode.Create);
					var ser = new DataContractSerializer(typeof (ShaderGraph), _serializableTypes, int.MaxValue, false, false, null);
					ser.WriteObject(writer, _selectedGraph);
					writer.Close();
					_lastGraphPath = path;
				}
				_shouldSaveGraph = false;
				_quickSaving = false;
			}
			
			//Load graph
			if( _shouldLoadGraph )
			{
				var loadDir = _graphsDir;

				if(!String.IsNullOrEmpty(_lastGraphPath))
					loadDir = _lastGraphPath;

				var path = string.IsNullOrEmpty(_overrideLoadPath) ? EditorUtility.OpenFilePanel("Load shader graph...", loadDir, "sgraph") : _overrideLoadPath;
				bool didLoad = false;
						
				if (!string.IsNullOrEmpty(path))
				{
					try
					{
						using( var fs = new FileStream(path, FileMode.Open) )
						{
							using( var reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas()) )
							{
								var ser = new DataContractSerializer(typeof (ShaderGraph), _serializableTypes, int.MaxValue, false, false, null);
								var loaded = ser.ReadObject(reader, true) as ShaderGraph;
								if (loaded != null)
								{
									_undoChain = new GraphHistory( _serializableTypes );
									_nextGraph = loaded;
									loaded.Initialize( new Rect(0, 0, Screen.width, Screen.height), true);
									_lastGraphPath = path;
									_lastExportPath = "";
									didLoad = true;
								}
							}
						}
					}
					catch( Exception e ){
						Debug.Log( e );
					}
					//Try loading autosave graph (it's in a differnt xml format)
					try
					{
						if( !didLoad )
						{
							using( var fs = new FileStream(path, FileMode.Open) )
							{
								using( var reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas()) )
								{
									var ser = new DataContractSerializer(typeof(AutoSaveGraph), _serializableTypes, int.MaxValue, false, false, null);
									var loaded = ser.ReadObject(reader, true) as AutoSaveGraph;
									if (loaded != null)
									{
										_undoChain = new GraphHistory( _serializableTypes );
										_nextGraph = loaded.Graph;
										_nextGraph.Initialize( new Rect(0, 0, Screen.width, Screen.height), true);
										_lastGraphPath = "";
										_lastExportPath = "";
										didLoad = true;
									}
									
								}
							}
						}
					}
					catch( Exception e)
					{
						Debug.Log(e);
					}
					
					if( !didLoad )
					{
						EditorUtility.DisplayDialog("Load Shader Error", "Could not load shader", "Ok");
					}
				}
				_shouldLoadGraph = false;
				_shouldUpdateShader = true;
				_overrideLoadPath = "";
			}
			
			//Export the shader to a .shader file
			if (_shouldExportShader)
			{
				if (_selectedGraph.IsGraphValid())
				{
					var path = _quickExport ? _lastExportPath : EditorUtility.SaveFilePanel("Export shader to file...", Application.dataPath, "shader.shader", "shader");
					_lastExportPath = path; // For quick exporting - Texel
					if (!string.IsNullOrEmpty(path))
					{
						File.WriteAllText(path, _selectedGraph.GetShader(_shaderTemplatePath, false));
						AssetDatabase.Refresh(); // Investigate if this is optimal
					}
					
					InstructionCounter.CountInstructions(); // Update the instruction count
					CacheCount(); // Solve the tooltip (Cached)
				}
				else
				{
					EditorUtility.DisplayDialog("Export Shader Error", "Cannot export shader, there are errors in the graph", "Ok");
				}
				_shouldExportShader = false;
				_quickExport = false;
			}
			Repaint();
		}
Example #2
0
		private void DisablePreview()
		{
			if( _previewWindow != null )
			{
				_previewWindow.ShouldUpdate = false;
				_previewWindow.Close();
				_previewWindow = null;
			}
		}