浏览代码

working on better workflow (double click shader to open in edit window)

/main
Tim Cooper 7 年前
当前提交
5e3b99fe
共有 7 个文件被更改,包括 128 次插入145 次删除
  1. 110
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphEditWindow.cs
  2. 5
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/GraphEditWindow.cs
  3. 29
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Presenters/TitleBarPresenter.cs
  4. 1
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Assets/IGraphAsset.cs
  5. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateShaderGraph.cs
  6. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  7. 126
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs

110
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/AbstractGraphEditWindow.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine.MaterialGraph;
using Object = UnityEngine.Object;
namespace UnityEditor.Graphing.Drawing
{

[NonSerialized]
private IGraphAsset m_Selected;
private Object m_Selected;
[SerializeField]
private ScriptableObject m_ToLoad;
public static bool allowAlwaysRepaint = true;

void Update()
{
if (m_ToLoad)
{
ChangeSelction (m_ToLoad as IGraphAsset);
m_ToLoad = null;
}
if (shouldRepaint)
Repaint();
}

public void PingAsset()
{
if (m_Selected != null)
EditorGUIUtility.PingObject(m_Selected.GetScriptableObject());
EditorGUIUtility.PingObject(m_Selected);
public void UpdateAsset()
{
if (m_Selected != null && m_Selected is IGraphAsset)
{
var path = AssetDatabase.GetAssetPath (m_Selected.GetScriptableObject());
public void UpdateAsset()
{
if (m_Selected != null && m_InMemoryAsset != null)
{
var path = AssetDatabase.GetAssetPath(m_Selected);
if (string.IsNullOrEmpty(path) || m_InMemoryAsset == null)
{
return;
}
var masterNode = ((MaterialGraphAsset) m_InMemoryAsset).materialGraph.masterNode;
if (masterNode == null)
return;
if (!string.IsNullOrEmpty(path) && m_InMemoryAsset != null)
{
File.WriteAllText (path, EditorJsonUtility.ToJson (m_InMemoryAsset.graph as object));
AssetDatabase.ImportAsset (path);
List<PropertyGenerator.TextureInfo> configuredTextures;
masterNode.GetFullShader(GenerationMode.ForReals, out configuredTextures);
var shaderImporter = AssetImporter.GetAtPath(path) as ShaderImporter;
if (shaderImporter == null)
return;
var asset = AssetDatabase.LoadAssetAtPath<ScriptableObject> (path) as IGraphAsset;
if (asset != null) {
m_Selected = null;
ChangeSelction (asset, false);
}
}
}
}
var textureNames = new List<string>();
var textures = new List<Texture>();
foreach (var textureInfo in configuredTextures.Where(
x => x.modifiable == TexturePropertyChunk.ModifiableState.Modifiable))
{
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
if (texture == null)
continue;
textureNames.Add(textureInfo.name);
textures.Add(texture);
}
shaderImporter.SetDefaultTextures(textureNames.ToArray(), textures.ToArray());
public virtual void ToggleRequiresTime()
textureNames.Clear();
textures.Clear();
foreach (var textureInfo in configuredTextures.Where(
x => x.modifiable == TexturePropertyChunk.ModifiableState.NonModifiable))
{
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
if (texture == null)
continue;
textureNames.Add(textureInfo.name);
textures.Add(texture);
}
shaderImporter.SetNonModifiableTextures(textureNames.ToArray(), textures.ToArray());
File.WriteAllText(path, EditorJsonUtility.ToJson(m_InMemoryAsset.graph));
shaderImporter.SaveAndReimport();
AssetDatabase.ImportAsset(path);
}
}
public virtual void ToggleRequiresTime()
public void ChangeSelction(IGraphAsset newSelection, bool refocus = true)
public void ChangeSelction(Object newSelection, bool refocus = true)
if (!(newSelection is ScriptableObject))
return;
var newGraph = (ScriptableObject)newSelection;
if (!EditorUtility.IsPersistent (newGraph))
if (!EditorUtility.IsPersistent (newSelection))
if (m_Selected != null) {
if (m_Selected != null)
{
if (EditorUtility.DisplayDialog ("Save Old Graph?", "Save Old Graph?", "yes!", "no")) {
UpdateAsset ();
}

m_InMemoryAsset = UnityEngine.Object.Instantiate (newGraph) as IGraphAsset;
var graph = m_InMemoryAsset.graph;
var mGraph = CreateInstance<MaterialGraphAsset>();
var path = AssetDatabase.GetAssetPath(newSelection);
var textGraph = File.ReadAllText(path, Encoding.UTF8);
mGraph.materialGraph = JsonUtility.FromJson<UnityEngine.MaterialGraph.MaterialGraph>(textGraph);
m_InMemoryAsset = mGraph;
var graph = m_InMemoryAsset.graph;
graph.OnEnable ();
graph.ValidateGraph ();

public void OnBeforeSerialize()
{
m_ToLoad = m_Selected as ScriptableObject;
//m_ToLoad = m_Selected as ScriptableObject;
}
public void OnAfterDeserialize()

5
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/GraphEditWindow.cs


{
public class GraphEditWindow : AbstractGraphEditWindow
{
[MenuItem("Window/Graph Editor")]
public static void OpenMenu()
public GraphEditWindow(string path)
GetWindow<GraphEditWindow>();
}
}
}

29
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/Presenters/TitleBarPresenter.cs


showInProjectItem.onClick += OnShowInProjectClick;
m_leftItems.Add(showInProjectItem);
var selectGraph = CreateInstance<TitleBarButtonPresenter>();
selectGraph.text = "Select Graph";
selectGraph.onClick += SelectGraph;
m_leftItems.Add(selectGraph);
var optionsItem = CreateInstance<TitleBarButtonPresenter>();
optionsItem.text = "Time";
optionsItem.onClick += ToggleTime;

}
return assets;
}
void SelectGraph()
{
var options = FindAssets();
var gm = new GenericMenu ();
foreach (var option in options) {
gm.AddItem (new GUIContent (AssetDatabase.GetAssetPath (option.GetScriptableObject())), false, Callback, new CallbackData(){asset = option, owner = m_Owner});
}
gm.ShowAsContext ();
}
void Callback(object userData)
{
if (!(userData is CallbackData))
return;
var cbData = (CallbackData)userData;
cbData.owner.ChangeSelction (cbData.asset);
}
//TODO: We need two currently.. fix later
m_Owner.UpdateAsset ();
m_Owner.UpdateAsset ();
}

1
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Assets/IGraphAsset.cs


ScriptableObject GetScriptableObject();
void OnEnable();
}
}

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/AssetCallbacks/CreateShaderGraph.cs


using System.IO;
using UnityEditor.ProjectWindowCallback;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph
{

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


using RMGUI.GraphView;
using UnityEditor.Graphing.Drawing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.Drawing
{

126
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Importers/ShaderGraphImporter.cs


using UnityEditor.Experimental.AssetImporters;
using UnityEngine.MaterialGraph;
using UnityEngine.MaterialGraph;
using System.Linq;
#if UNITY_EDITOR
using System.Reflection;
using UnityEditor;
#endif
using UnityEngine.Graphing;
using UnityEditor;
using UnityEditor.MaterialGraph.Drawing;
using UnityEngine.Events;
[ScriptedImporter(1, "ShaderGraph")]
public class ShaderGraphImporter : ScriptedImporter
class ShaderGraphTextGenerator : ICustomShaderImporter
public override void OnImportAsset(AssetImportContext ctx)
{
var textGraph = File.ReadAllText(ctx.assetPath, Encoding.UTF8);
var graph = JsonUtility.FromJson<MaterialGraph>(textGraph);
public string GetShaderText(string path)
{
try
{
var textGraph = File.ReadAllText(path, Encoding.UTF8);
var graph = JsonUtility.FromJson<MaterialGraph>(textGraph);
if (graph == null)
return;
IMasterNode masterNode = graph.masterNode;
if (masterNode == null)
return null;
var graphAsset = ScriptableObject.CreateInstance<MaterialGraphAsset> ();
graphAsset.materialGraph = graph;
ctx.SetMainAsset("MainAsset", graphAsset);
var shader = RegenerateShader (graphAsset);
if (shader == null)
return;
ctx.AddSubAsset("Shader", shader);
}
public Shader RegenerateShader(MaterialGraphAsset asset)
{
IMasterNode masterNode = asset.materialGraph.masterNode;
if (masterNode == null)
return null;
var path = "Assets/UnityShaderEditor/Editor/HelperShader.shader";
List<PropertyGenerator.TextureInfo> configuredTextures;
var shaderString = masterNode.GetFullShader(GenerationMode.ForReals, out configuredTextures);
var shader = AssetDatabase.LoadAssetAtPath(path, typeof(Shader)) as Shader;
if (shader == null)
return null;
File.WriteAllText (path, shaderString);
ShaderUtil.UpdateShaderAsset (shader, shaderString);
List<PropertyGenerator.TextureInfo> configuredTextures;
var shaderString = masterNode.GetFullShader(GenerationMode.ForReals, out configuredTextures);
return shaderString;
}
catch (Exception)
{
// ignored
}
return null;
}
var shaderImporter = AssetImporter.GetAtPath(path) as ShaderImporter;
if (shaderImporter == null)
return null;
public bool IsValidForPath(string path)
{
try
{
var textGraph = File.ReadAllText(path, Encoding.UTF8);
var graph = JsonUtility.FromJson<MaterialGraph>(textGraph);
var textureNames = new List<string>();
var textures = new List<Texture>();
foreach (var textureInfo in configuredTextures.Where(x => x.modifiable == TexturePropertyChunk.ModifiableState.Modifiable))
{
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
if (texture == null)
continue;
textureNames.Add(textureInfo.name);
textures.Add(texture);
}
shaderImporter.SetDefaultTextures(textureNames.ToArray(), textures.ToArray());
return graph != null;
}
catch (Exception)
{
// ignored
}
return false;
}
textureNames.Clear();
textures.Clear();
foreach (var textureInfo in configuredTextures.Where(x => x.modifiable == TexturePropertyChunk.ModifiableState.NonModifiable))
{
var texture = EditorUtility.InstanceIDToObject(textureInfo.textureId) as Texture;
if (texture == null)
continue;
textureNames.Add(textureInfo.name);
textures.Add(texture);
}
shaderImporter.SetNonModifiableTextures(textureNames.ToArray(), textures.ToArray());
shaderImporter.SaveAndReimport();
public void OnInspectorGUI(string path, UnityAction defaultOnInspectorGUI)
{
defaultOnInspectorGUI();
if (GUILayout.Button("Open Shader Editor"))
{
var window = new MaterialGraphEditWindow();
window.Show();
window.ChangeSelction(AssetDatabase.LoadAssetAtPath<Shader>(path));
}
}
var imported = shaderImporter.GetShader();
return imported;
}
public void OpenAsset(string path)
{
var window = new MaterialGraphEditWindow();
window.Show();
window.ChangeSelction(AssetDatabase.LoadAssetAtPath<Shader>(path));
}
}
正在加载...
取消
保存