Unity编辑器扩展-常用接口

1. 获取选中对象的相对路径:

AssetDatabase.GetAssetOrScenePath(Selection.activeGameObject).Replace('/', '\\');

2. 获取选中对象的完整路径:

var path = AssetDatabase.GetAssetOrScenePath(Selection.activeGameObject);
var fullPath = Path.GetFullPath(path);

3. 复制文本到剪切板:

GUIUtility.systemCopyBuffer = value;

4. 在扩展窗口选择对象:(如果需要选择字体,图片类型,则修改type)

(GameObject)EditorGUILayout.ObjectField(prefabTag, typeof(GameObject), true);

5. 打开并选择文件夹:

EditorUtility.OpenFolderPanel

6. 保存资源的修改:

AssetDatabase.SaveAssets();

7. 刷新资源:

AssetDatabase.Refresh();

8. 提示框:

EditorUtility.DisplayDialog("DisplayDialog", "Finished, you can close this window!", "Close");

9. 进度提示:

EditorUtility.DisplayProgressBar("DisplayProgressBar", "Progress", value);

10. 将信息写入文件:

File.WriteAllLines(fullPath, contents);

11. 编辑器布局:

1. BeginHorizontal		EndHorizontal    水平布局
2. BeginVertical		EndVertical      垂直布局
3. BeginScrollView		EndScrollView    滑动
4. GUILayout.Width      布局宽度
5. LabelField文字    ObjectField物体    Slider滑动条    Button按钮    TextFile文字
6. EditorUtility.OpenFilePanel("open", "e:/", ".txt") 打开文件浏览器,选择文件后返回路径

12.  AssetDatabase 获得资源相关信息:

1. AssetDatabase.GetAssetPath		获取资源路径,传入UnityEngine.Object
2. AssetDatabase.LoadAssetAtPath	通过路径获取资源
3. AssetDatabase.GetDependencies	获取依赖
4. AssetDatabase.SaveAssets			保存修改
5. AssetDatabase.Refresh			刷新
6. AssetDatabase.ImportAsset		导入资源

13. 鼠标右键调用:

[MenuItem("Assets/My Tools/Copy GameObject Full Path", false, 152)]

 14. 拖拽功能:

    private Rect window01 = new Rect(0, 0, 200, 80);
    private void OnGUI()
    {
        GUIStyle fontStyle = new GUIStyle(GUI.skin.button);
        fontStyle.fontSize = 40;
        window01 = GUI.Window(0, window01, DragWindow, "拖拽移动", fontStyle);
    }

    private void DragWindow(int windowID)
    {
        GUI.DragWindow(new Rect(0, 0, 200, 100));
    }

15. 下拉框:

(TextureImporterFormat)EditorGUILayout.EnumPopup("Importer Format:", m_importerFormat);

一键修改文件信息

编辑器扩展-入门篇

猜你喜欢

转载自blog.csdn.net/qq_33808037/article/details/109813324