public static string CreatAssetPath <T>(this T script, string subPath) where T : UnityEngine.Object //class
        {
            if (null == script)
            {
                return(string.Empty);
            }

            string path = ADB.FindAssets("t:Script")
                          .Where(v => Path.GetFileNameWithoutExtension(ADB.GUIDToAssetPath(v)) == script.GetType().Name)
                          .Select(id => ADB.GUIDToAssetPath(id))
                          .FirstOrDefault()
                          .ToString();
            string newPath = Path.Combine(Path.GetDirectoryName(path), subPath);

            if (!ADB.IsValidFolder(newPath))
            {
                newPath = ADB.GUIDToAssetPath(ADB.CreateFolder(path, subPath));
            }
            return(newPath);
        }
Beispiel #2
0
    /// <summary>
    /// 编辑器下使用,给定一个类的对象,在这个类的同级目录下创建文件夹并返回路径
    /// </summary>
    /// <typeparam name="T">类</typeparam>
    /// <param name="script">对象</param>
    /// <param name="subPath">指定要创建的文件夹的名称</param>
    /// <returns>文件夹的相对路径,相对于Assets文件夹</returns>
    public static string Creat <T>(T script, string subPath) where T : UnityEngine.Object //class
    {
        string newPath = "";

#if UNITY_EDITOR
        string path = ADB.FindAssets("t:Script")
                      .Where(v => Path.GetFileNameWithoutExtension(ADB.GUIDToAssetPath(v)) == script.GetType().Name)
                      .Select(id => ADB.GUIDToAssetPath(id))
                      .FirstOrDefault()
                      .ToString();
        newPath = Path.Combine(Path.GetDirectoryName(path), subPath);
        if (!ADB.IsValidFolder(newPath))
        {
            newPath = ADB.GUIDToAssetPath(ADB.CreateFolder(path, subPath));
        }
#else
        return(newPath);
#endif
        return(newPath);
    }
Beispiel #3
0
        private void ExtractTexturesGUI()
        {
            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.PrefixLabel(Styles.Textures);

                using (
                    new EditorGUI.DisabledScope(!m_HasEmbeddedTextures.boolValue &&
                                                !m_HasEmbeddedTextures.hasMultipleDifferentValues))
                {
                    if (GUILayout.Button(Styles.ExtractEmbeddedTextures))
                    {
                        // when extracting textures, we must handle the case when multiple selected assets could generate textures with the same name at the user supplied path
                        // we proceed as follows:
                        // 1. each asset extracts the textures in a separate temp folder
                        // 2. we remap the extracted assets to the respective asset importer
                        // 3. we generate unique names for each asset and move them to the user supplied path
                        // 4. we re-import all the assets to have the internal materials linked to the newly extracted textures

                        List <Tuple <Object, string> > outputsForTargets = new List <Tuple <Object, string> >();
                        // use the first target for selecting the destination folder, but apply that path for all targets
                        string destinationPath = (target as ModelImporter).assetPath;
                        destinationPath = EditorUtility.SaveFolderPanel("Select Textures Folder",
                                                                        FileUtil.DeleteLastPathNameComponent(destinationPath), "");
                        if (string.IsNullOrEmpty(destinationPath))
                        {
                            // cancel the extraction if the user did not select a folder
                            return;
                        }
                        destinationPath = FileUtil.GetProjectRelativePath(destinationPath);

                        try
                        {
                            // batch the extraction of the textures
                            AssetDatabase.StartAssetEditing();

                            foreach (var t in targets)
                            {
                                var tempPath = FileUtil.GetUniqueTempPathInProject();
                                tempPath = tempPath.Replace("Temp", UnityEditorInternal.InternalEditorUtility.GetAssetsFolder());
                                outputsForTargets.Add(Tuple.Create(t, tempPath));

                                var importer = t as ModelImporter;
                                importer.ExtractTextures(tempPath);
                            }
                        }
                        finally
                        {
                            AssetDatabase.StopAssetEditing();
                        }

                        try
                        {
                            // batch the remapping and the reimport of the assets
                            AssetDatabase.Refresh();
                            AssetDatabase.StartAssetEditing();

                            foreach (var item in outputsForTargets)
                            {
                                var importer = item.Item1 as ModelImporter;

                                var guids = AssetDatabase.FindAssets("t:Texture", new string[] { item.Item2 });

                                foreach (var guid in guids)
                                {
                                    var path = AssetDatabase.GUIDToAssetPath(guid);
                                    var tex  = AssetDatabase.LoadAssetAtPath <Texture>(path);
                                    if (tex == null)
                                    {
                                        continue;
                                    }

                                    importer.AddRemap(new AssetImporter.SourceAssetIdentifier(tex), tex);

                                    var newPath = Path.Combine(destinationPath, FileUtil.UnityGetFileName(path));
                                    newPath = AssetDatabase.GenerateUniqueAssetPath(newPath);
                                    AssetDatabase.MoveAsset(path, newPath);
                                }

                                AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);

                                AssetDatabase.DeleteAsset(item.Item2);
                            }
                        }
                        finally
                        {
                            AssetDatabase.StopAssetEditing();
                        }
                    }
                }
            }
        }
Beispiel #4
0
 public static string[] FindAssets(string filter)
 {
     return(AssetDatabase.FindAssets(filter, null));
 }