浏览代码

Make use of the generic duplicate name handling funciton when editing property and reference names

/main
Jens Holm 7 年前
当前提交
7f9416d0
共有 2 个文件被更改,包括 3 次插入65 次删除
  1. 63
      com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 5
      com.unity.shadergraph/Editor/Drawing/Blackboard/BlackboardProvider.cs

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


public string SanitizePropertyName(string displayName, Guid guid = default(Guid))
{
displayName = displayName.Trim();
if (m_Properties.Any(p => p.displayName == displayName && p.guid != guid))
{
// Strip out the " (n)" part of the name.
var baseRegex = new Regex(@"^(.*) \((\d+)\)$");
var baseMatch = baseRegex.Match(displayName);
if (baseMatch.Success)
displayName = baseMatch.Groups[1].Value;
var regex = new Regex(@"^" + Regex.Escape(displayName) + @" \((\d+)\)$");
var existingDuplicateNumbers = m_Properties.Where(p => p.guid != guid).Select(p => regex.Match(p.displayName)).Where(m => m.Success).Select(m => int.Parse(m.Groups[1].Value)).Where(n => n > 0).Distinct().ToList();
var duplicateNumber = 1;
existingDuplicateNumbers.Sort();
if (existingDuplicateNumbers.Any() && existingDuplicateNumbers.First() == 1)
{
duplicateNumber = existingDuplicateNumbers.Last() + 1;
for (var i = 1; i < existingDuplicateNumbers.Count; i++)
{
if (existingDuplicateNumbers[i - 1] != existingDuplicateNumbers[i] - 1)
{
duplicateNumber = existingDuplicateNumbers[i - 1] + 1;
break;
}
}
}
displayName = string.Format("{0} ({1})", displayName, duplicateNumber);
}
return displayName;
return GraphUtil.SanitizeName(m_Properties.Where(p => p.guid != guid).Select(p => p.displayName), "{0} ({1})", displayName);
}
public string SanitizePropertyReferenceName(string referenceName, Guid guid = default(Guid))

referenceName = Regex.Replace(referenceName, @"(?:[^A-Za-z_0-9])|(?:\s)", "_");
// Similar to the property names, duplicate names are handled by appending `_n`
// to the end of the referene name.
if (m_Properties.Any(p => p.referenceName == referenceName && p.guid != guid))
{
// Strip out the "_n" part of the name.
var baseRegex = new Regex(@"^(.*)_(\d+)$");
var baseMatch = baseRegex.Match(referenceName);
if (baseMatch.Success)
referenceName = baseMatch.Groups[1].Value;
var regex = new Regex(@"^" + Regex.Escape(referenceName) + @"_(\d+)$");
var existingDuplicateNumbers = m_Properties.Where(p => p.guid != guid).Select(p => regex.Match(p.referenceName)).Where(m => m.Success).Select(m => int.Parse(m.Groups[1].Value)).Where(n => n > 0).Distinct().ToList();
var duplicateNumber = 1;
existingDuplicateNumbers.Sort();
if (existingDuplicateNumbers.Any() && existingDuplicateNumbers.First() == 1)
{
duplicateNumber = existingDuplicateNumbers.Last() + 1;
for (var i = 1; i < existingDuplicateNumbers.Count; i++)
{
if (existingDuplicateNumbers[i - 1] != existingDuplicateNumbers[i] - 1)
{
duplicateNumber = existingDuplicateNumbers[i - 1] + 1;
break;
}
}
}
referenceName = string.Format("{0}_{1}", referenceName, duplicateNumber);
}
return referenceName;
return GraphUtil.SanitizeName(m_Properties.Where(p => p.guid != guid).Select(p => p.referenceName), "{0}_{1}", referenceName);
}
public void RemoveShaderProperty(Guid guid)

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


using System;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Experimental.UIElements.GraphView;

return;
if (create)
{
property.overrideReferenceName = m_Graph.SanitizePropertyReferenceName(property.overrideReferenceName);
}
var field = new BlackboardField(m_ExposedIcon, property.displayName, property.propertyType.ToString()) { userData = property };
var row = new BlackboardRow(field, new BlackboardFieldPropertyView(m_Graph, property));

正在加载...
取消
保存