void DrawPreviewWindow(BlockData block)
    {
        GUILayoutUtility.GetRect(144, 10f, GUILayout.ExpandWidth(false));
        var rect = GUILayoutUtility.GetRect(144f, 144f, GUILayout.ExpandWidth(false));

        if (previewCamera == null)
        {
            GameObject camObj = new GameObject("PreviewCamera");
            camObj.hideFlags          = HideFlags.HideAndDontSave;
            previewCamera             = camObj.AddComponent <Camera>();
            previewCamera.clearFlags  = CameraClearFlags.SolidColor;
            previewCamera.cullingMask = LayerMask.GetMask("EditorBlock");

            if (EditorGUIUtility.isProSkin)
            {
                previewCamera.backgroundColor = (Color)(new Color32(51, 51, 51, 0));
            }
            else
            {
                previewCamera.backgroundColor = (Color)(new Color32(135, 135, 135, 0));
            }

            previewCamera.orthographic     = true;
            previewCamera.orthographicSize = 1f;
            previewCamera.enabled          = false;
        }
        if (previewMesh == null)
        {
            previewMesh = new GameObject("PreviewMesh");
            MeshFilter meshFilter = previewMesh.AddComponent <MeshFilter>();
            previewMesh.AddComponent <MeshRenderer>();
            meshFilter.mesh   = new Mesh();
            previewMesh.layer = LayerMask.NameToLayer("EditorBlock");

            MeshUtility.CreatePreviewCube(meshFilter);

            previewMesh.hideFlags          = HideFlags.HideAndDontSave;
            previewMesh.transform.position = previewCamera.gameObject.transform.position + previewCamera.gameObject.transform.forward * 2f;
            previewMesh.transform.Rotate(-35f, 0f, -35f);
        }
        if (previewMaterials == null)
        {
            previewMaterials = new Material[6];
            for (int j = 0; j < 6; j++)
            {
                previewMaterials[j]           = new Material(Shader.Find("Unlit/Transparent"));
                previewMaterials[j].hideFlags = HideFlags.HideAndDontSave;
            }
            previewMesh.GetComponent <MeshRenderer>().materials = previewMaterials;
            UpdatePreviewModelTextures(block);
        }
        if (previewCamera)
        {
            previewCamera.Render();
            Handles.DrawCamera(rect, previewCamera, DrawCameraMode.Normal);

            EditorGUILayout.BeginHorizontal();
            bool leftButton  = GUILayout.RepeatButton(EditorGUIUtility.IconContent("Animation.Reverse"), EditorStyles.miniButton, GUILayout.ExpandWidth(false), GUILayout.Height(16f));
            bool rightButton = GUILayout.RepeatButton(EditorGUIUtility.IconContent("Animation.Forward"), EditorStyles.miniButton, GUILayout.ExpandWidth(false), GUILayout.Height(16f));
            if (leftButton)
            {
                manualSpinPreview = -1;
                spinPreview       = false;
            }
            else if (rightButton)
            {
                manualSpinPreview = 1;
                spinPreview       = false;
            }
            else
            {
                manualSpinPreview = 0;
            }
            if (GUILayout.Button(spinPreview ? EditorGUIUtility.IconContent("Animation.Pause") : EditorGUIUtility.IconContent("Animation.Play"), EditorStyles.miniButton, GUILayout.ExpandWidth(false), GUILayout.Height(16f)))
            {
                spinPreview = !spinPreview;
            }
            if (GUILayout.Button(showPreviewMeshLegend ? "Textured" : " Legend ", EditorStyles.miniButton, GUILayout.ExpandWidth(false), GUILayout.Height(16f)))
            {
                showPreviewMeshLegend = !showPreviewMeshLegend;
                UpdatePreviewModelTextures(block);
            }

            EditorGUILayout.EndHorizontal();
        }
        else
        {
            Debug.LogError("No preview Camera");
        }
    }
Esempio n. 2
0
    void UpdatePreviewModel(ItemData item)
    {
        if (previewMesh != null)
        {
            DestroyImmediate(previewMesh);
        }

        if (item == null)
        {
            return;
        }

        if (item.modelType == ItemData.ModelType.Cube)
        {
            if (item.placeableBlockID >= blockDatabase.blocks.Count)
            {
                return;
            }

            BlockData block = blockDatabase.blocks[item.placeableBlockID];

            previewMesh = new GameObject("PreviewMesh");
            MeshFilter   meshFilter   = previewMesh.AddComponent <MeshFilter>();
            MeshRenderer meshRenderer = previewMesh.AddComponent <MeshRenderer>();
            meshRenderer.material = previewMaterial;
            meshFilter.mesh       = new Mesh();

            MeshUtility.CreatePreviewCube(meshFilter);

            meshRenderer.materials = previewMaterials;

            if (previewMaterials != null)
            {
                for (int j = 0; j < 6; j++)
                {
                    if (block.textures[j] == null)
                    {
                        previewMaterials[j].mainTexture = EditorGUIUtility.IconContent(previewMeshIcons[j]).image;
                    }
                    else
                    {
                        previewMaterials[j].mainTexture = block.textures[j];
                    }
                }
            }

            previewMesh.hideFlags          = HideFlags.HideAndDontSave;
            previewMesh.transform.position = previewCamera.gameObject.transform.position + previewCamera.gameObject.transform.forward * 2f;
            previewMesh.transform.rotation = Quaternion.Euler(-35f, rotationY, -35f);
        }
        else if (item.modelType == ItemData.ModelType.Sprite)
        {
            if (item.sprite == null)
            {
                return;
            }
            previewMesh = CreateSprite(item.sprite);
            previewMesh.transform.rotation = Quaternion.Euler(0f, rotationY, 0f);
            previewMesh.transform.position =
                previewCamera.gameObject.transform.position
                + previewCamera.gameObject.transform.forward * 2f
                + previewCamera.gameObject.transform.up * -0.5f;
        }
        else if (item.modelType == ItemData.ModelType.Custom)
        {
            if (item.customModel == null)
            {
                return;
            }
            previewMesh = GameObject.Instantiate(item.customModel);
            previewMesh.transform.position =
                previewCamera.gameObject.transform.position
                + previewCamera.gameObject.transform.forward * 2f;
            previewMesh.transform.rotation = Quaternion.Euler(-35f, rotationY, -35f);
        }
        previewMesh.layer = LayerMask.NameToLayer("EditorItem");
    }