Created
February 8, 2022 11:36
-
-
Save hacha/0c610732c668563f8b139cbafee180c4 to your computer and use it in GitHub Desktop.
PlayMakerのVariablesのArray<Texture>に、指定ディレクトリ以下のテクスチャアセットを一括セットするエディタスクリプト
This file contains 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
#if PLAYMAKER | |
using System; | |
using System.Linq; | |
using HutongGames.PlayMaker; | |
using HutongGames.PlayMakerEditor; | |
using UnityEngine; | |
using UnityEditor; | |
public static class FsmArraySetter | |
{ | |
[MenuItem("CONTEXT/PlayMakerFSM/Texture Array Set")] | |
private static void Foo2(MenuCommand menuCommand) | |
{ | |
var fsm = menuCommand.context as PlayMakerFSM; | |
var arrayVariables = fsm.FsmVariables.ArrayVariables.Where(a => a.TypeConstraint == VariableType.Texture); | |
if (!arrayVariables.Any()) | |
{ | |
Debug.LogWarning("FsmArray<Texture> がありません"); | |
return; | |
} | |
FsmArray target = null; | |
foreach (var a in arrayVariables) | |
{ | |
if (EditorUtility.DisplayDialog("確認", $"{a.Name} に対して処理しますか?", "OK", "次へ")) | |
{ | |
target = a; | |
break; | |
} | |
} | |
if (target == null) | |
{ | |
Debug.LogWarning("処理対象が選ばれませんでした"); | |
return; | |
} | |
const string TargetDirectoryKey = "FsmTextureArraySetter/TargetDirectoryPath"; | |
var directoryPath = EditorUtility.OpenFolderPanel("フォルダ選択", EditorPrefs.GetString(TargetDirectoryKey, Application.dataPath), String.Empty); | |
if (string.IsNullOrEmpty(directoryPath)) | |
{ | |
return; | |
} | |
EditorPrefs.SetString(TargetDirectoryKey, directoryPath); | |
Debug.Log("対象ディレクトリ: " + directoryPath); | |
// 絶対パスを Assets/ から始まる相対パスにする | |
var absoluteUri = new Uri(directoryPath); | |
var baseUri = new Uri(Application.dataPath); | |
var relativeUri = baseUri.MakeRelativeUri(absoluteUri); | |
// Debug.Log(absoluteUri); | |
// Debug.Log(baseUri); | |
// Debug.Log(Uri.UnescapeDataString(relativeUri.ToString())); | |
var guids = AssetDatabase.FindAssets("t:texture2D", new[] {Uri.UnescapeDataString(relativeUri.ToString())}); | |
// foreach (var guid in guids) | |
// { | |
// Debug.Log(AssetDatabase.GUIDToAssetPath(guid)); | |
// } | |
fsm.FsmVariables.DeleteVariable(target); | |
var length = guids.Count(); | |
target.objectReferences = new UnityEngine.Object[length]; | |
var index = 0; | |
foreach (var guid in guids) | |
{ | |
var path = AssetDatabase.GUIDToAssetPath(guid); | |
var asset = AssetDatabase.LoadAssetAtPath<Texture>(path); | |
target.objectReferences[index++] = asset; | |
} | |
fsm.FsmVariables.AddVariable(target); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment