浏览代码

Dispose resources when an exception is thrown during Update or ChangeSelection, such that we don't run out of preview scenes and ruin previews for everyone

/main
Peter Bay Bastian 7 年前
当前提交
944c3654
共有 2 个文件被更改,包括 78 次插入52 次删除
  1. 128
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  2. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs

128
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs


[SerializeField]
SerializableGraphObject m_GraphObject;
[NonSerialized]
bool m_HasError;
GraphEditorView m_GraphEditorView;
GraphEditorView graphEditorView

void Update()
{
if (graphObject == null && selectedGuid != null)
if (m_HasError)
return;
try
var guid = selectedGuid;
selectedGuid = null;
ChangeSelection(guid);
}
if (graphObject == null && selectedGuid != null)
{
var guid = selectedGuid;
selectedGuid = null;
ChangeSelection(guid);
}
if (graphObject == null)
{
Close();
return;
}
var materialGraph = graphObject.graph as AbstractMaterialGraph;
if (materialGraph == null)
return;
if (graphEditorView == null)
{
var asset = AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GUIDToAssetPath(selectedGuid));
graphEditorView = new GraphEditorView(this, materialGraph, asset.name) { persistenceKey = AssetDatabase.AssetPathToGUID(AssetDatabase.GUIDToAssetPath(selectedGuid)) };
}
if (graphObject == null)
{
Close();
return;
graphEditorView.HandleGraphChanges();
graphObject.graph.ClearChanges();
var materialGraph = graphObject.graph as AbstractMaterialGraph;
if (materialGraph == null)
return;
if (graphEditorView == null)
catch (Exception)
var asset = AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GUIDToAssetPath(selectedGuid));
graphEditorView = new GraphEditorView(this, materialGraph, asset.name) { persistenceKey = AssetDatabase.AssetPathToGUID(AssetDatabase.GUIDToAssetPath(selectedGuid)) };
m_HasError = true;
m_GraphEditorView = null;
graphObject = null;
throw;
graphEditorView.HandleGraphChanges();
graphObject.graph.ClearChanges();
}
void OnDisable()

public void ChangeSelection(string newSelectionGuid)
{
var asset = AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GUIDToAssetPath(newSelectionGuid));
if (asset == null)
return;
try
{
var asset = AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GUIDToAssetPath(newSelectionGuid));
if (asset == null)
return;
if (!EditorUtility.IsPersistent(asset))
return;
if (!EditorUtility.IsPersistent(asset))
return;
if (selectedGuid == newSelectionGuid)
return;
var path = AssetDatabase.GetAssetPath(asset);
var extension = Path.GetExtension(path);
Type graphType;
switch (extension)
{
case ".ShaderGraph":
graphType = typeof(MaterialGraph);
break;
case ".ShaderSubGraph":
graphType = typeof(SubGraph);
break;
default:
if (selectedGuid == newSelectionGuid)
}
selectedGuid = newSelectionGuid;
var path = AssetDatabase.GetAssetPath(asset);
var extension = Path.GetExtension(path);
Type graphType;
switch (extension)
{
case ".ShaderGraph":
graphType = typeof(MaterialGraph);
break;
case ".ShaderSubGraph":
graphType = typeof(SubGraph);
break;
default:
return;
}
var textGraph = File.ReadAllText(path, Encoding.UTF8);
graphObject = CreateInstance<SerializableGraphObject>();
graphObject.hideFlags = HideFlags.HideAndDontSave;
graphObject.graph = JsonUtility.FromJson(textGraph, graphType) as IGraph;
graphObject.graph.OnEnable();
graphObject.graph.ValidateGraph();
selectedGuid = newSelectionGuid;
var textGraph = File.ReadAllText(path, Encoding.UTF8);
graphObject = CreateInstance<SerializableGraphObject>();
graphObject.hideFlags = HideFlags.HideAndDontSave;
graphObject.graph = JsonUtility.FromJson(textGraph, graphType) as IGraph;
graphObject.graph.OnEnable();
graphObject.graph.ValidateGraph();
graphEditorView = new GraphEditorView(this, m_GraphObject.graph as AbstractMaterialGraph, asset.name) { persistenceKey = AssetDatabase.GUIDToAssetPath(selectedGuid) };
graphEditorView.RegisterCallback<PostLayoutEvent>(OnPostLayout);
graphEditorView = new GraphEditorView(this, m_GraphObject.graph as AbstractMaterialGraph, asset.name) { persistenceKey = AssetDatabase.GUIDToAssetPath(selectedGuid) };
graphEditorView.RegisterCallback<PostLayoutEvent>(OnPostLayout);
titleContent = new GUIContent(asset.name);
titleContent = new GUIContent(asset.name);
Repaint();
Repaint();
}
catch (Exception)
{
m_HasError = true;
m_GraphEditorView = null;
graphObject = null;
throw;
}
}
void OnPostLayout(PostLayoutEvent evt)

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs


~PreviewManager()
{
ReleaseUnmanagedResources();
throw new Exception("PreviewManager was not disposed of properly.");
}
const string k_EmptyShader = @"

正在加载...
取消
保存