Unity创建asset文件的扩展编辑器

 1 using UnityEngine;
 2 using UnityEditor;
 3 using System.IO;
 4 
 5 public class CreateAsset : EditorWindow
 6 {
 7     private string mConfigName = "TestAsset";
 8     private string mAssetPath = "Assets/Config/";
 9     private string mAssetName = "MyAsset";
10 
11     [MenuItem("Tools/CreateAsset")]
12     public static void GenAsset()
13     {
14         GetWindow(typeof(CreateAsset));
15     }
16 
17     private void OnGUI()
18     {
19         mConfigName = EditorGUILayout.TextField("Asset类名:", mConfigName);
20         mAssetPath = EditorGUILayout.TextField("生成Asset路径", mAssetPath);
21         mAssetName = EditorGUILayout.TextField("生成Asset名称", mAssetName);
22         if (GUILayout.Button(new GUIContent("生成Asset")))
23         {
24             if (string.IsNullOrEmpty(mConfigName) || string.IsNullOrEmpty(mAssetPath) || string.IsNullOrEmpty(mAssetName))
25             {
26                 Debug.LogError("Err!");
27                 return;
28             }
29 
30             var newAsset = CreateInstance(mConfigName);
31             string fullPath = mAssetPath + mAssetName + ".asset";
32             AssetDatabase.CreateAsset(newAsset, fullPath);
33             Debug.Log(fullPath);
34             AssetDatabase.SaveAssets();
35             AssetDatabase.Refresh();
36             EditorUtility.FocusProjectWindow();
37             Selection.activeObject = newAsset;
38         }
39     }
40 }

 

posted @ 2018-10-29 16:24  大文豪  阅读(2291)  评论(1编辑  收藏  举报

123