[Unity] 找到选中文件节点下的所有文件
把editor的序列化模式改成文本: EditorSettings.serializationMode = SerializationMode.ForceText;
添加工具借口在菜单 :[MenuItem("Assets/ChooseObject")]
读取选中的文件 :Selection.activeObject
获得文件的路径 :AssetDatabase.GetAssetPath
获得文件下的所有子文件的路径: Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories);
用自文件路径加载每个子文件:AssetDatabase.LoadAssetAtPath<GameObject>
或者子文件后,就可以动态修改需要子文件的内容。
把修改了的文件从内存里存到本地:
EditorUtility.SetDirty(gameObject);
AssetDatabase.SaveAssets();
刷新unity:
AssetDatabase.Refresh();
以下是完整代码
```
[MenuItem("Assets/ChooseObject")]
public static void ChooseObject()
{
EditorSettings.serializationMode = SerializationMode.ForceText;
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (!string.IsNullOrEmpty(path))
{
string[] files = Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
GameObject gameObject = AssetDatabase.LoadAssetAtPath<GameObject>(files[i]);
Debug.LogError(gameObject.name);
EditorUtility.SetDirty(gameObject);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}
```