Added AutoTextureAssign assets helper
This commit is contained in:
27
Editor/AssetsHelpers/Helpers/VisualHelpers.cs
Normal file
27
Editor/AssetsHelpers/Helpers/VisualHelpers.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using UnityEditor;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Misaki.ArtToolEditor
|
||||
{
|
||||
public static class VisualHelpers
|
||||
{
|
||||
public static Button CreateFolderDialogButton(string buttonText, string defaultPath, string title, Action<string> onPathSelected)
|
||||
{
|
||||
var button = new Button()
|
||||
{
|
||||
text = buttonText,
|
||||
};
|
||||
button.clicked += () =>
|
||||
{
|
||||
var path = EditorUtility.OpenFolderPanel(title, defaultPath, string.Empty);
|
||||
if (!string.IsNullOrEmpty(path) && onPathSelected != null)
|
||||
{
|
||||
onPathSelected(path);
|
||||
}
|
||||
};
|
||||
|
||||
return button;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Editor/AssetsHelpers/Helpers/VisualHelpers.cs.meta
Normal file
2
Editor/AssetsHelpers/Helpers/VisualHelpers.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 96fbe047db3d24c4989a7d1ac8cf699a
|
||||
@@ -0,0 +1,33 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Misaki.ArtToolEditor
|
||||
{
|
||||
internal class AutoTextureAssignMenu
|
||||
{
|
||||
[MenuItem("Assets/Art Tools/Material Helpers/Auto Texture Assign", true)]
|
||||
public static bool AutoTextureAssignValidator()
|
||||
{
|
||||
foreach (var selectedObject in Selection.objects)
|
||||
{
|
||||
if (selectedObject is not Material)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
[MenuItem("Assets/Art Tools/Material Helpers/Auto Texture Assign")]
|
||||
public static void AutoTextureAssign()
|
||||
{
|
||||
var window = EditorWindow.GetWindow<OutputOptionsWindow>(true, "Auto Texture Assign Options");
|
||||
window.WithItemSource(Selection.objects);
|
||||
window.RegisterVisualProvider<AutoTextureAssignVisualProvider>();
|
||||
window.RegisterProcessor<AutoTextureAssignProcessor>();
|
||||
|
||||
window.InitializeAndShow(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 85b849706dec1f540aed9b4d40162191
|
||||
@@ -0,0 +1,56 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Misaki.ArtToolEditor
|
||||
{
|
||||
internal class AutoTextureAssignProcessor : IAssetsProcessor
|
||||
{
|
||||
public struct MatchData
|
||||
{
|
||||
public string propertyName;
|
||||
public string textureMatch;
|
||||
}
|
||||
|
||||
private const string Material_Name_Variable = "${Material Name}";
|
||||
|
||||
public string searchDirectory = "Assets";
|
||||
public List<MatchData> matchTable = new();
|
||||
|
||||
public void OnPostProcess(AssetsProcessContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnProcess(Object source, string outputDirectory, AssetsProcessContext context)
|
||||
{
|
||||
if (source is not Material material)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var match in matchTable)
|
||||
{
|
||||
if (!material.HasProperty(match.propertyName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var searchPattern = match.textureMatch.Replace(Material_Name_Variable, material.name);
|
||||
var matchedTextureGuid = AssetDatabase.FindAssets($"t=Texture {searchPattern}", new[] { searchDirectory }).FirstOrDefault();
|
||||
if (matchedTextureGuid == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var texturePath = AssetDatabase.GUIDToAssetPath(matchedTextureGuid);
|
||||
var textureAsset = AssetDatabase.LoadAssetAtPath<Texture>(texturePath);
|
||||
material.SetTexture(match.propertyName, textureAsset);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPreProcess(AssetsProcessContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d5758f1bafaccd41b057174036e4a24
|
||||
@@ -0,0 +1,76 @@
|
||||
using System.IO;
|
||||
using Unity.Properties;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Misaki.ArtToolEditor
|
||||
{
|
||||
internal class AutoTextureAssignVisualProvider : OptionsVisualProvider
|
||||
{
|
||||
private Button searchPathField;
|
||||
|
||||
public override void ContentBeforeButton(VisualElement rootVisualElement)
|
||||
{
|
||||
rootVisualElement.dataSource = processor;
|
||||
var autoTextureAssignProcessor = (AutoTextureAssignProcessor)processor;
|
||||
|
||||
searchPathField = VisualHelpers.CreateFolderDialogButton(
|
||||
autoTextureAssignProcessor.searchDirectory,
|
||||
"Assets",
|
||||
"Select Texture Directory",
|
||||
(path) =>
|
||||
{
|
||||
var projectRelativePath = Path.GetRelativePath(Path.GetDirectoryName(Application.dataPath), path);
|
||||
autoTextureAssignProcessor.searchDirectory = projectRelativePath;
|
||||
searchPathField.text = projectRelativePath;
|
||||
});
|
||||
|
||||
var matchTableListField = new ListView
|
||||
{
|
||||
headerTitle = "Match Table",
|
||||
bindingSourceSelectionMode = BindingSourceSelectionMode.AutoAssign,
|
||||
showAddRemoveFooter = true,
|
||||
allowAdd = true,
|
||||
allowRemove = true,
|
||||
showBorder = true,
|
||||
showFoldoutHeader = true,
|
||||
fixedItemHeight = 40,
|
||||
makeItem = () =>
|
||||
{
|
||||
var container = new VisualElement();
|
||||
|
||||
var propertyNameField = new TextField("Property Name")
|
||||
{
|
||||
tooltip = "The name of the property to match",
|
||||
};
|
||||
propertyNameField.SetBinding(nameof(TextField.value),
|
||||
new DataBinding()
|
||||
{
|
||||
dataSourceType = typeof(AutoTextureAssignProcessor.MatchData),
|
||||
dataSourcePath = PropertyPath.FromName(nameof(AutoTextureAssignProcessor.MatchData.propertyName))
|
||||
});
|
||||
|
||||
var textureMatchField = new TextField("Texture Match")
|
||||
{
|
||||
tooltip = "The regex to match when replace the name",
|
||||
};
|
||||
textureMatchField.SetBinding(nameof(TextField.value),
|
||||
new DataBinding()
|
||||
{
|
||||
dataSourceType = typeof(AutoTextureAssignProcessor.MatchData),
|
||||
dataSourcePath = PropertyPath.FromName(nameof(AutoTextureAssignProcessor.MatchData.textureMatch))
|
||||
});
|
||||
|
||||
container.Add(propertyNameField);
|
||||
container.Add(textureMatchField);
|
||||
return container;
|
||||
}
|
||||
};
|
||||
matchTableListField.SetBinding(nameof(ListView.itemsSource),
|
||||
new DataBinding() { dataSourcePath = PropertyPath.FromName(nameof(AutoTextureAssignProcessor.matchTable)) });
|
||||
|
||||
rootVisualElement.Add(searchPathField);
|
||||
rootVisualElement.Add(matchTableListField);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 75b97b03e9cde95459cd893b267335b7
|
||||
@@ -4,10 +4,6 @@ namespace Misaki.ArtToolEditor
|
||||
{
|
||||
public class ExtractMaterialsMenu
|
||||
{
|
||||
private const string Material_Extension = ".mat";
|
||||
|
||||
|
||||
|
||||
[MenuItem("Assets/Art Tools/Mesh Helpers/Extract Materials", true)]
|
||||
public static bool ExtractMaterialsValidator()
|
||||
{
|
||||
|
||||
@@ -104,8 +104,10 @@ namespace Misaki.ArtToolEditor
|
||||
|
||||
_processor.OnPostProcess(_context);
|
||||
|
||||
AssetDatabase.Refresh();
|
||||
AssetDatabase.SaveAssets();
|
||||
GC.Collect();
|
||||
EditorUtility.UnloadUnusedAssetsImmediate();
|
||||
AssetDatabase.Refresh();
|
||||
|
||||
Close();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user