Пример #1
0
    void OnEnable()
    {
        EditorApplication.playModeStateChanged += EditorApplicationOnPlayModeStateChanged;

        m_PuzzleLayout = target as PuzzleLayout;

        var assets = AssetDatabase.FindAssets("t:PuzzlePieceBox");

        foreach (var a in assets)
        {
            string         path    = AssetDatabase.GUIDToAssetPath(a);
            PuzzlePieceBox palette = AssetDatabase.LoadAssetAtPath <PuzzlePieceBox>(path);

            m_AvailablePieces.Add(palette);
        }

        m_PieceProperty = serializedObject.FindProperty("pieces");

        m_HighlightMaterial = new Material(Shader.Find("Hidden/Internal-Colored"));

        m_HighlightMaterial.color = new Color32(255, 238, 0, 255);
        m_HighlightMaterial.SetInt("ZTest", (int)UnityEngine.Rendering.CompareFunction.Always);

        foreach (var p in m_PuzzleLayout.pieces)
        {
            if (p != null)
            {
                p.gameObject.hideFlags = HideFlags.HideInHierarchy;
            }
        }
    }
Пример #2
0
    public override void OnInspectorGUI()
    {
        bool editing = GUILayout.Toggle(m_EditingLayout, "Editing Layout", "Button");

        if (editing != m_EditingLayout)
        {
            if (!editing)
            {//disabled editing, cleanup
                if (m_CurrentInstance != null)
                {
                    DestroyImmediate(m_CurrentInstance.gameObject);
                }
                m_CurrentBox      = null;
                m_CurrentInstance = null;
                m_SelectedPiece   = null;
            }

            m_EditingLayout = editing;
        }

        if (m_EditingLayout)
        {
            EditorGUILayout.HelpBox("Press R to change which connector the piece use to connect to other piece", MessageType.Info);

            EditorGUILayout.BeginHorizontal();

            int editingMode = GUILayout.Toolbar(m_EditingMode, new[] { "Add", "Remove" }, GUILayout.Width(120));
            if (editingMode != m_EditingMode)
            {
                if (editingMode == 1)
                {
                    if (m_CurrentInstance != null)
                    {
                        DestroyImmediate(m_CurrentInstance.gameObject);
                    }

                    m_SelectedPiece = null;
                }

                m_EditingMode = editingMode;
            }

            if (m_CurrentInstance != null)
            {
                EditorGUILayout.LabelField("Flip : ", GUILayout.Width(32));
                EditorGUI.BeginChangeCheck();

                bool flipX = GUILayout.Toggle(m_CurrentScale.x < 0, "X", "button", GUILayout.Width(32));
                bool flipY = GUILayout.Toggle(m_CurrentScale.y < 0, "Y", "button", GUILayout.Width(32));
                bool flipZ = GUILayout.Toggle(m_CurrentScale.z < 0, "Z", "button", GUILayout.Width(32));

                GUILayout.FlexibleSpace();

                if (EditorGUI.EndChangeCheck())
                {
                    m_CurrentScale = new Vector3(flipX ? -1 : 1, flipY ? -1 : 1, flipZ ? -1 : 1);
                    m_CurrentInstance.transform.localScale = m_CurrentScale;
                }
            }

            EditorGUILayout.EndHorizontal();

            //we repaint all scene view to be sure they get a notification so they can "steal" focus in edit mode
            SceneView.RepaintAll();
        }

        GUILayout.BeginHorizontal();

        GUILayout.BeginVertical();
        GUILayout.Label("Boxes");
        m_PuzzleBoxSelectionScroll = GUILayout.BeginScrollView(m_PuzzleBoxSelectionScroll);

        foreach (var p in m_AvailablePieces)
        {
            GUI.enabled = m_CurrentBox != p;
            if (GUILayout.Button(p.name))
            {
                if (!m_EditingLayout)
                {
                    m_EditingLayout = true;
                }

                m_CurrentBox = p;
            }
        }

        GUI.enabled = true;

        GUILayout.EndScrollView();
        GUILayout.EndVertical();

        GUILayout.BeginScrollView(m_PuzzleBoxSelectionScroll, GUILayout.Width(72 * 3));
        GUILayout.BeginVertical();

        if (m_CurrentBox != null)
        {
            bool horizontalOpen = false;

            for (int i = 0; i < m_CurrentBox.Pieces.Length; ++i)
            {
                PuzzlePiece part = m_CurrentBox.Pieces[i];

                if (i % 3 == 0 && i != 0)
                {
                    GUILayout.EndHorizontal();
                    horizontalOpen = false;
                }

                if (!horizontalOpen)
                {
                    GUILayout.BeginHorizontal();
                    horizontalOpen = true;
                }

                bool wrapValue = GUI.skin.button.wordWrap;

                var        renderers = part.gameObject.GetComponentInChildren <Renderer>();
                GUIContent content   = new GUIContent();

                if (renderers != null)
                {
                    content.image = AssetPreview.GetAssetPreview(part.gameObject);
                }
                else
                {
                    content.text             = part.gameObject.name;
                    GUI.skin.button.wordWrap = true;
                }

                GUI.enabled = part != m_SelectedPiece;
                if (GUILayout.Button(content, GUILayout.Width(64), GUILayout.Height(64)))
                {
                    if (m_EditingMode == 1)
                    {
                        m_EditingMode = 0;
                    }

                    m_SelectedPiece = part;

                    if (m_CurrentInstance != null)
                    {
                        DestroyImmediate(m_CurrentInstance.gameObject);
                    }

                    m_CurrentInstance = Instantiate(m_SelectedPiece, m_PuzzleLayout.transform);
                    m_CurrentInstance.gameObject.isStatic = false;
                    m_CurrentInstance.gameObject.tag      = "EditorOnly";
                    m_CurrentInstance.name = "TempInstance";
                    m_CurrentUsedExit      = 0;

                    m_CurrentInstance.transform.localScale = m_CurrentScale;
                }

                GUI.skin.button.wordWrap = wrapValue;
            }

            if (horizontalOpen)
            {
                GUILayout.EndHorizontal();
            }
        }

        GUI.enabled = true;
        GUILayout.EndVertical();
        GUILayout.EndScrollView();

        GUILayout.EndHorizontal();
    }