/// <summary>
        /// 创建或者更新精灵预制体
        /// </summary>
        /// <param name="path"></param>
        /// <param name="savePrefabPath"></param>
        private static void CreateOrUpdateSpritePrefab(string path, string savePrefabPath = "")
        {
            if (System.IO.Path.GetExtension(path).ToLower() != ".png" && System.IO.Path.GetExtension(path).ToLower() != ".jpg")
            {
                Debug.Log("Not Sprite");
                return;
            }
            TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter; //获取并转换该资源导入器

            //  Debug.Log(importer.textureType);
            if (importer.textureType != TextureImporterType.Sprite)
            {
                importer.textureType      = TextureImporterType.Sprite; //格式转换
                importer.mipmapEnabled    = false;
                importer.spriteImportMode = SpriteImportMode.Single;
                AssetDatabase.ImportAsset(path);
                AssetDatabase.Refresh();
            } //修改选中图片资源的格式为Sprite

            string prefabName = System.IO.Path.GetFileNameWithoutExtension(path);

            if (string.IsNullOrEmpty(savePrefabPath))
            {
                savePrefabPath = EditorDialogUtility.SaveFileDialog("保存生成的预制体", Application.dataPath + "/Resources", prefabName, "prefab");
                if (string.IsNullOrEmpty(savePrefabPath))
                {
                    Debug.LogInfor("取消 创建SpriteRender");
                    return;
                }
            }
            else
            {
                savePrefabPath = string.Format("{0}/{1}.prefab", savePrefabPath, prefabName);
            }

            Debug.Log("CreateSpriteRender  savePrefabPath=" + savePrefabPath);
            //********需要考虑已经存在的时候只需要替换Sprite  TODO


            // Debug.Log("path=" + path  + "           savePrefabPath=" + savePrefabPath);
            GameObject go      = new GameObject(prefabName);
            Sprite     sources = AssetDatabase.LoadAssetAtPath <Sprite>(path);

            go.AddComponent <SpriteRenderer>().sprite = sources;                                                            //设置Sprite的引用关系
            go.layer = LayerMask.NameToLayer("UI");
            GameObject prefab = PrefabUtility.CreatePrefab(savePrefabPath.Substring(savePrefabPath.IndexOf("Assets")), go); //创建预制体资源 路径必须从 Assets开始

            GameObject.DestroyImmediate(go);
        }
Example #2
0
        /// <summary>
        /// 创建Unity 序列化资源Asset
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static void CreateUnityAsset <T>(string title, string directoryPath, string fileName) where T : ScriptableObject
        {
            T      asset            = ScriptableObject.CreateInstance <T>();
            string assetPathAndName = EditorDialogUtility.SaveFileDialog(title, directoryPath, fileName, "asset");

            if (string.IsNullOrEmpty(assetPathAndName))
            {
                return;
            }
            Debug.Log("assetPathAndName= " + assetPathAndName);
            assetPathAndName = assetPathAndName.Substring(assetPathAndName.IndexOf("Assets"));

            Debug.Log("CreateUnityAsset >>>path :" + assetPathAndName);
            AssetDatabase.CreateAsset(asset, assetPathAndName); //创建资源Asset
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow();
            Selection.activeObject = asset;
        }
        private static void SaveMultiSprite(UnityEngine.Object[] selectObj)
        {
            if (selectObj == null || selectObj.Length == 0)
            {
                return;
            }
            string savePrefabPath = EditorDialogUtility.SaveFileDialog("保存生成的预制体", Application.dataPath + "/Resources", "只需要选择合适的目录,不需要填写文件名", "prefab");

            if (string.IsNullOrEmpty(savePrefabPath))
            {
                Debug.LogInfor("取消 创建SpriteRender");
                return;
            }

            Debug.Log("savePrefabPath=" + savePrefabPath);
            Debug.Log("savePrefabPath=" + System.IO.Path.GetDirectoryName(savePrefabPath));


            foreach (var item in selectObj)
            {
                string path = AssetDatabase.GetAssetPath(item);
                CreateOrUpdateSpritePrefab(path, System.IO.Path.GetDirectoryName(savePrefabPath));
            }
        }
        /// <summary>
        /// 根据选择的目录生成预制体并关联这个图片
        /// </summary>
        public static void CreateMultiSpriteRenders_Dirctory()
        {
            string dirctoryPath = AssetDatabase.GetAssetPath(Selection.activeObject);

            if (System.IO.Directory.Exists(dirctoryPath) == false)
            {
                Debug.LogError("当前选择的不是一个可用的目录路径,创建失败");
                return;
            }

            string savePrefabPath = EditorDialogUtility.SaveFileDialog("保存生成的预制体", Application.dataPath + "/Resources/Sprite", "选择一个目录时候不需要输入文件名,按照选择的目录自动创建目录", "");

            savePrefabPath = System.IO.Path.GetDirectoryName(savePrefabPath);

            if (string.IsNullOrEmpty(savePrefabPath))
            {
                Debug.LogEditorInfor("取消 创建SpriteRender");
                return;
            }

            //Debug.Log("dirctoryPath=" + dirctoryPath);
            string parentDirectoryPath = IoUtility.Instance.GetDirectoryParentDcirectory(dirctoryPath); //获取选择的目录的目录名

            if (string.IsNullOrEmpty(parentDirectoryPath))
            {
                Debug.LogError("无法处理的目录 " + parentDirectoryPath);
                return;
            }
            savePrefabPath = string.Format("{0}/{1}", savePrefabPath, parentDirectoryPath);
            if (System.IO.Directory.Exists(savePrefabPath) == false)
            {
                System.IO.Directory.CreateDirectory(savePrefabPath);
            }


            var           assets             = AssetDatabase.FindAssets("t:texture2D", new string[] { dirctoryPath }); //获取当前目录下所有的图片资源GUID
            List <string> allTexture2DAssets = new List <string>();                                                    //所有的图片资源相对资源路径

            foreach (var item in assets)
            {
                allTexture2DAssets.Add(AssetDatabase.GUIDToAssetPath(item));
            }//转成成资源的相对路径

            bool IsNeedReflushAsset = false; //标识是否需要刷新资源

            for (int dex = allTexture2DAssets.Count - 1; dex >= 0; --dex)
            {
                #region 对资源的格式进行处理

                TextureImporter asstImpoter = AssetImporter.GetAtPath(allTexture2DAssets[dex]) as TextureImporter;
                if (asstImpoter == null)
                {
                    IsNeedReflushAsset = true;
                    allTexture2DAssets.RemoveAt(dex);
                    Debug.LogError("理论上不存在这个情况");
                    continue;
                }

                if (asstImpoter.textureType != TextureImporterType.Sprite)
                {
                    IsNeedReflushAsset = true;
                    if (System.IO.Path.GetExtension(allTexture2DAssets[dex]).ToLower() != ".png")
                    {
                        Debug.LogEditorInfor("生成Sprite 关联资源时候 资源不是png格式 " + allTexture2DAssets[dex]);
                        allTexture2DAssets.RemoveAt(dex);
                        continue;
                    }//不是png 图片则不导入 否则强制导入图片
                    Texture2D texture = AssetDatabase.LoadAssetAtPath <Texture2D>(allTexture2DAssets[dex]);
                    TextureImporterHelper.OnPresProcessUITextureSetting(texture, asstImpoter, allTexture2DAssets[dex]);
                }
                #endregion
            }

            if (IsNeedReflushAsset)
            {
                AssetDatabase.Refresh();
            }

            foreach (var sprite2d in allTexture2DAssets)
            {
                //CreateOrUpdateSpritePrefab(sprite2d, System.IO.Path.GetDirectoryName(savePrefabPath));
                CreateOrUpdateSpritePrefab(sprite2d, savePrefabPath);
            }
            AssetDatabase.Refresh();
        }