查看: 1371|回复: 0
打印 上一主题 下一主题

[其他] Editor编辑器的一些用法

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53464
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2016-1-17 05:06:36 |只看该作者 |倒序浏览
共有两个脚本,一个是有关Inspector面板的,一个是window的

using UnityEngine;
using System.Collections;
using UnityEditor;

//2.枚举下拉框
//public enum OPTIONS {
//    CUBE=0,
//    SPHERE,
//    PLANE,
//}

public class Texts : MonoBehaviour {
    //1.通过拖这个脚本可以改变颜色和曲线
    //public Color myColor;
    //public AnimationCurve curveX = AnimationCurve.Linear(0, 0, 10, 10);
    //public AnimationCurve curveY = AnimationCurve.Linear(0, 0, 10, 10);
    //public AnimationCurve curveZ = AnimationCurve.Linear(0, 0, 10, 10);

    //public Rect myRect;
    //public Texture myTexture;

    //void Update() {
    //    if (Input.GetKeyUp(KeyCode.A)) {
    //        print("MyColor" + myColor);  //输出颜色
    //        print("curveX" + curveX.Evaluate(Time.time));  //利用时间获取曲线的值
    //        print("curveY" + curveY.Evaluate(Time.time));
    //        print("curveZ" + curveZ.Evaluate(Time.time));
    //        print("myRect" + myRect);  //打印Rect的x,y,width,height
    //        print("myTexture" + myTexture);  //打印Texture
    //    }
    //    //根据三个曲线,实时获取他们的值,来设置该对象的位置
    //    transform.position = new Vector3(curveX.Evaluate(Time.time), curveY.Evaluate(Time.time), curveZ.Evaluate(Time.time));
    //}
    //2.枚举下拉框
    //public OPTIONS options = OPTIONS.PLANE;
    //public float number;
    //public StaticEditorFlags staticFlagMask = 0;
    //3.显示transform,rotation,scal
    //public Transform myTransform;
    //public Vector3 myRotation;
    //public Vector3 myLocal;
    //public int number;
    //4.Slider
    //public int xx = 3;
    //public int yy = 5;
    //public int zz = 80;
    //5.Layer, Mask, Tag
    //public int selectLayer = 0;
    //public int selectFlag = 0;
    //public string[] options = { "CanJump", "CanShoot", "CanSwim" };
    //public string tagStr = "";
    //6.Password
    //public string text = "Some text Here";
    //7.TextField  Toggle
    //public string MyName;
    //public bool showBtn = true;
    //8.vector2,vector3,vector4
    //public Vector2 p2;
    //public Vector3 p3;
    //public Vector4 p4;
    //9.Button
    //public string MyName;
    //public bool showBtn = true;
    //10.Toggle-Button-Label
    //public void MyTestA() {
    //    Debug.Log("Click");
    //}
    //11.toolbar
    //public void MyTestA() {
    //    Debug.Log("Click");
    //}
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEditor;

//[CustomEditor(typeof(Texts))]
//10
//[CanEditMultipleObjects]
//[CustomEditor(typeof(Texts))]
//public class MyEditor :  Editor{
    //10.
    //private Texts texts;
    //private bool isTrue = true;
    //private void OnEnable() {
        //texts = (Texts)target;
    //}
    //public override void OnInspectorGUI() {  //该方法会绘制在inspector面板上
        //1.通过拖这个脚本可以改变颜色和曲线
        //Texts myTexts = (Texts)target;
        ////颜色
        //myTexts.myColor = EditorGUILayout.ColorField("New Color", myTexts.myColor);
        ////曲线
        //myTexts.curveX = EditorGUILayout.CurveField("Animation On X", myTexts.curveX);
        //myTexts.curveY = EditorGUILayout.CurveField("Animation On Y", myTexts.curveY);
        //myTexts.curveZ = EditorGUILayout.CurveField("Animation On Z", myTexts.curveZ);
        ////rect
        //myTexts.myRect = EditorGUILayout.RectField("窗口", myTexts.myRect);
        ////texture
        //myTexts.myTexture = EditorGUILayout.ObjectField("贴图", myTexts.myTexture,typeof(Texture),true)as Texture;
        //base.OnInspectorGUI();
        //2.枚举类型和 系统自带的 静态Mask枚举
        //Texts myTexts = (Texts)target;
        //myTexts.options = (OPTIONS)EditorGUILayout.EnumPopup("options", myTexts.options);
        //myTexts.number = EditorGUILayout.FloatField("number", myTexts.number);
        //myTexts.staticFlagMask = (StaticEditorFlags)EditorGUILayout.EnumMaskField("static Flags", myTexts.staticFlagMask);
        //3.修改transform
        //Texts myTexts = (Texts)target;
        //myTexts.myTransform = myTexts.transform;
        //myTexts.myTransform.position = EditorGUILayout.Vector3Field("MyPosition", myTexts.transform.position);
        //EditorGUILayout.Space();
        //Quaternion rot = myTexts.transform.rotation; //将Quaternion转换为vector3
        //Vector3 rotation = new Vector3(rot.eulerAngles.x, rot.eulerAngles.y, rot.eulerAngles.z);
        //myTexts.myRotation = EditorGUILayout.Vector3Field("myRotation", rotation);
        //myTexts.myLocal = EditorGUILayout.Vector3Field("myLocal", myTexts.transform.localScale);
        //myTexts.number = EditorGUILayout.IntField("number", myTexts.number);
        //4.Slider
        //myTexts.xx = EditorGUILayout.IntSlider(myTexts.xx, 1, 10);
        //myTexts.yy = EditorGUILayout.IntSlider(myTexts.yy, 2, 8);
        //myTexts.zz = EditorGUILayout.IntSlider(myTexts.zz, 0, 100);
        //5.Layer, Mask, Tag
        //myTexts.selectLayer = EditorGUILayout.LayerField("Layer Objects", myTexts.selectLayer);
        //myTexts.selectFlag = EditorGUILayout.MaskField("player Flags ", myTexts.selectFlag, myTexts.options);
        //myTexts.tagStr = EditorGUILayout.TagField("Tag for Objects:", myTexts.tagStr);
        //6.password
        //myTexts.text = EditorGUILayout.PasswordField("Type Somthing", myTexts.text);
        //7.TextField  Toggle
        //myTexts.MyName = EditorGUILayout.TextField("Object Name", myTexts.MyName);
        //myTexts.showBtn = EditorGUILayout.Toggle("Show Button", myTexts.showBtn);
        //8.vector2,vector3,vector4
        //myTexts.p2 = EditorGUILayout.Vector2Field("Point 2", myTexts.p2);
        //myTexts.p3 = EditorGUILayout.Vector3Field("Point 3", myTexts.p3);
        //myTexts.p4 = EditorGUILayout.Vector4Field("Point 4", myTexts.p4);
        //9.button
        //EditorGUILayout.HelpBox("abcde", MessageType.Error); //错误提示框
        //if (GUILayout.Button("Add Path Points")) {
        //    Debug.Log("ABCDE");
        //    return;
        //}
        //10.Toggle-Button-Label
        //EditorGUILayout.BeginVertical("box"); //开始水平布局,“box”为一个 方框区域
        //EditorGUILayout.LabelField("ABCDE");  //不可点的lable
        //EditorGUILayout.SelectableLabel("asdadsa");  //可点的label
        //if (GUILayout.Button("Click")) {  //创建button, 当点击按钮时,调用相应方法
        //    texts.MyTestA();
        //}
        //EditorGUILayout.EndVertical();
        //EditorGUILayout.Space();  //空格行

        //isTrue = EditorGUILayout.Toggle("isTrigger", isTrue);
        //EditorGUILayout.BeginVertical();
        //EditorGUI.BeginDisabledGroup(isTrue);  //如果isTrue为真,则下方显示在面板上(灰色)不可操作,为假则可操作
        //if (GUILayout.Button("Button")) {
        //    texts.MyTestA();
        //}
        //EditorGUI.EndDisabledGroup();
        //EditorGUILayout.EndVertical();
        //EditorGUILayout.HelpBox("help help help", MessageType.Warning);  //显示帮助框,类型为警告
        //EditorGUILayout.Space();
        //EditorGUILayout.HelpBox("aaa aa a", MessageType.Error);  //显示帮助框,类型为错误
        //11.toolbar
//    private Texts texts;

//    private int select = 0;
//    private int selected = 0;
//    private string[] names = { "one", "two", "three", "four" };
//    private static GUIContent[] myGUIContent = new GUIContent[4];
//    private Texture2D[] myTexture = new Texture2D[4];

//    private void OnEnable() {
//        texts = (Texts)target;
//        Init();
//    }

//    private void Init()  //初始化获取图片
//    {

//        for (int i = 0; i < 4; i++) {
//            //通过路径获取图片资源
//            myTexture[i] = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/MyTexture/1.png", typeof(Texture2D));
//            //将图片数组和字符串数组存进去
//            myGUIContent[i] = new GUIContent(myTexture[i], names[i]);
//        }
//    }

//    public override void OnInspectorGUI()  //绘制在Inspector面板上
//    {
//        EditorGUILayout.BeginVertical();
//        //创建 Toolbar  , 返回值为选中的项, 参数1 为 选中的项, 参数 2 每项上的文字
//        select = GUILayout.Toolbar(select, names);

//        EditorGUILayout.Space();
//        EditorGUILayout.Space();
//        //参数 1, 选中的项, 参数2 每项的图片, 参数3 整个Toolbar的宽度, 参数4 Toolbar的高度
//        selected = GUILayout.Toolbar(selected, myGUIContent, GUILayout.Width(300), GUILayout.Height(64));
//        EditorGUILayout.Space();

//        EditorGUILayout.EndVertical();
//    }
//}
    //12.window
//public class MyEditor : EditorWindow {
//    public string[] savedCopies = new string[5];  //定义一个字符串数组
//    public bool load = false;

//    [MenuItem("Examples/Improved copy buffer")] //在工具栏创建按钮
//    static void Init() {
//        MyEditor window = (MyEditor)EditorWindow.GetWindow(typeof(MyEditor));  //定义一个窗口对象
//    }

//    void OnGUI() {
//        load = EditorGUILayout.Toggle("Load:", load); //创建一个 Toggle
//        EditorGUILayout.BeginHorizontal();  //开始水平布局
//        for (int i = 0; i < savedCopies.Length; i++) {
//            if (GUILayout.Button(i.ToString())) // 创建这些按钮,并且当点击按钮时触发条件
//            {
//                if (load) {
//                    EditorGUIUtility.systemCopyBuffer = savedCopies[i]; // 获取值
//                } else {
//                    savedCopies[i] = "123"; //给每一个字符串赋值
//                }
//            }
//        }
//        EditorGUILayout.EndHorizontal();  //结束水平布局

//        for (int j = 0; j < savedCopies.Length; j++) {
//            EditorGUILayout.LabelField("Saved " + j, savedCopies[j]); //将所有已字符串以Label的形式显示出来
//        }

//        EditorGUILayout.LabelField("Current buffer:", EditorGUIUtility.systemCopyBuffer); //将获取的值以Label的形式显示出来
//        if (GUILayout.Button("Clear all saves"))  //清空所有的缓存
//        {
//            int i = 0;
//            foreach (string s in savedCopies) {
//                savedCopies[i] = "";
//                i++;
//            }
//        }
//    }

//    void OnInspectorUpdate() {
//        this.Repaint();  //重新画窗口
//    }
//}
    //13.实时获取摄像机的视野,摄像机视野内的所有对象均可被重画
//public class MyEditor : EditorWindow {
//    Camera myCamera = Camera.main;  //获取摄像机
//    RenderTexture renderTexture;   //

//    [MenuItem("Examples/Camera Viewer")]  //在菜单栏创建按钮
//    static void Init()
//    {
//        EditorWindow editorWindows = GetWindow(typeof(MyEditor));  //创建新窗口
//        editorWindows.autoRepaintOnSceneChange = true;
//        editorWindows.Show();
//    }

//    public void Awake()  //当跳出窗口时首先调用该方法
//    {
//        Debug.Log("Awake");
//        renderTexture = new RenderTexture((int)position.width,
//            (int)position.height, (int)RenderTextureFormat.ARGB32);  //获取renderTexture
//    }

//    public void Update()  //跳出窗口后每帧调用该方法
//    {
//        Debug.Log("update");
//        if (myCamera != null)
//        {
//            myCamera.targetTexture = renderTexture;
//            myCamera.Render();
//            myCamera.targetTexture = null;
//        }

//        if (renderTexture != null)
//        {
//            if (renderTexture.width != position.width || renderTexture.height != position.height)
//            {
//                renderTexture = new RenderTexture((int)position.width, (int)position.height,
//                    (int)RenderTextureFormat.ARGB32); //实时更新
//            }
//        }
//    }

//    void OnGUI()
//    {
//        if (renderTexture != null)
//        {
//            GUI.DrawTexture(new Rect(0.0f, 0.0f, position.width, position.height), renderTexture); //该方法一直绘制摄像机视野内的物体
//        }
//    }
//}
    //14.window_button
//    public class MyEditor : EditorWindow {
//        private bool WantsMouseMove = false;

//    [MenuItem("Example/Window postion")]
//    static void Init()
//    {
//        MyEditor window = (MyEditor)EditorWindow.GetWindow(typeof(MyEditor));
//        window.position = new Rect(100, 100, 300, 300);  // 窗口的坐标
//    }

//    void OnGUI()
//    {
//        GUILayout.Space(20);
//        if (GUILayout.Button("myButton"))  //在窗口上创建一个按钮
//        {
//            Debug.Log("button");
//        }
//        //在窗口创建Toggle 并且获取 Toggle的
//        WantsMouseMove = EditorGUILayout.Toggle("Receive MoveMent", WantsMouseMove);
//        //
//        EditorGUILayout.LabelField("Mouse Postion:", Event.current.mousePosition.ToString());

//        if (Event.current.type == EventType.MouseMove)//当事件为移动鼠标
//        {
//            Repaint(); //重新绘制
//        }
//    }
//}
    //15.scrollview
//public class MyEditor : EditorWindow {
//     Rect windowRect = new Rect(100, 100, 200, 200);
//    Vector3 scrollPos = Vector2.zero;

//    [MenuItem("Example/Window postion")]
//    static void Init()
//    {
//        MyEditor window = (MyEditor)EditorWindow.GetWindow(typeof(MyEditor));
//        window.position = new Rect(100, 100, 300, 300);  // 窗口的坐标
//    }

//    void OnGUI()
//    {
//        //创建 scrollView  窗口
//        scrollPos = GUI.BeginScrollView(new Rect(0, 0, position.width, position.height),
//            scrollPos, new Rect(0, 0, 1000, 1000));

//        GUILayout.Space(100);  // 100 像素宽的空格

//        if (GUILayout.Button("ButtonA")) //创建按钮
//        {
//            Debug.Log("AAA");
//        }

//        GUILayout.Space(50);

//        if (GUILayout.Button("ButtonB"))  //创建按钮
//        {
//            Debug.Log("BBB");
//        }

//        GUI.EndScrollView();  //结束 ScrollView 窗口

//        if (GUILayout.Button("ButtonC"))
//        {
//            Debug.Log("CCC");
//        }

//    }

//}
//16.Notification通知
//public class MyEditor : EditorWindow {
//    string notification = "This is a Notification";  //通知内容

//    [MenuItem("Example/Notification usage")]
//    static void Init() {
//        MyEditor window = (MyEditor)EditorWindow.GetWindow(typeof(MyEditor));
//        window.Show();
//    }

//    void OnGUI() {
//        notification = EditorGUILayout.TextField(notification);

//        if (GUILayout.Button("Show Notification")) {
//            this.ShowNotification(new GUIContent(notification)); //调用该方法发送通知
//        }

//        if (GUILayout.Button("Remove Notification")) {
//            this.RemoveNotification();  //取消通知
//        }
//    }

//    void OnInspectorUpdate()  //该方法每秒调用 10 帧
//      {
//        this.Repaint();
//    }

//    void OnHierarchyChange()  //当Hierarchy面板发生变化时,调用该方法
//      {

//    }

//    void OnProjectChange() //当 Project 面板发生变化时调用该方法
//    {

//    }


//    void OnSelectionChange() //当选择的对象改变时调用该方法
//        {

//    }

//    void Update()  //每秒调用 100 次
//        {

//    }
//}


分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-7 23:48 , Processed in 0.092141 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部