Exemple #1
0
        /// <summary>
        /// todo not contextual enough to a generalized folder field, might apply differently later
        /// </summary>
        public string DrawFolderField()
        {
            string assetPath      = Path.GetFullPath(_originalPath);
            string defaultRelPath = LDtkPathUtility.DirectoryOfAssetPath(assetPath);

            if (!DrawFieldAndButton(defaultRelPath))
            {
                string propStringValue = _pathProp.stringValue;
                if (!propStringValue.IsNullOrEmpty())
                {
                    return(propStringValue);
                }
                return(defaultRelPath);
            }

            string destinationPath = EditorUtility.OpenFolderPanel(_filePanelDescription,
                                                                   defaultRelPath,
                                                                   "");

            if (destinationPath.StartsWith(Application.dataPath))
            {
                destinationPath = "Assets" + destinationPath.Substring(Application.dataPath.Length);
            }

            if (!string.IsNullOrEmpty(destinationPath) && AssetDatabase.IsValidFolder(destinationPath))
            {
                _pathProp.stringValue = destinationPath;
            }
            else
            {
                Debug.LogWarning($"LDtk Export: Cannot specify a folder outside of the Unity project\n{destinationPath}");
            }

            return(_pathProp.stringValue);
        }
Exemple #2
0
        public string DrawPathField()
        {
            string assetPath = Path.GetFullPath(_originalPath);
            string csPath    = Path.ChangeExtension(assetPath, $".{_extension}");

            string defaultRelPath = GetRelativePath(assetPath, csPath);

            if (!DrawFieldAndButton(defaultRelPath))
            {
                string propStringValue = _pathProp.stringValue;
                if (!propStringValue.IsNullOrEmpty())
                {
                    return(_pathProp.stringValue);
                }
                return(defaultRelPath);
            }

            string destinationEnumPath = EditorUtility.SaveFilePanel(_filePanelDescription,
                                                                     Path.GetDirectoryName(csPath),
                                                                     Path.GetFileName(csPath), _extension);

            if (!string.IsNullOrEmpty(destinationEnumPath) && AssetDatabase.IsValidFolder(LDtkPathUtility.DirectoryOfAssetPath(destinationEnumPath)))
            {
                string relPath = GetRelativePath(assetPath, destinationEnumPath);
                relPath = LDtkPathUtility.CleanPathSlashes(relPath);
                _pathProp.stringValue = relPath;
            }
            else
            {
                Debug.LogWarning("LDtk Export: Cannot specify within a folder outside of the Unity project");
            }

            return(_pathProp.stringValue);
        }
        /// <returns>
        /// Whether the file creation was successful.
        /// </returns>
        public bool CreateEnumFile()
        {
            string directory = Path.GetDirectoryName(_writePath);;

            string wholeText = GetWholeEnumText();

            LDtkPathUtility.TryCreateDirectory(directory);
            File.WriteAllText(_writePath, wholeText);

            return(true);
        }
        private T GetAssetRelativeToAssetPath <T>(string assetPath, string relPath) where T : Object
        {
            string directory = Path.GetDirectoryName(assetPath);

            string assetsPath = $"{directory}/{relPath}";

            assetsPath = LDtkPathUtility.CleanPath(assetsPath);

            //basic find
            T assetAtPath = (T)AssetDatabase.LoadMainAssetAtPath(assetsPath);

            if (assetAtPath != null)
            {
                return(assetAtPath);
            }



            /*//try a reimport as it may fix it
             * if (File.Exists(assetsPath))
             * {
             *  AssetDatabase.ImportAsset(assetsPath, ImportAssetOptions.ForceUpdate);
             *
             *  AssetDatabase.Refresh();
             *  assetAtPath = AssetDatabase.LoadAssetAtPath<T>(assetsPath);
             *  if (assetAtPath != null)
             *  {
             *      return assetAtPath;
             *  }
             * }
             *
             * //if the asset is null, try an asset database refresh (the refresh costs time so try try only if it was unsuccessful)
             * AssetDatabase.Refresh();
             * assetAtPath = AssetDatabase.LoadAssetAtPath<T>(assetsPath);
             * if (assetAtPath != null)
             * {
             *  return assetAtPath;
             * }*/


            //if we couldn't load it but the file indeed exists, spit a different error
            if (File.Exists(assetsPath))
            {
                Debug.LogError($"LDtk: File does exist but could not load the asset at \"{assetsPath}\". " +
                               $"Is the asset imported yet?");
                return(null);
            }

            Debug.LogError($"LDtk: Could not find an asset in the path relative to \"{assetPath}\": \"{relPath}\". " +
                           $"Is the asset also locatable by LDtk, and is the asset located in the Unity Project?");
            return(null);
        }
Exemple #5
0
        public void Generate()
        {
            string filePath = GetFilePath(_ctx);

            LDtkPathUtility.CleanPath(filePath);
            LDtkPathUtility.TryCreateDirectoryForFile(filePath);

            LDtkEnumFactory factory = new LDtkEnumFactory(_templates, filePath, _enumScriptNamespace);

            if (factory.CreateEnumFile())
            {
                EditorApplication.delayCall += AssetDatabase.Refresh;
            }
        }
Exemple #6
0
        private List <T> CloneArtifacts <T>(List <T> artifacts, string extraPath, string assetName = null) where T : Object
        {
            if (artifacts.IsNullOrEmpty())
            {
                return(new List <T>());
            }

            string parentPath = $"{_path}{extraPath}";

            LDtkPathUtility.TryCreateDirectory(parentPath);

            List <T> list = new List <T>();

            foreach (T artifact in artifacts)
            {
                if (artifact == null)
                {
                    continue;
                }

                string cloneName       = assetName != null ? assetName : artifact.name;
                string destinationPath = $"{parentPath}/{cloneName}.asset";

                //Debug.Log($"Copy asset\n{artifact.name}\nto\n{destinationPath}");

                Object clone = CreateClone(artifact);
                clone.name = cloneName;

                T loadedAsset = AssetDatabase.LoadAssetAtPath <T>(destinationPath);

                if (loadedAsset)
                {
                    EditorUtility.CopySerializedIfDifferent(clone, loadedAsset);
                    list.Add(loadedAsset);
                }
                else
                {
                    AssetDatabase.CreateAsset(clone, destinationPath);
                    list.Add((T)clone);
                }
            }

            return(list);
        }