In the previous article, we have come up with two core learning ideas:
- According to the problem to learn, and collect.
- Take the initiative to learn and think about the applicable scenarios.
Let's solve the problem of MenuItem display order today.
At present, MenuItem is shown as follows:
Let's take a look at the definition of MenuItem.
The second parameter is whether it is a verification method. It is not understood at present. The default value on the official website is false.
The third parameter, which means priority, indicates the display order of MenuItem. The larger the value, the more at the bottom.
Let's give the seventh example a try. Change the code to read:
using System.IO; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace QFramework { public class CustomShortCut : MonoBehaviour { #if UNITY_EDITOR [MenuItem("QFramework/7.Custom shortcut %e",false,-10)] private static void MenuClicked() { var generatePackageName = Exporter.GenerateUnityPackageName(); EditorUtil.ExportPackage("Assets/QFramework",generatePackageName + ".unitypackage"); EditorUtil.OpenInFolder(Path.Combine(Application.dataPath, "../")); } #endif } }
After compiling, observe the menu bar, and the results are as follows:
Shown at the top. By the way, let's change the file name and menu name of the seventh example from 7.XXX to 1.XXX.
The file name is as follows:
The menu name code is as follows:
using System.IO; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace QFramework { public class CustomShortCut : MonoBehaviour { #if UNITY_EDITOR [MenuItem("QFramework/1.Custom shortcut %e",false,-10)] private static void MenuClicked() { var generatePackageName = Exporter.GenerateUnityPackageName(); EditorUtil.ExportPackage("Assets/QFramework",generatePackageName + ".unitypackage"); EditorUtil.OpenInFolder(Path.Combine(Application.dataPath, "../")); } #endif } }
After compilation, the menu is as follows:
In fact, the name in the menu is not reasonable. Although this example is for learning to customize and write down quickly, if it takes a long time, we can see that the menu bar above will be confused. In fact, its name should be export UnityPackage.
We will change the menu and directory name to this 1. export UnityPackage. Do you know how to change it?
The menu after modification is as follows:
The contents are as follows:
In the above, we solved the key problem of adjusting the order of menu bar.
Let's finish the rest of the sequence today. Let's start with the eighth example.
Eighth example
Let's take a look at the first MenuItem in the eighth example. The code is as follows.
[MenuItem("QFramework/8.Summarize previous methods/1.Get file name")] private static void MenuClicked() { Debug.Log(Exporter.GenerateUnityPackageName()); }
Among them, "QFramework/8. Method before summary / 1. Get filename" can be changed to "QFramework/2. Method before summary / 1. Get filename"
However, the function of this summary is not very useful for us, because in the first example we just completed, we have included the usage of Exporter.GenerateUnityPackageName.
The first example code is as follows:
using System.IO; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; namespace QFramework { public class CustomShortCut : MonoBehaviour { #if UNITY_EDITOR [MenuItem("QFramework/1.export UnityPackage %e",false,-10)] private static void MenuClicked() { var generatePackageName = Exporter.GenerateUnityPackageName(); EditorUtil.ExportPackage("Assets/QFramework",generatePackageName + ".unitypackage"); EditorUtil.OpenInFolder(Path.Combine(Application.dataPath, "../")); } #endif } }
So, let's delete the MenuItem method in the eighth example.
Look at the second MenuItem code as follows:
[MenuItem("QFramework/8.Summarize previous methods/2.Copy text to clipboard")] private static void MenuClicked2() { CommonUtil.CopyText("Keywords to copy"); }
The use of CommonUtil.CopyText in the code is not included in the first example. So this MenuItem should be kept.
This can be kept. We improved the MenuItem of this code. The improved code is as follows:
[MenuItem("QFramework/2.Copy text to clipboard",false,2)] private static void MenuClicked2() { CommonUtil.CopyText("Keywords to copy"); }
In the code, the menu changes from the second level to the first level, and the order is added, which is the second order.
After the code is compiled, the menu is shown as follows:
The order is correct. But there is a split line between the first and second examples. Why is there such a split line?
It is possible that the first example is set in the order of - 10, while the second example is set in the order of 2. There is too much difference between them.
Let's change the order of the first example to 1. The changed code will not be shown.
After the change, the menu is as follows:
The horizontal line disappears, but this split line is an unexpected harvest. We can make good use of it. For example, we can use it to make a good division of our menu structure. We won't say anything else. Let's move on to sorting out.
The menu of the second example is sorted out. How to sort out the folders? Now the MenuItem method of this example is in previousfunctions in the eighth example, and the core API of the example: commonutil.copytext is also in the file of the eighth example.
In fact, it's very simple. Just extract these two parts. Write the MenuItem example method to the top of the implementation position of CommonUtil.CopyText method. Then extract CommonUtil from previousfunctions.cs and put it into the second example.
The code is as follows:
CommonUtil.cs
using UnityEngine; namespace QFramework { public class CommonUtil { #if UNITY_EDITOR [UnityEditor.MenuItem("QFramework/2.Copy text to clipboard", false, 2)] #endif private static void MenuClicked2() { CopyText("Keywords to copy"); } public static void CopyText(string text) { GUIUtility.systemCopyBuffer = text; } } }
The file directory of the code is as follows:
The second example is finished.
The reason for putting CommonUtil in a single folder is the same as the problem of "class name of method". The class of the method is unreasonable, so the class name of the method is strange. Similarly, the folder where the class is located is unreasonable, so the menu is unreasonable. Because the name of our menu corresponds to the name of the file directory one by one.
Although this point is not emphasized in the article, the author always implements this rule.
Today's content is here. See you next. Bye~
Reprint address: sandal notes: liangxiegame.com
More
-
QFramework address: https://github.com/liangxiegame/QFramework
-
QQ communication group: 623597263
-
Unity advanced class:
- Main training contents:
- Frame building training (first year)
- Follow case study Shader (first year)
- Incubation of sidelines (second and third year)
- Please check the details of rights and interests, teaching form, etc Small class product manual: https://liangxiegame.com/master/intro
- Main training contents:
-
Pay attention to the official account number: liangxiegame gets the first update notice and more free content.