This article is fromInspoy miscellaneous collection | food chicken inspoy learning record

preface

Project this thing really is still do up will find pit, try to fill as early as possible

View Prefab

The root node of the UI default body vwTest is a Panel control, the content is a background box, it should be changed, the control containing specific content should not be in the root node, now change the root node to transparent Panel, size of the entire View. The background box is placed under the root node.

0501


0502

Add the UI

To make it easier to add UI, I’ve added a few static methods to SFSceneManager that simplify the steps of adding UI by specifying the default body, the parent’s transform, and the hierarchy order (the bigger the more forward)

static public GameObject addView(GameObject prefab, Transform trans = null.int sibIdx = - 1)
{
    if (prefab == null)
    {
        SFUtils.logWarning("Prefab is empty");
        return null;
    }
    Transform parent = trans;
    if (parent == null)
    {
        parent = SFSceneManager.uiRoot.transform;
    }
    var GO = GameObject.Instantiate(prefab, parent) as GameObject;
    if (zOrder >= 0)
    {
        GO.SetSiblingIndex(sibIdx);
    }
    return GO;
}
static public GameObject addView(string viewName, Transform trans = null.int sibIdx = - 1)
{
    var prefab = Resources.Load(viewName) as GameObject;
    if (prefab == null)
    {
        SFUtils.logWarning(string.Format("View :{0}", viewName));
        return null;
    }
    return SFSceneManager.addView(prefab, trans, sibIdx);
}Copy the code

If you provide only the first parameter, it will be added to the top of the entire UI by default. You can specify a custom node to be added to, or you can specify the hierarchy

UI code generation

The envisioned workflow goes like this: UI designers modify or create UI Prefab, right click on Prefab in the Project View, and there is a menu item to export generated code. Click on it to generate code with one click and automatically mount the View script to Prefab. To achieve this effect, Unity’s editor must be extended. Unity provides a rich interface for editor extensions, such as adding menu items, customizing the Inspector panel, and even customizing the auxiliary UI in the Scene view. Create a C# script in Assets with any name you like, and write a class with any name you like:

public class SFUIExporterMenu
{
    [MenuItem("Assets/SF/Export UI")]
    private static void exportUI()
    { generateUICode(Selection.activeGameObject); }}Copy the code

Selection. ActiveGameObject said that the current in the Project view the selected Prefab, if the selected is not a Prefab is null. The MenuItem property indicates that this method is used to extend the MenuItem. The “Assets/SF/Export UI” parameter indicates the location of the custom MenuItem. To add it to Assets, right-click on Asset to see the corresponding MenuItem

0503


[MenuItem("Assets/SF/Export UI".true)]
private static bool exportUIValidation()
{
    var GO = Selection.activeGameObject;
    returnGO ! =null && GO.name.Substring(0.2) = ="vw";
}Copy the code

The second argument to the property, true, indicates that the method is used to validate, returning true for legal and false for illegal. The code is then generated from Prefab and saved

string viewName = prefab.name.Substring(2);
string viewFilepath = string.Format("Assets/Scripts/UI/SF%sView.cs", viewName);
var viewFile = new FileInfo(viewFilepath);
var sw = viewFile.CreateText();
sw.Write(viewContent);
sw.Close();
// The logic for generating viewContent is complicated and will not be posted in this articleCopy the code

Automatically mount scripts

Finally, of course, you need to mount the generated View script on Prefab

string componentName = "SF" + viewName + "View";
AssetDatabase.Refresh();
var component = prefab.GetComponent(componentName);
if (component == null)
{
   prefab.AddComponent(getTypeByName(componentName));
}Copy the code

Because component.addComponent (string) is officially deprecated in version 5.0, we have to find other ways to save the country. The following method refers to the USTR blog of the su Sitcom. This method is bound to be inefficient. This is especially true when the project is larger, but at least the extensions for the editor will not be executed during the run-time phase of the game and will not be too frequent, so a slight performance loss is acceptable

private static Type getTypeByName(string className)
{
    foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        foreach (Type type in assembly.GetTypes())
        {
            if (type.Name == className)
            {
                returntype; }}}return null;
}Copy the code

other

Expand the editor found a fun thing, inheritance UnityEditor. AssetModificationProcessor and implement OnWillCreateAsset () method can be notified when creating the Assets, You can use this method to comment out header file information for scripts you create later. The code is as follows:

public class SFScriptHeaderGenerator : UnityEditor.AssetModificationProcessor { static private string header = "/**\n" + " * Created on ##DateTime## by ##UserName##\n" + " * All rights reserved.\n" + " */\n\n"; public static void OnWillCreateAsset(string path) { path = path.Replace(".meta", ""); if (path.EndsWith(".cs")) { string fullText = header; fullText = fullText.Replace("##DateTime##", System.DateTime.Now.ToString("yyyy/MM/dd")); fullText = fullText.Replace("##UserName##", System.Environment.UserName); fullText += File.ReadAllText(path); File.WriteAllText(path, fullText); }}}Copy the code

The effect is as follows:

0504

The complete code

The code snippet posted above only contains key parts due to space limitations, the full code can be found on my Github