微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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<string>(){".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);
        }
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐