浏览代码

Updated teh Graph path functionality. It now saves the path and reads it back

/main
Martin Thorzen 6 年前
当前提交
dbf93edc
共有 3 个文件被更改,包括 84 次插入34 次删除
  1. 15
      com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 75
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardProvider.cs
  3. 28
      com.unity.shadergraph/Editor/Importers/ShaderGraphImporter.cs

15
com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs


set { m_PreviewData = value; }
}
[SerializeField]
string m_Path;
public string path
{
get { return m_Path; }
set
{
if (m_Path == value)
return;
m_Path = value;
owner.RegisterCompleteObjectUndo("Change Path");
}
}
public void ClearChanges()
{
m_AddedNodes.Clear();

75
com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardProvider.cs


ResizeBorderFrame m_ResizeBorderFrame;
public Blackboard blackboard { get; private set; }
Label m_PathLabel;
TextField m_PathLabelTextField;
bool m_EditPathCancelled = false;
public Action onDragFinished
{
get { return m_WindowDraggable.OnDragFinished; }

blackboard = new Blackboard()
{
scrollable = true,
title = assetName,
subTitle = "my awesome subtitle",
title = assetName.Split('/').Last(),
subTitle = graph.path,
editTextRequested = EditTextRequested,
addItemRequested = AddItemRequested,
moveItemRequested = MoveItemRequested

m_PathLabel.RegisterCallback<MouseDownEvent>(OnMouseDownEvent);
/*
* 1. Register event on double click
* 2. Spawn a text field on top of the label using absolute position
* (tip: add it to blackboard.parent)
* 3. Focus the text field using Focus
*/
m_PathLabelTextField = new TextField { visible = false };
m_PathLabelTextField.RegisterCallback<FocusOutEvent>(e => { OnEditPathTextFinished();});
m_PathLabelTextField.RegisterCallback<KeyDownEvent>(OnPathTextFieldKeyPressed);
blackboard.shadow.Add(m_PathLabelTextField);
m_WindowDraggable = new WindowDraggable(blackboard.shadow.Children().First().Q("header"));
blackboard.AddManipulator(m_WindowDraggable);

blackboard.Add(m_Section);
}
evt.PreventDefault();
TextField tField = new TextField();
tField.text = "Muppet";
tField.style.positionType = PositionType.Absolute;
var position = m_PathLabel.ChangeCoordinatesTo(blackboard, Vector2.zero);
tField.style.positionLeft = position.x;
tField.style.positionTop = position.y;
m_PathLabelTextField.visible = true;
m_PathLabelTextField.value = m_PathLabel.text;
m_PathLabelTextField.style.positionType = PositionType.Absolute;
var rect = m_PathLabel.ChangeCoordinatesTo(blackboard, new Rect(Vector2.zero, m_PathLabel.layout.size));
m_PathLabelTextField.style.positionLeft = rect.xMin;
m_PathLabelTextField.style.positionTop = rect.yMin;
m_PathLabelTextField.style.width = rect.width;
m_PathLabelTextField.style.fontSize = 11;
m_PathLabelTextField.style.marginLeft = 0;
m_PathLabelTextField.style.marginRight = 0;
m_PathLabelTextField.style.marginTop = 0;
m_PathLabelTextField.style.marginBottom = 0;
m_PathLabel.visible = false;
m_PathLabelTextField.Focus();
m_PathLabelTextField.SelectAll();
}
void OnPathTextFieldKeyPressed(KeyDownEvent evt)
{
switch (evt.keyCode)
{
case KeyCode.Escape:
m_EditPathCancelled = true;
m_PathLabelTextField.Blur();
break;
case KeyCode.Return:
case KeyCode.KeypadEnter:
m_PathLabelTextField.Blur();
break;
default:
break;
}
}
void OnEditPathTextFinished()
{
m_PathLabel.visible = true;
m_PathLabelTextField.visible = false;
blackboard.shadow.Add(tField);
if (!m_EditPathCancelled && (m_PathLabel.text != m_PathLabelTextField.text) && (m_PathLabelTextField.text.Trim() != ""))
{
m_PathLabel.text = m_PathLabelTextField.text.Trim();
m_Graph.path = m_PathLabel.text;
}
Debug.Log(m_PathLabel.text);
m_EditPathCancelled = false;
}
void MoveItemRequested(Blackboard blackboard, int newIndex, VisualElement visualElement)

28
com.unity.shadergraph/Editor/Importers/ShaderGraphImporter.cs


using UnityEditor.Experimental.AssetImporters;
using UnityEditor.ShaderGraph.Drawing;
[ScriptedImporter(12, ShaderGraphImporter.ShaderGraphExtension)]
[ScriptedImporter(13, ShaderGraphImporter.ShaderGraphExtension)]
private string errorShader = @"
const string k_ErrorShader = @"
Shader ""Hidden/GraphErrorShader2""
{
SubShader

ShaderUtil.ClearShaderErrors(oldShader);
List<PropertyCollector.TextureInfo> configuredTextures;
var text = GetShaderText<MaterialGraph>(ctx.assetPath, out configuredTextures);
if (text == null)
text = errorShader;
var name = Path.GetFileNameWithoutExtension(ctx.assetPath);
string shaderName = string.Format("graphs/{0}", name);
text = text.Replace("Hidden/GraphErrorShader2", shaderName);
var text = GetShaderText(ctx.assetPath, out configuredTextures);
var shader = ShaderUtil.CreateShaderAsset(text);
EditorMaterialUtility.SetShaderDefaults(

ctx.SetMainObject(shader);
}
private static string GetShaderText<T>(string path, out List<PropertyCollector.TextureInfo> configuredTextures) where T : IShaderGraph
static string GetShaderText(string path, out List<PropertyCollector.TextureInfo> configuredTextures)
string shaderString = null;
var shaderName = Path.GetFileNameWithoutExtension(path);
var graph = JsonUtility.FromJson<T>(textGraph);
var graph = JsonUtility.FromJson<MaterialGraph>(textGraph);
var name = Path.GetFileNameWithoutExtension(path);
var shaderString = graph.GetShader(string.Format("graphs/{0}", name), GenerationMode.ForReals, out configuredTextures);
//Debug.Log(shaderString);
return shaderString;
if (!string.IsNullOrEmpty(graph.path))
shaderName = graph.path + "/" + shaderName;
shaderString = graph.GetShader(shaderName, GenerationMode.ForReals, out configuredTextures);
}
catch (Exception)
{

return null;
return shaderString ?? k_ErrorShader.Replace("Hidden/GraphErrorShader2", shaderName);
}
}

正在加载...
取消
保存