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

原文链接 : http://blog.csdn.net/lyh916/article/details/44515343



一、MenuItem





点击My Window时调用Init()


二、RequireComponent





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


三、AddComponentMenu





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


四、ContextMenu






按下Test时,执行Test()


五、自定义Attribute属性


[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class TestAttribute : MonoBehaviour {  
  5.   
  6.     [Range(0, 10)]  
  7.     public int a;  
  8.   
  9.     [MyRange(-5f, 5f)]  
  10.     public float b;  
  11.   
  12.     public enum Force {蜀国,吴国,魏国};  
  13.   
  14.     [MyEnum]  
  15.     public Force force;//多选  
  16. }  

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class MyRange : PropertyAttribute {  
  5.   
  6.     public float min;  
  7.     public float max;  
  8.   
  9.     public MyRange(float min, float max)  
  10.     {  
  11.         this.min = min;  
  12.         this.max = max;  
  13.     }  
  14. }  

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEditor;  
  4.   
  5. [CustomPropertyDrawer(typeof(MyRange))]  
  6. public class MyRangeDrawer : PropertyDrawer {  
  7.   
  8.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)  
  9.     {  
  10.         MyRange myRange = attribute as MyRange;  
  11.   
  12.         if (property.propertyType == SerializedPropertyType.Float)  
  13.             EditorGUI.Slider(position, property, myRange.min, myRange.max, label);  
  14.         else if (property.propertyType == SerializedPropertyType.Integer)  
  15.             EditorGUI.IntSlider(position, property, (int)myRange.min, (int)myRange.max, label);  
  16.     }  
  17. }  

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class MyEnum : PropertyAttribute {  
  5.   
  6.       
  7. }  

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEditor;  
  4.   
  5. [CustomPropertyDrawer(typeof(MyEnum))]  
  6. public class MyEnumDrawer : PropertyDrawer {  
  7.   
  8.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)  
  9.     {  
  10.         property.intValue = EditorGUI.MaskField(position,label,property.intValue,property.enumNames);  
  11.     }  
  12. }  



/

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using UnityEditor;  
  3. using System.IO;  
  4.   
  5. /// <summary>  
  6. /// 需要创建Editor文件夹并把此类放进去  
  7. /// 操作文件的相关类:  
  8. /// Directory       提供操作目录的静态方法  
  9. /// DirectoryInfo   提供操作目录的实例方法  
  10. /// File            提供操作文件的静态方法  
  11. /// FileInfo        提供操作文件的实例方法  
  12. /// Path            操作路径  
  13. ///   
  14. /// Selection  
  15. /// AssetDatabase  
  16. /// 各种Importer  
  17. /// SerializedObject 与 SerializedProperty:  
  18. /// http://blog.csdn.net/qinyuanpei/article/details/49120765  
  19. /// http://www.manew.com/thread-46528-1-1.html?_dsign=4fc09684  
  20. /// </summary>  
  21. public class TestEditor {  
  22.   
  23.     static string dataPath = Application.dataPath + "/A";  
  24.     static string outputPath = Application.dataPath + "/B";  
  25.   
  26.     [MenuItem("Window/OperateFile")]  
  27.     static void OperateFile()  
  28.     {  
  29.         if (!Directory.Exists(dataPath))  
  30.         {  
  31.             Directory.CreateDirectory(dataPath);  
  32.             FileStream fs = File.Create(dataPath + "/a.txt");  
  33.             fs.Close();  
  34.         }  
  35.    
  36.         DirectoryInfo di = new DirectoryInfo(outputPath);  
  37.         if (!di.Exists)  
  38.         {  
  39.             di = new DirectoryInfo(outputPath);  
  40.             di.Create();  
  41.             FileInfo fi = new FileInfo(outputPath + "/b.txt");  
  42.             fi.Create();  
  43.         }  
  44.   
  45.         //返回目录中所有以.txt结尾的文件的完整路径  
  46.         string[] dataPaths = Directory.GetFiles(dataPath, "*.txt");  
  47.         if (dataPaths != null)  
  48.         {  
  49.             for (int i = 0; i < dataPaths.Length; i++)  
  50.             {  
  51.                 string s = Path.GetFileNameWithoutExtension(dataPaths[i]);  
  52.                 File.WriteAllText(dataPaths[i], s);  
  53.                 Debug.Log(File.ReadAllText(dataPaths[i]));  
  54.             }  
  55.         }  
  56.   
  57.         Debug.Log("finish");  
  58.         AssetDatabase.Refresh();  
  59.     }  
  60.   
  61.     [MenuItem("Window/MultiplyModify")]  
  62.     static void MultiplyModify()  
  63.     {  
  64.         Object[] o = Selection.GetFiltered(typeof(Texture), SelectionMode.DeepAssets);  
  65.         foreach (Texture s in o)  
  66.         {  
  67.             string path = AssetDatabase.GetAssetPath(s);  
  68.             TextureImporter textureImporter = TextureImporter.GetAtPath(path) as TextureImporter;  
  69.             textureImporter.textureType = TextureImporterType.Sprite;  
  70.             AssetDatabase.ImportAsset(path);  
  71.         }  
  72.     }  
  73.   
  74.     //右键菜单  
  75.     [MenuItem("Assets/DebugPath")]  
  76.     static void DebugPath()  
  77.     {  
  78.         for (int i = 0; i < Selection.objects.Length; i++)  
  79.         {  
  80.             Debug.Log(AssetDatabase.GetAssetPath(Selection.objects[i]));  
  81.         }       
  82.     }  
  83.   
  84. }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值