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

一、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]));
        }     
    }

}


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值