/// <summary>
    /// 生成连接元素声明代码
    /// </summary>
    /// <param name="link"></param>
    protected static void CopyElementDeclare(UIFormLink link)
    {
        if (link == null)
        {
            return;
        }
        ElementData[] elementData = GetElementData(link);
        if (elementData == null)
        {
            return;
        }
        // 输出
        string content = string.Empty;

        for (int i = 0; i < elementData.Length; i++)
        {
            content += "private const int " + elementData[i].IndexName + " = " + i + ";\n";
        }

        content += "\r\n";
        for (int i = 0; i < elementData.Length; i++)
        {
            content += "private " + elementData[i].TypeName + ' ' + elementData[i].VariantName + ";\n";
        }
        if (!string.IsNullOrEmpty(content))
        {
            TextEditor te = new TextEditor();
            te.text = content;
            te.OnFocus();
            te.Copy();
        }
    }
    /// <summary>
    /// 生成反初始化代码
    /// </summary>
    /// <param name="prefab"></param>
    protected static void CopyElementUnIninitializeLua(UIFormLink link)
    {
        if (link == null)
        {
            return;
        }
        ElementData[] elementData = GetElementData(link);
        if (elementData == null)
        {
            return;
        }
        // 输出
        string content = string.Empty;

        for (int i = 0; i < elementData.Length; i++)
        {
            ElementData data = elementData[i];
            content += "self." + data.VariantName + " = false\n";
        }
        if (!string.IsNullOrEmpty(content))
        {
            TextEditor te = new TextEditor();
            te.text = content;
            te.OnFocus();
            te.Copy();
        }
    }
    /// <summary>
    /// 生成连接元素初始化代码
    /// </summary>
    /// <param name="link"></param>
    protected static void CopyElementInitialize(UIFormLink link)
    {
        if (link == null)
        {
            return;
        }
        ElementData[] elementData = GetElementData(link);
        if (elementData == null)
        {
            return;
        }
        // 输出
        string content = string.Empty;

        for (int i = 0; i < elementData.Length; i++)
        {
            ElementData data = elementData[i];

            content += data.VariantName + " = ";

            if (data.TypeName == "GameObject")
            {
                content += "GetGameObject(";
                content += data.IndexName;
                content += ");\r\n";
            }
            else
            {
                content += "GetComponent(";
                content += data.IndexName;
                content += ")";
                content += "as " + data.TypeName;
                content += ";\r\n";
            }
        }
        if (!string.IsNullOrEmpty(content))
        {
            TextEditor te = new TextEditor();
            te.text = content;
            te.OnFocus();
            te.Copy();
        }
    }
    /// <summary>
    /// 生成Lua全部代码
    /// </summary>
    /// <param name="link"></param>
    protected static void CopyElementWholeLua(UIFormLink link)
    {
        if (link == null)
        {
            return;
        }
        ElementData[] elementData = GetElementData(link);
        if (elementData == null)
        {
            return;
        }
        // 输出
        string content   = string.Empty;
        string className = link.gameObject.name + "Class";
        // lua第一行声明
        string declareStr = string.Format("local {0} = DeclareClass(\"{1}\", ClassLib.UIFormClass)\n", className, className);

        content += declareStr;
        // lua ctor 函数
        string ctorCotent = string.Empty;

        for (int i = 0; i < elementData.Length; i++)
        {
            ctorCotent += "\tself." + elementData[i].IndexName + " = " + i + ";\r\n";
        }
        ctorCotent += "\r\n";
        for (int i = 0; i < elementData.Length; i++)
        {
            ctorCotent += "\tself." + elementData[i].VariantName + " = false" + "\r\n";
        }

        content += GetLuaFunctionStr(className, "ctor", ctorCotent);
        // lua vGetPath 函数
        content += GetLuaFunctionStr(className, "vGetPath", "\treturn \'moduleName/" + link.gameObject.name + "\'");
        // lua vOnResourceLoaded
        string funcContent = string.Empty;

        for (int i = 0; i < elementData.Length; i++)
        {
            ElementData data = elementData[i];

            funcContent += "\tself." + data.VariantName + " = ";

            if (data.TypeName == "GameObject")
            {
                funcContent += "self:GetGameObject(self." + data.IndexName + ")\n";
            }
            else
            {
                funcContent += "self:GetComponent(self." + data.IndexName + ")\n";
            }
        }

        content += GetLuaFunctionStr(className, "vOnResourceLoaded", funcContent);

        //vOnResourceUnLoaded
        funcContent = string.Empty;
        for (int i = 0; i < elementData.Length; i++)
        {
            funcContent += "\tself." + elementData[i].VariantName + " = false" + "\r\n";
        }
        content += GetLuaFunctionStr(className, "vOnResourceUnLoaded", funcContent);
        //vOnInitialize
        content += GetLuaFunctionStr(className, "vOnInitialize", "", "argument");
        //vOnUninitialize
        content += GetLuaFunctionStr(className, "vOnUninitialize", "");

        if (!string.IsNullOrEmpty(content))
        {
            TextEditor te = new TextEditor();
            te.text = content;
            te.OnFocus();
            te.Copy();
        }
    }
    private static ElementData[] GetElementData(UIFormLink prefab)
    {
        if (prefab == null || prefab.Elements == null || prefab.Elements.Length == 0)
        {
            EditorUtility.DisplayDialog("错误", "没有节点", "确定");
            return(null);
        }

        ElementData[] elementData = new ElementData[prefab.Elements.Length];

        for (int i = 0; i < prefab.Elements.Length; i++)
        {
            Component  component = prefab.Elements[i].Component;
            GameObject go        = component.gameObject;
            string     goName    = go.name;
            if (string.IsNullOrEmpty(goName))
            {
                EditorUtility.DisplayDialog("错误", "节点名为空", "确定");
                return(null);
            }

            goName = goName.Trim();
            if (string.IsNullOrEmpty(goName))
            {
                EditorUtility.DisplayDialog("错误", "节点名为空", "确定");
                return(null);
            }

            // 索引
            ElementData data;

            // 类型和变量
            data.TypeName = component.GetType().Name;
            string shortTypeName = data.TypeName;
            if (data.TypeName.IndexOf("UI") == 0)
            {
                shortTypeName = data.TypeName.Substring(2);
            }

            // index
            data.IndexName = _prefix + char.ToUpper(goName[0]) + goName.Substring(1) + shortTypeName + "Index";

            // variant name
            if (string.IsNullOrEmpty(_prefix))
            {
                data.VariantName = "_" + char.ToLower(goName[0]) + goName.Substring(1) + shortTypeName;
            }
            else
            {
                data.VariantName = _prefix + char.ToUpper(goName[0]) + goName.Substring(1) + shortTypeName;
            }

            // 名字重复
            for (int j = 0; j < i; j++)
            {
                if (elementData[j].IndexName.Equals(data.IndexName, StringComparison.OrdinalIgnoreCase) ||
                    elementData[j].VariantName.Equals(data.VariantName, StringComparison.OrdinalIgnoreCase))
                {
                    EditorUtility.DisplayDialog(
                        "错误",
                        string.Format("节点名重复,{0},{1}", data.IndexName, data.VariantName),
                        "确定");
                    return(null);
                }
            }

            //
            elementData[i] = data;
        }

        return(elementData);
    }
Exemplo n.º 6
0
    //private bool _aniExpand = false;

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        // EditorGUI.BeginDisabledGroup(true);
        EditorGUILayout.BeginVertical("box");
        SerializedProperty sp = serializedObject.FindProperty("ReferenceResolution");

        // EditorGUILayout.Vector2Field("设计分辨率", sp.vector2Value);
        // sp = serializedObject.FindProperty("IsSingleton");
        EditorGUILayout.PropertyField(sp, new GUIContent("设计分辨率"));
        // EditorGUI.EndDisabledGroup();

        sp = serializedObject.FindProperty("IsSingleton");
        EditorGUILayout.PropertyField(sp, new GUIContent("是否单例"));

        sp = serializedObject.FindProperty("IsModal");
        EditorGUILayout.PropertyField(sp, new GUIContent("是否模态"));

        sp = serializedObject.FindProperty("ShowPriority");
        EditorGUILayout.PropertyField(sp, new GUIContent("显示优先级"));

        sp = serializedObject.FindProperty("GroupId");
        EditorGUILayout.PropertyField(sp, new GUIContent("显示组"));

        sp = serializedObject.FindProperty("IsFullScreenBG");
        EditorGUILayout.PropertyField(sp, new GUIContent("全屏背景"));

        sp = serializedObject.FindProperty("IsDisableInput");
        EditorGUILayout.PropertyField(sp, new GUIContent("禁止输入"));

        sp = serializedObject.FindProperty("IsHideUnderForms");
        EditorGUILayout.PropertyField(sp, new GUIContent("隐藏下面Form"));

        sp = serializedObject.FindProperty("IsAlwaysKeepVisible");
        EditorGUILayout.PropertyField(sp, new GUIContent("始终保持可见"));

        EditorGUILayout.EndVertical();

        /*
         * _audioExpand=EditorGUIHelper.DrawHead("音效", _audioExpand);
         * if (_audioExpand)
         * {
         *  //todo 拖动填充
         *  GUILayout.BeginVertical("box");
         *  {
         *      sp = serializedObject.FindProperty("OpenedWwiseEvents");
         *      EditorGUIHelper.ArrayField(sp, "打开");
         *      sp = serializedObject.FindProperty("ClosedWwiseEvents");
         *      EditorGUIHelper.ArrayField(sp, "关闭");
         *  }
         *  EditorGUILayout.EndVertical();
         * }
         */
        /*
         * _aniExpand = EditorGUIHelper.DrawHead("动画", _aniExpand);
         * if (_aniExpand)
         * {
         *  //todo 拖动填充
         *  GUILayout.BeginVertical("box");
         *  {
         *      sp = serializedObject.FindProperty("FadeInAnimationType");
         *      EditorGUILayout.PropertyField(sp, new GUIContent("FadeIn动画类型"));
         *      sp = serializedObject.FindProperty("FadeInAnimationName");
         *      EditorGUILayout.PropertyField(sp, new GUIContent("FadeIn动画名称"));
         *
         *      sp = serializedObject.FindProperty("FadeOutAnimationType");
         *      EditorGUILayout.PropertyField(sp, new GUIContent("FadeOut动画类型"));
         *      sp = serializedObject.FindProperty("FadeOutAnimationName");
         *      EditorGUILayout.PropertyField(sp, new GUIContent("FadeOut动画名称"));
         *  }
         *  EditorGUILayout.EndVertical();
         * }*/

        //连接元素
        base.OnDrawElements();

        //
        serializedObject.ApplyModifiedProperties();

        //添加自动生成
        _genExpand = EditorGUIHelper.DrawHead("辅助", _genExpand);
        if (_genExpand)
        {
            GUILayout.BeginVertical("box");
            {
                EditorGUI.BeginDisabledGroup(true);
                _prefix = EditorGUILayout.TextField("引用前缀", _prefix);
                EditorGUI.EndDisabledGroup();

                if (GUILayout.Button("复制节点定义"))
                {
                    UIFormLink pl = target as UIFormLink;
                    CopyElementDeclare(pl);
                }
                if (GUILayout.Button("复制节点初始化"))
                {
                    UIFormLink pl = target as UIFormLink;
                    CopyElementInitialize(pl);
                }
                if (GUILayout.Button("复制节点反初始化"))
                {
                    UIFormLink pl = target as UIFormLink;
                    CopyElementUnIninitialize(pl);
                }

                if (GUILayout.Button("复制Lua节点定义"))
                {
                    UIFormLink pl = target as UIFormLink;
                    CopyElementDeclareLua(pl);
                }

                if (GUILayout.Button("复制Lua节点初始化"))
                {
                    UIFormLink pl = target as UIFormLink;
                    CopyElementInitializeLua(pl);
                }

                if (GUILayout.Button("复制Lua节点反初始化"))
                {
                    UIFormLink pl = target as UIFormLink;
                    CopyElementUnIninitializeLua(pl);
                }

                if (GUILayout.Button("复制Lua全部初始化代码"))
                {
                    UIFormLink pl = target as UIFormLink;
                    CopyElementWholeLua(pl);
                }
            }
            EditorGUILayout.EndVertical();
        }
    }