1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-02-04 14:24:54 +08:00

Unified all functions that set the default node name (#161)

This commit is contained in:
Simon Rodriguez 2019-06-23 11:54:14 +02:00 committed by Thor Brigsted
parent 0553dc8894
commit 5d45300935
4 changed files with 15 additions and 11 deletions

View File

@ -86,9 +86,9 @@ namespace XNodeEditor {
/// <summary> Rename the node asset. This will trigger a reimport of the node. </summary> /// <summary> Rename the node asset. This will trigger a reimport of the node. </summary>
public void Rename(string newName) { public void Rename(string newName) {
if (newName == null || newName.Trim() == "") newName = UnityEditor.ObjectNames.NicifyVariableName(target.GetType().Name); if (newName == null || newName.Trim() == "") newName = NodeEditorUtilities.NodeDefaultName(target.GetType());
target.name = newName; target.name = newName;
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(target), target.name);
} }
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]

View File

@ -133,6 +133,15 @@ namespace XNodeEditor {
} else return type.ToString(); } else return type.ToString();
} }
/// <summary> Returns the default name for the node type. </summary>
public static string NodeDefaultName(Type type) {
string typeName = type.Name;
// Automatically remove redundant 'Node' postfix
if (typeName.EndsWith("Node")) typeName = typeName.Substring(0, typeName.LastIndexOf("Node"));
typeName = UnityEditor.ObjectNames.NicifyVariableName(typeName);
return typeName;
}
/// <summary>Creates a new C# Class.</summary> /// <summary>Creates a new C# Class.</summary>
[MenuItem("Assets/Create/xNode/Node C# Script", false, 89)] [MenuItem("Assets/Create/xNode/Node C# Script", false, 89)]
private static void CreateNode() { private static void CreateNode() {

View File

@ -74,12 +74,7 @@ namespace XNodeEditor {
public virtual void CreateNode(Type type, Vector2 position) { public virtual void CreateNode(Type type, Vector2 position) {
XNode.Node node = target.AddNode(type); XNode.Node node = target.AddNode(type);
node.position = position; node.position = position;
if (string.IsNullOrEmpty(node.name)) { if (node.name == null || node.name.Trim() == "") node.name = NodeEditorUtilities.NodeDefaultName(type);
// Automatically remove redundant 'Node' postfix
string typeName = type.Name;
if (typeName.EndsWith("Node")) typeName = typeName.Substring(0, typeName.LastIndexOf("Node"));
node.name = UnityEditor.ObjectNames.NicifyVariableName(typeName);
}
AssetDatabase.AddObjectToAsset(node, target); AssetDatabase.AddObjectToAsset(node, target);
if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets(); if (NodeEditorPreferences.GetSettings().autoSave) AssetDatabase.SaveAssets();
NodeEditorWindow.RepaintAll(); NodeEditorWindow.RepaintAll();

View File

@ -48,8 +48,8 @@ namespace XNodeEditor {
// If input is empty, revert name to default instead // If input is empty, revert name to default instead
if (input == null || input.Trim() == "") { if (input == null || input.Trim() == "") {
if (GUILayout.Button("Revert to default") || (e.isKey && e.keyCode == KeyCode.Return)) { if (GUILayout.Button("Revert to default") || (e.isKey && e.keyCode == KeyCode.Return)) {
target.name = UnityEditor.ObjectNames.NicifyVariableName(target.GetType().Name); target.name = NodeEditorUtilities.NodeDefaultName(target.GetType());
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(target)); AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(target), target.name);
Close(); Close();
} }
} }
@ -57,7 +57,7 @@ namespace XNodeEditor {
else { else {
if (GUILayout.Button("Apply") || (e.isKey && e.keyCode == KeyCode.Return)) { if (GUILayout.Button("Apply") || (e.isKey && e.keyCode == KeyCode.Return)) {
target.name = input; target.name = input;
AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(target), input); AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(target), target.name);
Close(); Close();
} }
} }