Unity编译器拓展

1.Resources

资源会被压缩,使用Resources.Load加载,是同步加载的,不管在哪都可以被找到,有多个Resources都可以被找到

2.StreamingAsset

放AB资源包用的,不会被压缩,只可读的文件夹,不能写

3.Plugins

插件库文件夹,用于放移动平台的插件和SDK的

4.Editor

编译器扩展,不管路径,只要名字相同就可以提供功能,脚本只能在编译器下运行,并且不会被打包进入工程。

输出一个字符串

    [MenuItem("GameObject/Test!!!!!",false,1)]
    static void Test()
    {
        Debug.Log("01");
    }

输出选中GameObjects的名字

[MenuItem("GameObject/TestSelect!!!!")]
    static void TestSelect()
    {
        foreach(var item in Selection.gameObjects)
        {
            Debug.Log(item.name);
        }
    }

给一个选项添加快捷方式

To create a hotkey you can use the following special characters: % (ctrl on Windows, cmd on macOS), # (shift), & (alt). If no special modifier key combinations are required the key can be given after an underscore. For example to create a menu with hotkey shift-alt-g use "MyMenu/Do Something #&g". To create a menu with hotkey g and no key modifiers pressed use "MyMenu/Do Something _g".

用例:

    [MenuItem("GameObject/TestSelect!!!! %g",false)]
    static void TestSelect()
    {
        foreach (var item in Selection.gameObjects)
        {
            Debug.Log(item.name);
        }
    }
MenuItem

Parameters

itemName The itemName is the menu item represented like a pathname. For example the menu item could be "GameObject/Do Something".

isValidateFunction

If isValidateFunction is true, this is a validation function and will be called before invoking the menu function with the same itemName.

priority

The order by which the menu items are displayed.

public MenuItem(string itemName, bool isValidateFunction, int priority);

用例:输出选中的物体的名称,没有选中物体的时候,不能调用

    [MenuItem("GameObject/TestSelect!!!! %g", true)]
    static bool TestSelect0()
    {
        return Selection.activeObject != null;
    }
    [MenuItem("GameObject/TestSelect!!!! %g",false)]
    static void TestSelect()
    {
        foreach (var item in Selection.gameObjects)
        {
            Debug.Log(item.name);
        }
    }

Undo

Lets you register undo operations on specific objects you are about to perform changes on.
The Undo system stores delta changes in the undo stack.

MenuCommand
CONTEXT : 哪个类型东西进行编译
用例:改变一个脚本中的一些字段
此脚本放在Editor文件夹下

public class PlayerX : MonoBehaviour {
    public string name;
    public int age;
}
public class PlayerXEditor {
    [MenuItem("CONTEXT/PlayerX/ChangePlData")]
    static void ChangePlayerData(MenuCommand menuCommand)
    {
        PlayerX playerX = menuCommand.context as PlayerX;
        playerX.name = "abc";
        playerX.age = 100;
    }
}
ContextMenu、ContextMenuItem

给继承自MonoBehavior的类的方法添加ContextMenu添加特征,可以在component中右键调用方法。
给字段添加特征ContextMenuItem,可以在component的字段中右键调用方法。

ScriptableWizard

一个类继承ScriptableWizard
通过ScriptableWizard.DisplayWizard<>()方法创建窗口,泛型中传自己的类。

两个按钮OnChange、Apply两个按钮前者按下执行OnWizardCreate方法,
后者执行OnWizardOtherButton方法,OnWizardUpdate方法在创建此窗口的时候进行调用。
OnSelectionChange()父类中的方法,每次进行选择操作都会调用一次。

用例:创建一个窗口将自身数据进行显示

public class ChangePlayerSpeed : ScriptableWizard{
    public int newSpeed = 1;

    [MenuItem("Tools/CreateChangePlayerSpeed")]
    static void Create()
    {
        ScriptableWizard.DisplayWizard<ChangePlayerSpeed>("批量修改","OnChange","Apply");
    }

    private void OnWizardCreate()
    {
        if(Selection.gameObjects.Length>=0)
        {
            foreach(var item in Selection.gameObjects)
            item.GetComponent<PlayerX>().speed += newSpeed;
        }
    }
    private void OnWizardOtherButton()
    {
        if (Selection.gameObjects.Length >= 0)
        {
            foreach (var item in Selection.gameObjects)
                item.GetComponent<PlayerX>().speed += newSpeed;
        }
    }
    private void OnWizardUpdate()
    {
        if(Selection.gameObjects.Length == 0)
        {
            errorString = "0";
        }
        else
        {
            helpString = "选中目标数 : "+Selection.gameObjects.Length;
        }
        Debug.Log("Select : " + Selection.gameObjects.Length + " gameObjs");
    }
    private void OnSelectionChange()
    {
        if (Selection.gameObjects.Length == 0)
        {
            errorString = "0";
        }
        else
        {
            errorString = "";            
        }
        helpString = "选中目标数 : " + Selection.gameObjects.Length;
    }
}

EditorPrefs
在编译器中存储临时数据

EditorUtility

private void OnWizardCreate()
    {
        int num = Selection.gameObjects.Length;
        EditorUtility.DisplayProgressBar("Loading", "0/" + num, 0);
        int count = 0;
        if (Selection.gameObjects.Length>=0)
        {
            foreach (var item in Selection.gameObjects)
            {
                count++;
                Thread.Sleep(300);
                EditorUtility.DisplayProgressBar("Loading", (float)count + "/" + num, (float)count / num);
                item.GetComponent<PlayerX>().speed += newSpeed;
            }
        }
    }
EditorWindows

自定义窗口
用例1:

public class CreateMyWindows : EditorWindow  {
    string mname;
    [MenuItem("Tools/mywindow")]
    static void CreateWindow()
    {
        CreateMyWindows createMyWindows = EditorWindow.GetWindow<CreateMyWindows>("MyWindow");
    }
    private void OnGUI()
    {
        GUILayout.Label("window Label");
        mname = GUILayout.TextField(mname);
        if (GUILayout.Button("Create"))
        {
            GameObject obj = new GameObject(mname);
        }
    }
}

用例2:


11173460-61d50e131a97812c.PNG
CustomWindow.PNG
public class CreateMyWindow : EditorWindow {

    private string myString = "xixi";
    private bool myBool = false;
    private UnityEngine.Object targetObj;
    private bool isPopup;
    private bool sub0;
    private bool sub1;

    private const string myBoolKey = "myBoolKey";

    [MenuItem("Tools/MyWindows")]
    static void Init()
    {
        CreateMyWindow createMyWindow = EditorWindow.GetWindow<CreateMyWindow>(true, "Custom Window"); 
    }

    private void OnEnable()
    {
        myBool = EditorPrefs.GetBool(myBoolKey);
    }

    //Implement your own editor GUI here
    private void OnGUI()
    {
        ShowContent();
        this.Repaint();
    }
    private void OnSelectionChange()
    {
        if(Selection.activeObject == null)
        {
            return;
        }
        targetObj = Selection.activeObject;
        this.Repaint();
    }
    //具体windows内容
    private void ShowContent()
    {
        GUILayout.Label("Base", EditorStyles.boldLabel);
        targetObj = EditorGUILayout.ObjectField("Target", targetObj, typeof(UnityEngine.Object),true);
        myString = EditorGUILayout.TextField(myString);
        myBool = EditorGUILayout.Toggle("Toggle", myBool);
        EditorPrefs.SetBool(myBoolKey, myBool);

        isPopup = EditorGUILayout.Foldout(isPopup, "Pop");
        if (isPopup)
        {
            sub0 = EditorGUILayout.Toggle("Sub0", sub0);
            sub1 = EditorGUILayout.Toggle("Sub1", sub1);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值