[Unity编辑器]与编辑器相关的属性与类

来源:互联网 发布:simpson算法流程图 编辑:程序博客网 时间:2024/05/21 15:49

一、MenuItem





点击My Window时调用Init()


二、RequireComponent





添加脚本A时自动添加刚体组件


三、AddComponentMenu





当按下脚本A时,为当前选择的GameObject添加脚本A


四、ContextMenu






按下Test时,执行Test()


五、自定义Attribute属性


using UnityEngine;using System.Collections;public class TestAttribute : MonoBehaviour {    [Range(0, 10)]    public int a;    [MyRange(-5f, 5f)]    public float b;    public enum Force {蜀国,吴国,魏国};    [MyEnum]    public Force force;//多选}

using UnityEngine;using System.Collections;public class MyRange : PropertyAttribute {    public float min;    public float max;    public MyRange(float min, float max)    {        this.min = min;        this.max = max;    }}

using UnityEngine;using System.Collections;using UnityEditor;[CustomPropertyDrawer(typeof(MyRange))]public class MyRangeDrawer : PropertyDrawer {    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)    {        MyRange myRange = attribute as MyRange;        if (property.propertyType == SerializedPropertyType.Float)            EditorGUI.Slider(position, property, myRange.min, myRange.max, label);        else if (property.propertyType == SerializedPropertyType.Integer)            EditorGUI.IntSlider(position, property, (int)myRange.min, (int)myRange.max, label);    }}

using UnityEngine;using System.Collections;public class MyEnum : PropertyAttribute {}

using UnityEngine;using System.Collections;using UnityEditor;[CustomPropertyDrawer(typeof(MyEnum))]public class MyEnumDrawer : PropertyDrawer {    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)    {        property.intValue = EditorGUI.MaskField(position,label,property.intValue,property.enumNames);    }}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using UnityEngine;using UnityEditor;using System.IO;/// <summary>/// 需要创建Editor文件夹并把此类放进去/// 操作文件的相关类:/// Directory       提供操作目录的静态方法/// DirectoryInfo   提供操作目录的实例方法/// File            提供操作文件的静态方法/// FileInfo        提供操作文件的实例方法/// Path            操作路径/// /// Selection/// AssetDatabase/// 各种Importer/// SerializedObject 与 SerializedProperty:/// http://blog.csdn.net/qinyuanpei/article/details/49120765/// http://www.manew.com/thread-46528-1-1.html?_dsign=4fc09684/// </summary>public class TestEditor {    static string dataPath = Application.dataPath + "/A";    static string outputPath = Application.dataPath + "/B";    [MenuItem("Window/OperateFile")]    static void OperateFile()    {        if (!Directory.Exists(dataPath))        {            Directory.CreateDirectory(dataPath);            FileStream fs = File.Create(dataPath + "/a.txt");            fs.Close();        }         DirectoryInfo di = new DirectoryInfo(outputPath);        if (!di.Exists)        {            di = new DirectoryInfo(outputPath);            di.Create();            FileInfo fi = new FileInfo(outputPath + "/b.txt");            fi.Create();        }        //返回目录中所有以.txt结尾的文件的完整路径        string[] dataPaths = Directory.GetFiles(dataPath, "*.txt");        if (dataPaths != null)        {            for (int i = 0; i < dataPaths.Length; i++)            {                string s = Path.GetFileNameWithoutExtension(dataPaths[i]);                File.WriteAllText(dataPaths[i], s);                Debug.Log(File.ReadAllText(dataPaths[i]));            }        }        Debug.Log("finish");        AssetDatabase.Refresh();    }    [MenuItem("Window/MultiplyModify")]    static void MultiplyModify()    {        Object[] o = Selection.GetFiltered(typeof(Texture), SelectionMode.DeepAssets);        foreach (Texture s in o)        {            string path = AssetDatabase.GetAssetPath(s);            TextureImporter textureImporter = TextureImporter.GetAtPath(path) as TextureImporter;            textureImporter.textureType = TextureImporterType.Sprite;            AssetDatabase.ImportAsset(path);        }    }    //右键菜单    [MenuItem("Assets/DebugPath")]    static void DebugPath()    {        for (int i = 0; i < Selection.objects.Length; i++)        {            Debug.Log(AssetDatabase.GetAssetPath(Selection.objects[i]));        }         }}


0 0
原创粉丝点击