因为经常用到,还要到旧项目去找,因此记录一下:
using UnityEditor;
using UnityEngine;
public class TextureHelp : Editor
{
[MenuItem("Tools/获取选中文件相对路径")]
public static void GetFileRelativePath()
{
string[] GUIDs = Selection.assetGUIDs;
foreach (var guid in GUIDs)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
Debug.Log(path);
//输出结果为:Assets/测试文件.png
}
}
[MenuItem("Tools/获取选中文件绝对路径")]
public static void GetFileAbsolutePath()
{
string[] GUIDs = Selection.assetGUIDs;
foreach (var guid in GUIDs)
{
string path = Application.dataPath.Remove(Application.dataPath.Length - 6) + AssetDatabase.GUIDToAssetPath(guid);
Debug.Log(path);
///输出结果为:Volumes/Eevee_4TB/ZKCM/Test/Draw/Assets/测试文件.png
}
}
[MenuItem("Tools/遍历全项目Texture文件相对路径")]
public static void TraverseTexturesPath()
{
foreach (var guid in AssetDatabase.FindAssets("t:Texture"))//此处Texture类型也可以是Model、Audio之类
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
Debug.Log(assetPath);
//后续可以用以下加载方式继续操作
//var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
}
}
[MenuItem("Tools/遍历子孙物体", false, 1)]
static void SelectionObjects()
{
GameObject[] selections = Selection.gameObjects;
foreach (GameObject selection in selections)//遍历每个选中的物体
{
foreach (Transform child in selection.GetComponentsInChildren<Transform>())
{
//遍历子物体及孙物体
Debug.Log(child.name);
}
//如果仅遍历子物体
//foreach (Transform child in selection.transform)
//{
// Debug.Log(child.name);
//}
}
Debug.Log("SelectionObjects End!");
}
}
网友评论