Exemple #1
0
    /// <summary>
    /// Convenience method that works without warnings in both Unity 3 and 4.
    /// </summary>

    static public void RegisterUndo(UnityEngine.Object obj, string name)
    {
#if UNITY_EDITOR
        UnityEditor.Undo.RecordObject(obj, name);
        EngineTools.SetDirty(obj);
#endif
    }
        public override void OnInspectorGUI()
        {
            UIPanelRoot root = target as UIPanelRoot;

            if (root == null)
            {
                return;
            }

            EditorGUIUtility.labelWidth = 120;
            Transform parentTrans = root.transform.parent;
            bool      isRoot      = true;

            if (parentTrans)
            {
                UIPanelRoot parentRoot = parentTrans.GetComponentInParent <UIPanelRoot>();
                if (parentRoot)
                {
                    isRoot = false;
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("Field"));
                }
            }

            SerializedProperty requirePath = serializedObject.FindProperty("LuaRequirePath");

            //            requirePath.stringValue = EditorGUILayout.TextField("LuaRequirePath" , requirePath.stringValue);
            if (!isRoot)
            {
                EditorGUILayout.PropertyField(requirePath);
            }

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("FilePath", GUILayout.Width(120)))
            {
                string selectFilePath = EditorUtility.SaveFilePanel("Select Folder", Path.Combine(Application.dataPath, root.FilePath),
                                                                    root.gameObject.name, "lua");
                root.FilePath = selectFilePath.Replace(Application.dataPath, "/").Replace("//", "");
                root.Field    = Path.GetFileNameWithoutExtension(selectFilePath);

                string requireLuaPath = root.FilePath.Replace("LuaFramework/Lua/", "");
                serializedObject.FindProperty("FilePath").stringValue = root.FilePath;
                serializedObject.FindProperty("Field").stringValue    = root.Field;
                requirePath.stringValue = requireLuaPath.Replace(".lua", "").Replace("/", ".");

                if (isRoot && !root.Field.Equals(root.gameObject.name.Trim()))
                {
                    Debug.LogError(string.Format("UI根结点与Lua脚本名称不一致!{0}~={1}", root.gameObject.name, root.Field));
                    EditorUtility.DisplayDialog("错误", "请保证UI根结点与Lua脚本名字一致!!", "OK");
                }
            }
            GUILayout.TextArea(root.FilePath);
            EditorGUILayout.EndHorizontal();

            GUI.color = Color.green;
            if (isRoot && GUILayout.Button("Build"))
            {
                if (PrefabUtility.GetPrefabType(root.gameObject) != PrefabType.PrefabInstance)
                {
                    EditorUtility.DisplayDialog("提示", "请先存储UI为Prefab\n注意格式为(xxxPanel.prefab)", "OK");
                    return;
                }

                XmlDocument    doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                doc.AppendChild(dec);

                if (root.gameObject.layer == LayerMask.NameToLayer("Default"))
                {
                    EngineTools.SetLayer(root.gameObject, LayerMask.NameToLayer("UI"));
                }
                bool flag = root.BuildPanel(doc, root.gameObject, true);

                if (flag == false)
                {
                    return;
                }

                string genDir = Path.Combine(Application.dataPath, GenLuaConst.GenLogFolder);
                if (!Directory.Exists(genDir))
                {
                    Directory.CreateDirectory(genDir);
                }

                string filePath = Path.Combine(genDir, root.name + ".xml");
                doc.Save(filePath);

                Debug.Log("<color=#2fd95b>Build Success !</color>");
                EditorUtility.DisplayDialog("恭喜", "Build Success !", "OK");
            }
            GUI.color = Color.white;

            serializedObject.ApplyModifiedProperties();
        }
Exemple #3
0
    public override void WrapContent()
    {
        float extents = itemSize * (mChildren.Count / RowNum) * 0.5f;

        Vector3[] corners = mPanel.worldCorners;

        for (int i = 0; i < 4; ++i)
        {
            Vector3 v = corners[i];
            v          = mTrans.InverseTransformPoint(v);
            corners[i] = v;
        }

        Vector3 center         = Vector3.Lerp(corners[0], corners[2], 0.5f);
        bool    allWithinRange = true;
        float   ext2           = extents * 2f;

        if (mHorizontal)
        {
            float min = corners[0].x - itemSize;
            float max = corners[2].x + itemSize;

            for (int i = 0, imax = mChildren.Count; i < imax; ++i)
            {
                Transform t        = mChildren[i];
                float     distance = t.localPosition.x - center.x;

                if (distance < -extents)
                {
                    Vector3 pos = t.localPosition;
                    pos.x   += ext2;
                    distance = pos.x - center.x;
                    int realIndex = Mathf.RoundToInt(pos.x / itemSize);

                    if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex))
                    {
                        t.localPosition = pos;
                        UpdateItem(t, i);
                    }
                    else
                    {
                        allWithinRange = false;
                    }
                }
                else if (distance > extents)
                {
                    Vector3 pos = t.localPosition;
                    pos.x   -= ext2;
                    distance = pos.x - center.x;
                    int realIndex = Mathf.RoundToInt(pos.x / itemSize);

                    if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex))
                    {
                        t.localPosition = pos;
                        UpdateItem(t, i);
                    }
                    else
                    {
                        allWithinRange = false;
                    }
                }
                else if (mFirstTime)
                {
                    UpdateItem(t, i);
                }

                if (cullContent)
                {
                    distance += mPanel.clipOffset.x - mTrans.localPosition.x;
                    if (!UICamera.IsPressed(t.gameObject))
                    {
                        EngineTools.SetActive(t.gameObject, (distance > min && distance < max), false);
                    }
                }
            }
        }
        else
        {
            float min = corners[0].y - itemSize;
            float max = corners[2].y + itemSize;

            for (int i = 0, imax = mChildren.Count; i < imax; ++i)
            {
                Transform t        = mChildren[i];
                float     distance = t.localPosition.y - center.y;

                if (distance < -extents)
                {
                    Vector3 pos = t.localPosition;
                    pos.y   += ext2;
                    distance = pos.y - center.y;
                    int realIndex = Mathf.RoundToInt(pos.y / itemSize);

                    if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex))
                    {
                        t.localPosition = pos;
                        UpdateItem(t, i);
                    }
                    else
                    {
                        allWithinRange = false;
                    }
                }
                else if (distance > extents)
                {
                    Vector3 pos = t.localPosition;
                    pos.y   -= ext2;
                    distance = pos.y - center.y;
                    int realIndex = Mathf.RoundToInt(pos.y / itemSize);

                    if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex))
                    {
                        t.localPosition = pos;
                        UpdateItem(t, i);
                    }
                    else
                    {
                        allWithinRange = false;
                    }
                }
                else if (mFirstTime)
                {
                    UpdateItem(t, i);
                }

                if (cullContent)
                {
                    distance += mPanel.clipOffset.y - mTrans.localPosition.y;
                    if (!UICamera.IsPressed(t.gameObject))
                    {
                        EngineTools.SetActive(t.gameObject, (distance > min && distance < max), false);
                    }
                }
            }
        }
        mScroll.restrictWithinPanel = !allWithinRange;
        mScroll.InvalidateBounds();
    }