private void UpdateTemplateDescriptionUI(SceneTemplateInfo newSceneTemplateInfo)
        {
            // Text info
            var sceneTemplateTitleLabel = rootVisualElement.Q <Label>(k_SceneTemplateTitleLabelName);

            if (sceneTemplateTitleLabel != null && newSceneTemplateInfo != null)
            {
                sceneTemplateTitleLabel.text = newSceneTemplateInfo.name;
            }

            var sceneTemplatePathLabel   = rootVisualElement.Q <Label>(k_SceneTemplatePathName);
            var sceneTemplatePathSection = rootVisualElement.Q(k_SceneTemplatePathSection);

            if (sceneTemplatePathLabel != null && newSceneTemplateInfo != null && sceneTemplatePathSection != null)
            {
                sceneTemplatePathLabel.text      = newSceneTemplateInfo.assetPath;
                sceneTemplatePathSection.visible = !string.IsNullOrEmpty(newSceneTemplateInfo.assetPath);
            }

            var sceneTemplateDescriptionLabel   = rootVisualElement.Q <Label>(k_SceneTemplateDescriptionName);
            var sceneTemplateDescriptionSection = rootVisualElement.Q(k_SceneTemplateDescriptionSection);

            if (sceneTemplateDescriptionLabel != null && newSceneTemplateInfo != null && sceneTemplateDescriptionSection != null)
            {
                sceneTemplateDescriptionLabel.text      = newSceneTemplateInfo.description;
                sceneTemplateDescriptionSection.visible = !string.IsNullOrEmpty(newSceneTemplateInfo.description);
            }

            // Thumbnail
            m_PreviewArea?.UpdatePreview(newSceneTemplateInfo?.thumbnail, newSceneTemplateInfo?.badge);
            m_PreviewArea?.UpdatePreviewAreaSize();
        }
        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);
        }
Пример #3
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);
        }