Example #1
0
        private VisualElement MakeThumbnailField(SerializedProperty thumbnailProperty, string label)
        {
            var propertyElement      = new VisualElement();
            var thumbnailObjectField = new PropertyField(thumbnailProperty, label);

            propertyElement.Add(thumbnailObjectField);

            m_PreviewArea = new SceneTemplatePreviewArea(k_ThumbnailAreaName, thumbnailProperty.objectReferenceValue as Texture2D, "No preview available");
            var previewAreaElement = m_PreviewArea.Element;

            previewAreaElement.AddToClassList(StyleSheetLoader.Styles.classUnityBaseField);
            propertyElement.Add(previewAreaElement);

            thumbnailObjectField.RegisterCallback <ChangeEvent <Object> >(e =>
            {
                m_PreviewArea.UpdatePreview(e.newValue as Texture2D);
                TriggerSceneTemplateModified();
            });

            // Snapshot header row
            var snapshotHeaderRowElement = new VisualElement();

            snapshotHeaderRowElement.AddToClassList(StyleSheetLoader.Styles.classUnityBaseField);
            propertyElement.Add(snapshotHeaderRowElement);
            var snapshotHeaderLabel = new Label("Snapshot");

            snapshotHeaderLabel.AddToClassList(StyleSheetLoader.Styles.classUnityLabel);
            snapshotHeaderRowElement.Add(snapshotHeaderLabel);

            // Snapshot button with dropdown
            var cameraNames = Camera.allCameras.Select(c => new SnapshotTargetInfo {
                Name = c.name, OnSnapshotAction = TakeSnapshotFromCamera
            }).ToList();

            cameraNames.Add(new SnapshotTargetInfo()); // Separator
            cameraNames.Add(new SnapshotTargetInfo {
                Name = "Game View", OnSnapshotAction = (info, property) => TakeSnapshotFromGameView(property)
            });
            var snapshotTargetPopup = new PopupField <SnapshotTargetInfo>("View", cameraNames, Camera.allCameras.Length == 0 ? 1 : 0);

            snapshotTargetPopup.formatListItemCallback      = info => info.Name;
            snapshotTargetPopup.formatSelectedValueCallback = info => info.Name;
            propertyElement.Add(snapshotTargetPopup);

            var snapshotSecondRowElement = new VisualElement()
            {
                name = k_SnapshotRowName
            };

            snapshotSecondRowElement.AddToClassList(StyleSheetLoader.Styles.classUnityBaseField);
            propertyElement.Add(snapshotSecondRowElement);
            var emptyLabel = new VisualElement();

            emptyLabel.AddToClassList(StyleSheetLoader.Styles.classUnityBaseFieldLabel);
            snapshotSecondRowElement.Add(emptyLabel);
            var snapshotButton = new Button(() =>
            {
                var targetInfo = snapshotTargetPopup.value;
                if (targetInfo.OnSnapshotAction == null)
                {
                    return;
                }
                targetInfo.OnSnapshotAction(targetInfo, thumbnailProperty);
            });

            snapshotButton.tooltip = k_SnapshotTooltip;
            snapshotButton.text    = k_SnapshotButtonLabel;
            snapshotButton.AddToClassList(StyleSheetLoader.Styles.classUnityBaseFieldInput);
            snapshotSecondRowElement.Add(snapshotButton);

            return(propertyElement);
        }
        private VisualElement MakeThumbnailField(SerializedProperty thumbnailProperty, SerializedProperty thumbnailBadgeProperty)
        {
            var propertyElement           = new VisualElement();
            var thumbnailBadgeObjectField = new PropertyField(thumbnailBadgeProperty, L10n.Tr("Badge"));

            thumbnailBadgeObjectField.tooltip = L10n.Tr("Scene template badge. Shown in New Scene Dialog.");
            propertyElement.Add(thumbnailBadgeObjectField);

            thumbnailBadgeObjectField.RegisterCallback <ChangeEvent <Object> >(e =>
            {
                TriggerSceneTemplateModified();
            });

            var thumbnailObjectField = new PropertyField(thumbnailProperty, L10n.Tr("Preview"));

            thumbnailObjectField.tooltip = L10n.Tr("Scene template thumbnail. Shown in New Scene Dialog.");
            propertyElement.Add(thumbnailObjectField);

            m_PreviewArea = new SceneTemplatePreviewArea(k_ThumbnailAreaName, thumbnailProperty.objectReferenceValue as Texture2D, null, "No preview available");
            var previewAreaElement = m_PreviewArea.Element;

            previewAreaElement.AddToClassList(Styles.classUnityBaseField);
            propertyElement.Add(previewAreaElement);

            thumbnailObjectField.RegisterCallback <ChangeEvent <Object> >(e =>
            {
                m_PreviewArea.UpdatePreview(e.newValue as Texture2D, null);
                TriggerSceneTemplateModified();
            });

            // Snapshot header row
            var snapshotHeaderRowElement = new VisualElement();

            snapshotHeaderRowElement.AddToClassList(Styles.classUnityBaseField);
            propertyElement.Add(snapshotHeaderRowElement);
            var snapshotHeaderLabel = new Label(L10n.Tr("Snapshot"));

            snapshotHeaderLabel.tooltip = L10n.Tr("Generate a Scene template thumbnail from a snapshot in Scene or Game view.");
            snapshotHeaderLabel.AddToClassList(Styles.classUnityLabel);
            snapshotHeaderRowElement.Add(snapshotHeaderLabel);

            // Snapshot button with dropdown
            var cameraNames = Camera.allCameras.Select(c => new SnapshotTargetInfo {
                Name = c.name, OnSnapshotAction = TakeSnapshotFromCamera
            }).ToList();

            cameraNames.Add(new SnapshotTargetInfo()); // Separator
            cameraNames.Add(new SnapshotTargetInfo {
                Name = L10n.Tr("Game View"), OnSnapshotAction = (info, callback) => TakeSnapshotFromGameView(callback)
            });
            var snapshotTargetPopup = new PopupField <SnapshotTargetInfo>(L10n.Tr("View"), cameraNames, Camera.allCameras.Length == 0 ? 1 : 0);

            snapshotTargetPopup.tooltip = L10n.Tr("View or Camera to use as the source of the snapshot.");
            snapshotTargetPopup.formatListItemCallback      = info => info.Name;
            snapshotTargetPopup.formatSelectedValueCallback = info => info.Name;
            snapshotTargetPopup.name = k_SnapshotTargetPopupName;
            propertyElement.Add(snapshotTargetPopup);

            var snapshotSecondRowElement = CreateEmptyLabelRow();

            propertyElement.Add(snapshotSecondRowElement);
            var snapshotButton = new Button(() =>
            {
                var targetInfo = snapshotTargetPopup.value;
                if (targetInfo.OnSnapshotAction == null)
                {
                    return;
                }

                targetInfo.OnSnapshotAction(targetInfo, null);
            });

            snapshotButton.tooltip = k_SnapshotTooltip;
            snapshotButton.text    = k_SnapshotButtonLabel;
            snapshotButton.AddToClassList(Styles.classUnityBaseFieldInput);
            snapshotSecondRowElement.Add(snapshotButton);

            return(propertyElement);
        }
        private void CreateTemplateDescriptionUI(VisualElement rootContainer)
        {
            rootContainer.style.flexDirection = FlexDirection.Column;

            // Thumbnail container
            m_PreviewArea = new SceneTemplatePreviewArea(k_SceneTemplateThumbnailName, m_LastSelectedTemplate?.thumbnail, m_LastSelectedTemplate?.badge, L10n.Tr("No preview thumbnail available"));
            var thumbnailElement = m_PreviewArea.Element;

            rootContainer.Add(thumbnailElement);

            rootContainer.RegisterCallback <GeometryChangedEvent>(evt => m_PreviewArea?.UpdatePreviewAreaSize());

            // Text container
            var sceneTitleLabel = new Label();

            sceneTitleLabel.name = k_SceneTemplateTitleLabelName;
            sceneTitleLabel.AddToClassList(Styles.classWrappingText);
            rootContainer.Add(sceneTitleLabel);

            var assetPathSection = new VisualElement();

            assetPathSection.name = k_SceneTemplatePathSection;
            {
                var scenePathLabel = new Label();
                scenePathLabel.name = k_SceneTemplatePathName;
                scenePathLabel.AddToClassList(Styles.classWrappingText);
                assetPathSection.Add(scenePathLabel);

                var editLocateRow = new VisualElement();
                editLocateRow.style.flexDirection = FlexDirection.Row;
                {
                    var scenePathLocate = new Label();
                    scenePathLocate.text = L10n.Tr("Locate");
                    scenePathLocate.AddToClassList(Styles.classTextLink);
                    scenePathLocate.RegisterCallback <MouseDownEvent>(e =>
                    {
                        if (string.IsNullOrEmpty(scenePathLabel.text))
                        {
                            return;
                        }

                        var asset = AssetDatabase.LoadAssetAtPath <SceneTemplateAsset>(scenePathLabel.text);
                        if (!asset)
                        {
                            return;
                        }

                        EditorApplication.delayCall += () =>
                        {
                            EditorWindow.FocusWindowIfItsOpen(typeof(ProjectBrowser));
                            EditorApplication.delayCall += () => EditorGUIUtility.PingObject(asset);
                        };
                    });
                    editLocateRow.Add(scenePathLocate);

                    editLocateRow.Add(new Label()
                    {
                        text = " | "
                    });

                    var scenePathEdit = new Label();
                    scenePathEdit.name = k_SceneTemplateEditTemplateButtonName;
                    scenePathEdit.text = L10n.Tr("Edit");
                    scenePathEdit.AddToClassList(Styles.classTextLink);
                    scenePathEdit.RegisterCallback <MouseDownEvent>(e =>
                    {
                        OnEditTemplate(m_LastSelectedTemplate);
                    });
                    editLocateRow.Add(scenePathEdit);
                }
                assetPathSection.Add(editLocateRow);
            }
            rootContainer.Add(assetPathSection);

            var descriptionSection = new VisualElement();

            descriptionSection.name = k_SceneTemplateDescriptionSection;
            {
                var descriptionLabel = new Label();
                descriptionLabel.AddToClassList(Styles.classHeaderLabel);
                descriptionLabel.text = L10n.Tr("Description");
                descriptionSection.Add(descriptionLabel);

                var sceneDescriptionLabel = new Label();
                sceneDescriptionLabel.AddToClassList(Styles.classWrappingText);
                sceneDescriptionLabel.name = k_SceneTemplateDescriptionName;
                descriptionSection.Add(sceneDescriptionLabel);
            }
            rootContainer.Add(descriptionSection);
        }