改名字的关键代码:
//修改图片名称
string oldFilePath = Application.streamingAssetsPath + $"/Content/{imageNameOld}.jpg";
string newFilePath = Application.streamingAssetsPath + $"/Content/{imageName}.jpg";
File.Move(oldFilePath, newFilePath);
全部代码,图片是放在StreamingAssets文件夹下的Content文件夹下,这个代码实现的功能是把所有的图片的名字改成"x_0",例如:把1改名为1_0(注意,这个只能改jpg的文件)
using BaseData;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class BaseDataManager : MonoBehaviour
{
PhotoPraiseData photoData; //图片数据
List<PhotoPraiseData> allPhotoDataList; //所有图片数据信息
private void Awake()
{
IEnumerator photoIE = DownloadImage(GetAllSpriteFilePath(ConfigFile.LoadString("ImageContent")));
StartCoroutine(photoIE);
}
#region 获取指定文件夹下的所有图片路径
/// <summary>
/// 获取文件夹下的所有图片
/// </summary>
/// <param name="folder">文件夹名称</param>
/// <returns></returns>
public List<string> GetAllSpriteFilePath(string folder)
{
List<string> filePaths = new List<string>();
string imgtype = ConfigFile.LoadString("ImgType");
string[] ImageType = imgtype.Split('|');
for (int i = 0; i < ImageType.Length; i++)
{
//获取Application.dataPath文件夹下所有的图片路径
string[] dirs = Directory.GetFiles(Application.streamingAssetsPath + $"/{folder}", ImageType[i]);
for (int j = 0; j < dirs.Length; j++)
{
filePaths.Add(dirs[j]);
}
}
return filePaths;
}
#endregion
#region 协程图片异步加载
string[] array;
/// <summary>
/// 协程图片异步加载
/// </summary>
/// <param name="type">类型</param>
/// <param name="allImagePathArray">所有图片的下载路径</param>
/// <returns></returns>
IEnumerator DownloadImage(List<string> allImagePathArray)
{
//图片路径下标
int index = 0;
allPhotoDataList = new List<PhotoPraiseData>();
while (index < allImagePathArray.Count)
{
UnityWebRequest www = UnityWebRequestTexture.GetTexture(allImagePathArray[index]);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
/*3.设置图片精灵名称
* Path.GetFileNameWithoutExtension获取路径最后一个名称
*/
array = Path.GetFileNameWithoutExtension(allImagePathArray[index]).Split(' ');
string imageNameOld = array[0];
string imageName = imageNameOld +"_0";
//修改图片名称
string oldFilePath = Application.streamingAssetsPath + $"/Content/{imageNameOld}.jpg";
string newFilePath = Application.streamingAssetsPath + $"/Content/{imageName}.jpg";
File.Move(oldFilePath, newFilePath);
}
index++;
}
yield return null;
}
#endregion
#region 退出
public void QuitAPP()
{
Application.Quit();
}
#endregion
}
知识点记录:$符的作用:用$符可以将引号内的所有元素相加(相当于加号的作用)
eg:string oldFilePath = Application.streamingAssetsPath +"/Content/"+imageNameOld"+.jpg";
用$符可以改为:string oldFilePath = Application.streamingAssetsPath + $"/Content/{imageNameOld}.jpg";