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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM