Last active
March 11, 2022 05:18
-
-
Save shivaduke28/21dd8d6b965fdabeb905f8bcb0edf214 to your computer and use it in GitHub Desktop.
U#スクリプトと同じフォルダに置いてProgramAssetを移動したりするやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
using VRC.Udon; | |
using System.Linq; | |
namespace Shivaduke.Editor | |
{ | |
[CreateAssetMenu(menuName = "Shivaduke/UdonProgramLocation", fileName = "UdonProgramLocation")] | |
public class UdonProgramLocation : ScriptableObject | |
{ | |
const string DirectoryName = "SerializedUdonPrograms"; | |
public void MoveSerializedUdonPrograms() | |
{ | |
var list = new List<AbstractUdonProgramSource>(); | |
var dirPath = GetDirPath(); | |
// load all udon program sources in the same directory | |
var sourcePaths = AssetDatabase.FindAssets($"t:{nameof(AbstractUdonProgramSource)}", new string[1] { dirPath }) | |
.Select(guid => AssetDatabase.GUIDToAssetPath(guid)) | |
.ToList(); | |
if (sourcePaths.Count == 0) return; | |
// create dest directory | |
var dstDirPath = Path.Combine(dirPath, DirectoryName); | |
if (!AssetDatabase.IsValidFolder(Path.Combine(dirPath, DirectoryName))) | |
{ | |
AssetDatabase.CreateFolder(dirPath, DirectoryName); | |
} | |
foreach (var sourcePath in sourcePaths) | |
{ | |
var source = AssetDatabase.LoadAssetAtPath<AbstractUdonProgramSource>(sourcePath); | |
var src = AssetDatabase.GetAssetPath(source.SerializedProgramAsset); | |
if (string.IsNullOrEmpty(src)) continue; | |
// already moved | |
if (Path.GetDirectoryName(src) == dstDirPath) continue; | |
var dst = Path.Combine(dstDirPath, Path.GetFileName(src)); | |
AssetDatabase.MoveAsset(src, dst); | |
Debug.Log($"move {sourcePath}'s program {src} -> {dst}"); | |
} | |
} | |
public void DeleteUnusedUdonPrograms() | |
{ | |
var dirPath = GetDirPath(); | |
var dstDirPath = Path.Combine(dirPath, DirectoryName); | |
var sources = AssetDatabase.FindAssets($"t:{nameof(AbstractUdonProgramSource)}", new string[1] { dirPath }) | |
.Select(AssetDatabase.GUIDToAssetPath) | |
.Select(AssetDatabase.LoadAssetAtPath<AbstractUdonProgramSource>) | |
.ToList(); | |
var programs = AssetDatabase.FindAssets($"t:{nameof(AbstractSerializedUdonProgramAsset)}", new string[1] { dirPath }) | |
.Select(AssetDatabase.GUIDToAssetPath) | |
.Select(path => (path, AssetDatabase.LoadAssetAtPath<AbstractSerializedUdonProgramAsset>(path))) | |
.Where(pathAndProgram => sources.All(source => source.SerializedProgramAsset != pathAndProgram.Item2)) | |
.ToList(); | |
foreach (var (path, _) in programs) | |
{ | |
AssetDatabase.DeleteAsset(path); | |
Debug.Log($"Delete {path}"); | |
} | |
} | |
string GetDirPath() | |
{ | |
var assetPath = AssetDatabase.GetAssetPath(this); | |
return Path.GetDirectoryName(assetPath); | |
} | |
[MenuItem("Tool/Shivaduke/Apply All UdonProgramLocations")] | |
public static void ApplyAllUdonProgramLocations() | |
{ | |
var locations = AssetDatabase.FindAssets($"t:{nameof(UdonProgramLocation)}", new string[1] { "Assets" }) | |
.Select(AssetDatabase.GUIDToAssetPath) | |
.Select(AssetDatabase.LoadAssetAtPath<UdonProgramLocation>) | |
.ToList(); | |
foreach (var location in locations) | |
{ | |
location.MoveSerializedUdonPrograms(); | |
location.DeleteUnusedUdonPrograms(); | |
} | |
} | |
} | |
[CustomEditor(typeof(UdonProgramLocation))] | |
public class UdonProgramLocationEditor : UnityEditor.Editor | |
{ | |
public override void OnInspectorGUI() | |
{ | |
base.OnInspectorGUI(); | |
if (GUILayout.Button("Move Serialized Programs")) | |
{ | |
var location = (UdonProgramLocation)target; | |
location.MoveSerializedUdonPrograms(); | |
} | |
if (GUILayout.Button("Delete Unused Serialized Programs")) | |
{ | |
var location = (UdonProgramLocation)target; | |
location.DeleteUnusedUdonPrograms(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment