Exemplo n.º 1
0
        public static bool CanGenerateResolver(string assetPath)
        {
            if (File.Exists(assetPath))
            {
                PackageInfo packageInfo = PackageInfo.FindForAssetPath(assetPath);

                return(packageInfo == null || packageInfo.source == PackageSource.Embedded);
            }

            return(false);
        }
        public static bool ShouldFormat(FormatContext formatContext, NPath pathToFormat)
        {
            // files only please
            if (!pathToFormat.FileExists())
            {
                return(false);
            }

            var fullPath = Path.GetFullPath(pathToFormat).ToNPath();

            var libraryFolder = formatContext.BaseDir.Combine("Library");

            // ignore files under Library folder (for instance files under Library/PackageCache are Git/npm cached packages, whence immutable)
            if (fullPath.IsChildOf(libraryFolder))
            {
                return(false);
            }

            if (!fullPath.IsChildOf(formatContext.BaseDir))
            {
#if UNITY_2019_2_OR_NEWER
                // Ignore any file that is not a child of the project root or a local package (which can live anywhere in the file system)
                var p = PackageInfo.FindForAssetPath(pathToFormat.ToString(SlashMode.Forward));
                if (p == null || p.source != PackageSource.Local)
                {
                    return(false);
                }
#else
                // can't find the package source of a given asset path until 2019.2
                return(false);
#endif
            }

            if (lastFormattedAssets != null && lastFormattedAssets.TryGetValue(fullPath, out var lastTimestamp))
            {
                var currentTimeStamp = File.GetLastWriteTime(fullPath).Ticks;

                // if timestamp is different, format (user may have reverted some changes through a VCS which resets the timestamp).
                var shouldFormat = lastTimestamp != currentTimeStamp;
                LogFormatDecision(fullPath, shouldFormat, formatContext.Logger, lastTimestamp, currentTimeStamp);

                if (!shouldFormat)
                {
                    return(false);
                }
                return(shouldFormat);
            }

            return(true);
        }
Exemplo n.º 3
0
        private void ManipulateWorld(string command)
        {
            World world = LoadWorld();

            if (world == null)
            {
                return;
            }

            switch (command)
            {
            case "SetToCurrentVersion":
                PackageInfo pi = PackageInfo.FindForAssetPath("Packages/com.wetzold.travrsal.sdk/package.json");
                if (pi != null)
                {
                    world.minVersion = pi.version;
                    EditorUtility.DisplayDialog("Success", $"Set minimum app version required to play the world to {pi.version}.", "OK");
                }
                break;

            case "AddZone":
                if (string.IsNullOrEmpty(zoneName))
                {
                    return;
                }
                string root = GetWorldPaths()[0];
                string path = $"{root}/Data/{zoneName}.tmx";
                if (File.Exists(path))
                {
                    EditorUtility.DisplayDialog("Error", "A zone with that name already exists.", "OK");
                    return;
                }
                AssetDatabase.CopyAsset($"Packages/{SDKUtil.PACKAGE_NAME}/Editor/CopyTemplates/EmptyZone.copy", $"Assets/Worlds/{world.key}/Data/{zoneName}.tmx");

                // two cases: either world explicitly lists files already or default is used with config.world mechanism
                if (world.worldData != null && world.worldData.Count > 0)
                {
                    world.worldData.Add(new WorldDataReference(path, WorldDataReference.ImportType.TileMap));
                }
                else
                {
                    string  mapFile  = root + "/Data/Config.world";
                    TMWorld worldMap = SDKUtil.ReadJSONFileDirect <TMWorld>(mapFile);
                    if (worldMap != null)
                    {
                        worldMap.maps = worldMap.maps.Append(new WorldMap(Path.GetFileName(path)));

                        File.WriteAllText(mapFile, SDKUtil.SerializeObject(worldMap));
                    }
                    else
                    {
                        EditorUtility.DisplayDialog("Error", $"Data/Config.world file for {world.key} does not exist. New zone was created but needs to be manually linked.", "OK");
                    }
                }
                zoneName = "";
                AssetDatabase.Refresh();
                break;

            case "AddVariable":
                if (string.IsNullOrEmpty(varName))
                {
                    return;
                }
                if (world.initialVariables == null)
                {
                    world.initialVariables = new List <Variable>();
                }
                world.initialVariables.Add(new Variable(varName));
                varName = "";
                break;

            case "AddSetting":
                if (string.IsNullOrEmpty(worldSetting))
                {
                    return;
                }
                if (world.settings == null)
                {
                    world.settings = new List <WorldSetting>();
                }
                world.settings.Add(new WorldSetting(worldSetting));
                worldSetting = "";
                break;

            case "AddCustomShader":
                if (string.IsNullOrEmpty(customShader))
                {
                    return;
                }
                if (world.customShaders == null)
                {
                    world.customShaders = new List <string>();
                }
                world.customShaders.Add(customShader);
                customShader = "";
                break;
            }

            SaveWorld(world);
        }
Exemplo n.º 4
0
        public BuilderToolbar(
            BuilderPaneWindow paneWindow,
            BuilderSelection selection,
            ModalPopup saveDialog,
            BuilderViewport viewport,
            BuilderExplorer explorer,
            BuilderLibrary library,
            BuilderInspector inspector,
            BuilderTooltipPreview tooltipPreview)
        {
            m_PaneWindow     = paneWindow;
            m_Selection      = selection;
            m_SaveDialog     = saveDialog;
            m_Viewport       = viewport;
            m_Explorer       = explorer;
            m_Library        = library;
            m_Inspector      = inspector;
            m_TooltipPreview = tooltipPreview;

            // Query the UI
            m_SaveDialogUxmlPathField      = m_SaveDialog.Q <TextField>("save-dialog-uxml-path");
            m_SaveDialogUxmlLocationButton = m_SaveDialog.Q <Button>("save-dialog-uxml-location-button");
            m_SaveDialogUssPathField       = m_SaveDialog.Q <TextField>("save-dialog-uss-path");
            m_SaveDialogUssLocationButton  = m_SaveDialog.Q <Button>("save-dialog-uss-location-button");
            m_SaveDialogSaveButton         = m_SaveDialog.Q <Button>("save-dialog-save-button");
            m_SaveDialogCancelButton       = m_SaveDialog.Q <Button>("save-dialog-cancel-button");
            m_SaveDialogTitleLabel         = m_SaveDialog.Q <Label>("title");

            m_SaveDialogUxmlPathField.RegisterValueChangedCallback(OnUxmlPathFieldChange);
            m_SaveDialogUssPathField.RegisterValueChangedCallback(OnUssPathFieldChange);

            m_SaveDialogSaveButton.clickable.clicked         += SaveDocument;
            m_SaveDialogCancelButton.clickable.clicked       += m_SaveDialog.Hide;
            m_SaveDialogUxmlLocationButton.clickable.clicked += OnUxmlLocationButtonClick;
            m_SaveDialogUssLocationButton.clickable.clicked  += OnUssLocationButtonClick;

            var saveDialogValidationBoxContainer = m_SaveDialog.Q("save-dialog-validation-box");

            m_SaveDialogValidationBox = new IMGUIContainer(DrawSaveDialogValidationMessage);
            m_SaveDialogValidationBox.style.overflow = Overflow.Hidden;
            saveDialogValidationBoxContainer.Add(m_SaveDialogValidationBox);

            var template = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(
                BuilderConstants.UIBuilderPackagePath + "/BuilderToolbar.uxml");

            template.CloneTree(this);

            // File Menu
            m_FileMenu = this.Q <ToolbarMenu>("file-menu");
            SetUpFileMenu();

            // Preview Button
            var previewButton = this.Q <ToolbarToggle>("preview-button");

            previewButton.RegisterValueChangedCallback(TogglePreviewMode);

            m_CanvasThemeMenu = this.Q <ToolbarMenu>("canvas-theme-menu");
            SetUpCanvasThemeMenu();
            ChangeCanvasTheme(document.currentCanvasTheme);
            UpdateCanvasThemeMenuStatus();

            // Track unsaved changes state change.
            document.unsavedChangesStateChanged = SetViewportSubTitle;
            SetViewportSubTitle();

            // Get Builder package version.
            var packageInfo = PackageInfo.FindForAssetPath("Packages/" + BuilderConstants.BuilderPackageName);

            if (packageInfo == null)
            {
                m_BuilderPackageVersion = null;
            }
            else
            {
                m_BuilderPackageVersion = packageInfo.version;
            }

            RegisterCallback <AttachToPanelEvent>(RegisterCallbacks);
        }
        public void ImportUxmlFromProject(BuilderLibraryItem projectCategory, bool includePackages)
        {
            var categoryStack = new List <BuilderLibraryItem>();
            var assets        = AssetDatabase.FindAllAssets(m_SearchFilter);

            foreach (var asset in assets)
            {
                var assetPath  = AssetDatabase.GetAssetPath(asset.instanceID);
                var prettyPath = assetPath;
                prettyPath = Path.GetDirectoryName(prettyPath);
                prettyPath = prettyPath.ConvertSeparatorsToUnity();
                if (prettyPath.StartsWith("Packages/") && !includePackages)
                {
                    continue;
                }

                if (prettyPath.StartsWith(BuilderConstants.UIBuilderPackageRootPath))
                {
                    continue;
                }

                // Check to make sure the asset is actually writable.
                var packageInfo = PackageInfo.FindForAssetPath(assetPath);
                if (packageInfo != null && packageInfo.source != PackageSource.Embedded && packageInfo.source != PackageSource.Local)
                {
                    continue;
                }

                // Anoter way to check the above. Leaving it here for references in case the above stops working.
                //AssetDatabase.GetAssetFolderInfo(assetPath, out bool isRoot, out bool isImmutable);
                //if (isImmutable)
                //continue;

                var split = prettyPath.Split('/');
                AddCategoriesToStack(projectCategory, categoryStack, split);

                var vta     = asset.pptrValue as VisualTreeAsset;
                var newItem = BuilderLibraryContent.CreateItem(asset.name + ".uxml", nameof(TemplateContainer), typeof(TemplateContainer),
                                                               () =>
                {
                    if (vta == null)
                    {
                        return(null);
                    }

                    var tree = vta.CloneTree();
                    tree.SetProperty(BuilderConstants.LibraryItemLinkedTemplateContainerPathVEPropertyName, assetPath);
                    tree.name = vta.name;
                    return(tree);
                },
                                                               (inVta, inParent, ve) =>
                {
                    var vea = inVta.AddTemplateInstance(inParent, assetPath) as VisualElementAsset;
                    vea.AddProperty("name", vta.name);
                    ve.SetProperty(BuilderConstants.ElementLinkedInstancedVisualTreeAssetVEPropertyName, vta);
                    return(vea);
                },
                                                               null, vta);

                var data = newItem.data;
                if (data.icon == null)
                {
                    data.SetIcon((Texture2D)EditorGUIUtility.IconContent("VisualTreeAsset Icon").image);
                }
                data.hasPreview = true;

                if (categoryStack.Count == 0)
                {
                    projectCategory.AddChild(newItem);
                }
                else
                {
                    categoryStack.Last().AddChild(newItem);
                }
            }
        }
Exemplo n.º 6
0
        private static bool IsExternal(Assembly unityAssembly)
        {
            var packageInfo = PackageInfo.FindForAssetPath(unityAssembly.sourceFiles.First());

            return(packageInfo != null && packageInfo.source == PackageSource.Registry);
        }