Unity编辑器工具编写记录

获取当前选中的物体

//单个物体
var obj = Selection.activeObject;

//多个物体
var objs = Selection.objects;

 

获取选中物体的路劲

var path = AssetDatabase.GetAssetPath(Selection.activeObject);

 

获取文件的guid:

var guid = AssetDatabase.AssetPathToGUID(path);

 

查找Assets目录中以.xxx为后缀的所有文件

var suffix = new List(){".prefab", ".mat", ".asset"};

string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories).Where(s=> suffix.Contains(Path.GetExtension(s).ToLower())).ToArray();

 

创建一个按钮样式

GUIStyle btnStyle = new GUIStyle(EditorSytles.miniButton);

btnStyle.fontSize = 14;

btnStyle.normal.textColor = Color.green;

 

给某个MonoBehaviour的子类编写Inspector的绘制UI

[CustomEditor(typeof(XXXX), true)]
public class XXXX_Inspector: Editor
{
    public overrid void OnInspectorGUI()
    {
        //TODO:自定义绘制
        
        base.OnInspectorGUI();
    }
}

 

Inspector中显示lua脚本的预览

[CustomEditor(typeof(UnityEditor.DefaultAsset))]
public class PreviewLuaFile : Editor
{
    public overrid void OnInspectorGUI()
    {
        var path = AssetDatabase.GetAssetPath(target);
        if(path.EndsWith(".lua"))
        {
            GUI.enabled = true;
            GUI.backgroundColor = new Color(60,60,60);
            var str = System.IO.File.ReadAllText(path);
            if(str.Lenght > 1024 *20)
            {
                str = str.SubString(0,1024*20)+"...";
            }
            GUILayout.TextArea(str);
        }
    }
}

 

你可能感兴趣的:(unity3D,Unity3D)