当前位置:   article > 正文

Unity生成简易代码工具_method has zero rva

method has zero rva

C#代码,可以自己编辑下,只要有规律,可以改成Lua的Panel代码,手写代码易错.特蛋痛的是lua写错编辑器还不报错
效果图
代码如下

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Text;
using System.IO;

public class ZSXGenerateCodeWindow : EditorWindow
{
    [MenuItem("Helps/生成初始代码")]
    public static void OpenWindow()
    {
        if (codeWindow == null)
            codeWindow = EditorWindow.GetWindow(typeof(ZSXGenerateCodeWindow)) as ZSXGenerateCodeWindow;
        codeWindow.Show();
    }

    private static ZSXGenerateCodeWindow codeWindow = null;
    //选择的根游戏体
    private GameObject root;

    void OnGUI()
    {
        DrawSelectUI();
        DrawFindWidget();
    }

    /// <summary>
    /// 绘制 选择要分析的UI
    /// </summary>
    private void DrawSelectUI()
    {
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("把Prefab拖到Hierarchy面板,Hierarchy面板的Prefab拖入UI框中,选中物体,选择要使用的组件");//, GUILayout.Width(100));
        using (EditorGUILayout.HorizontalScope hScope = new EditorGUILayout.HorizontalScope())
        {
            EditorGUILayout.LabelField("选择待处理UI:", GUILayout.Width(100));
            root = EditorGUILayout.ObjectField(root, typeof(GameObject), true) as GameObject;
        }
    }

    /// <summary>
    /// 绘制 查找UI控件
    /// </summary>
    private void DrawFindWidget()
    {
        EditorGUILayout.Space();
        using (EditorGUILayout.HorizontalScope hScope = new EditorGUILayout.HorizontalScope())
        {
            GUI.backgroundColor = Color.white;
            Rect rect = hScope.rect;
            rect.height = EditorGUIUtility.singleLineHeight;
            GUI.Box(rect, "");

            if (GUILayout.Button(" copy 组件属性 "))
            {
                GUIUtility.systemCopyBuffer = string.Join("\r\n", mAttributes);
            }

            if (GUILayout.Button(" copy 生成路径 "))
            {
                GUIUtility.systemCopyBuffer = string.Join("\r\n", mFinds);
            }
            if (GUILayout.Button(" copy 属性+路径 "))
            {
                var tt = string.Join("\r\n", mAttributes) + "\r\n" + string.Join("\r\n", mFinds);
                GUIUtility.systemCopyBuffer = tt;
            }
            if (GUILayout.Button(" 清空 "))
            {
                mFinds.Clear();
                mAttributes.Clear();
            }
        }
        Repaint();
        if (Selection.activeGameObject == null || root == null) return;
        var mComponents = Selection.activeGameObject.GetComponents<Component>();
        using (EditorGUILayout.HorizontalScope hScope = new EditorGUILayout.HorizontalScope())
        {
            GUI.backgroundColor = Color.white;
            Rect rect = hScope.rect;
            EditorGUILayout.LabelField("有以下的组件",GUILayout.Width(110));

            for (int i = 0; i < mComponents.Length; i++)
            {
                var tCom = mComponents[i];
                GenerateCode(tCom, root.transform);
            }
        }

        using (EditorGUILayout.VerticalScope hScope = new EditorGUILayout.VerticalScope())
        {
            GUI.backgroundColor = Color.white;
            Rect rect = hScope.rect;           
            var tGenUI = string.Format(@"using UnityEngine.UI;
using UnityEngine;
public class {0} : MonoBehaviour
    {{
       {1}
        void Awake()
        {{
          {2}
        }}
    }}
",root.name, string.Join("\r\n       ", mAttributes), string.Join("\r\n          ", mFinds));

            int height = 140;
            if (mAttributes.Count > 0)
                height = mAttributes.Count * 15 + mFinds.Count * 15 + 110;

            if (GUILayout.Button("生成脚本__"+root.name))
            {
                CreateCsUIScript(tGenUI);
            }
            EditorGUILayout.LabelField(tGenUI, GUILayout.Height(height));
        }
    }

    List<string> mAttributes = new List<string>();
    List<string> mFinds = new List<string>();

    string FindPath(Transform pParent, Transform pSelect)
    {
        if (pParent == null)
        {
            Debug.LogError("父 路径空了");
            return "";
        }
        if (pSelect == null)
        {
            Debug.LogError("选中 路径空了");
            return "";
        }

        var tStr = "";
        Transform temp = pSelect;
        for (int i = 0; i < 10; i++)
        {
            if (temp.transform == pParent)
            {
                break;
            }
            else
            {
                tStr = temp.transform.name + "/" + tStr;
                temp = temp.transform.parent;
            }
        }
        return "\"" + (tStr.TrimEnd('/')) + "\"";
    }

    void GenerateCode(Component tCom, Transform tPrefab)
    {
        var tGoName = tCom.gameObject.name;//挂件名(自己起的)
        var tTypeName = tCom.GetType().Name;//组件名
        if (tTypeName.Equals("CanvasRenderer")) return;
        var styleHas = new GUIStyle(GUI.skin.button);
        styleHas.normal.textColor = Color.red;
        var tAttName = tGoName + "_" + tTypeName;//将生成属性名

        var transStr = "private " + tTypeName + " " + tAttName + ";";
        var findStr =  tAttName + "=transform.Find(" + FindPath(tPrefab, tCom.transform) + ").GetComponent<" + tCom.GetType().Name + ">();";
        if (mAttributes.Contains(transStr) == false)
        {
            if (GUILayout.Button(tTypeName))
            {
                mAttributes.Add(transStr);
                mFinds.Add(findStr);
            }
        }
        else if (GUILayout.Button(tTypeName, styleHas))
        {
            mAttributes.Remove(transStr);
            mFinds.Remove(findStr);
        }
    }
    /// <summary>
    /// 生成C# UI脚本
    /// </summary>
    private void CreateCsUIScript(string pStr)
    {
        string path = EditorPrefs.GetString("create_script_folder", "");
        path = EditorUtility.SaveFilePanel("Create Script", path, root.name + ".cs", "cs");
        if (string.IsNullOrEmpty(path)) return;

        File.WriteAllText(path, pStr, new UTF8Encoding(false));
        AssetDatabase.Refresh();
        EditorPrefs.SetString("create_script_folder", path);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/115272
推荐阅读
相关标签
  

闽ICP备14008679号