当前位置:   article > 正文

Unity UGUI 效果 之 多页翻页,多页任意翻页的简单效果实现(示例演示为动态加载多图片实现翻页预览效果)_unity 图片多次翻书展开动画

unity 图片多次翻书展开动画

 

 

Unity UGUI 效果 之 多页翻页,多页任意翻页的简单效果实现(示例演示为动态加载多图片实现翻页预览效果)

 

目录

Unity UGUI 效果 之 多页翻页,多页任意翻页的简单效果实现(示例演示为动态加载多图片实现翻页预览效果)

一、简单介绍

二、实现原理

三、注意事项

四、效果预览

六、关键代码

七、参考工程


 

一、简单介绍

UGUI,是Unity自带的 GUI 系统,有别于 NGUI;使用 UGUI 也能制作出比较酷炫的效果 。

本节介绍,使用 UGUI 通过代码实现多页翻页的效果,示例是以动态加载图片为例实现的多页翻页效果。

 

二、实现原理

1、通过展示的总的动态图片个书,每页每页展示图片的数量,分为若干页

2、Button实现前后翻页,Toggle 实现任意翻页

3、关键的逻辑代码是当前页时,代码实现展示的内容

 

三、注意事项

1、在计算分页总页数的时候,注意整除和右余数的时候,总页数是不一样的

2、前后翻页和任意翻页都要互相更新显示

3、代码 toggle.isOn 的时候,是不会触发对应的监听时间函数,请注意

4、多说一句,在加载图片展示的时候,最好不要一张一张边加载,边展示,这样很有可能会程序崩溃;多图多的话,分批多次加载,一批加载完,展示一批

5、图片加载的路径(pageIconPath = @"D:\Tmp\Images"),是我本机的图片位置,代码中记得更改为自己的图片路径

 

四、效果预览

 

五、实现步骤

1、打开 Unity ,新建空工程

 

2、在场景中布局 UGUI ,PageItem 和 MultiPageToggleItem 会作为预制体,用于动态加载

 

3、在工程中新建脚本,编辑脚本,实现相关逻辑代码

 

4、把脚本对应挂载,并赋值

 

5、运行场景,效果如上

 

六、关键代码

1、MultiPageShowWrapper.cs

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using UnityEngine.UI;
  7. public class MultiPageShowWrapper : MonoBehaviour
  8. {
  9. #region 常量
  10. // Page Icon 加载路径
  11. const string pageIconPath = @"D:\Tmp\Images";
  12. // PageItem Resources 加载路径
  13. const string PageItemPath = @"PageItem";
  14. // MultiPageToggleItem Resources 加载路径
  15. const string MultiPageToggleItemPath = @"MultiPageToggleItem";
  16. // 每页展示 icon 的数量
  17. const int ShowPageIconItemCountPerPage = 10;
  18. #endregion
  19. #region 字段
  20. // 生成显示放置 PageItem 的父物体
  21. public Transform PageGrid_Image;
  22. // 生成显示放置 MultiPageToggleItem 的父物体
  23. public Transform PageToggleGroup;
  24. // 左翻页按钮
  25. public Button LeftPage_Button;
  26. // 右翻页按钮
  27. public Button RightPage_Button;
  28. // 当前页/总页显示文本
  29. public Text PageCount_Text;
  30. // PageItem 物体
  31. GameObject mPageItem;
  32. // MultiPageToggleItem 物体
  33. GameObject mMultiPageToggleItem;
  34. // Page ICon 图片路径的列表
  35. private List<string> mPageIconList = new List<string>();
  36. private int currentPage = 0;
  37. private int totalPage = 0;
  38. private int currentIntiateItem = 0;
  39. private int PageIconListCount = 0;
  40. #endregion
  41. #region 方法
  42. #region 其他视情况而定的方法 协程加载指定路径的图片
  43. private List<Texture2D> mPageIconTextureList = new List<Texture2D>();
  44. int loadIndex = 0;
  45. bool isLoadedEnd = false;
  46. void InitLoadIconTexture2d()
  47. {
  48. mPageIconList = GetWindiowsPCPicturesPath.GetPictutresPathsInTheFilePath(pageIconPath);
  49. for (int i = 0; i < mPageIconList.Count; i++)
  50. {
  51. loadIndex++;
  52. GetTexture(mPageIconList[i], (texture2D) =>
  53. {
  54. loadIndex--;
  55. mPageIconTextureList.Add(texture2D);
  56. });
  57. }
  58. }
  59. void Start()
  60. {
  61. InitLoadIconTexture2d();
  62. }
  63. void Update() {
  64. if (mPageIconTextureList.Count>0 && loadIndex <=0 && isLoadedEnd == false)
  65. {
  66. isLoadedEnd = true;
  67. StartCoroutine(ShowPageIconList(mPageIconTextureList));
  68. }
  69. }
  70. /// <summary>
  71. /// 请求图片
  72. /// </summary>
  73. /// <param name="url">图片地址,like 'http://www.my-server.com/image.png '</param>
  74. /// <param name="action">请求发起后处理回调结果的委托,处理请求结果的图片</param>
  75. /// <returns></returns>
  76. public void GetTexture(string url, Action<Texture2D> actionResult)
  77. {
  78. StartCoroutine(_GetTexture(url, actionResult));
  79. }
  80. /// <summary>
  81. /// 请求图片
  82. /// </summary>
  83. /// <param name="url">图片地址,like 'http://www.my-server.com/image.png '</param>
  84. /// <param name="action">请求发起后处理回调结果的委托,处理请求结果的图片</param>
  85. /// <returns></returns>
  86. IEnumerator _GetTexture(string url, Action<Texture2D> actionResult)
  87. {
  88. UnityWebRequest uwr = new UnityWebRequest(url);
  89. DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
  90. uwr.downloadHandler = downloadTexture;
  91. yield return uwr.SendWebRequest();
  92. Texture2D t = null;
  93. if (!(uwr.isNetworkError || uwr.isHttpError))
  94. {
  95. t = downloadTexture.texture;
  96. }
  97. else
  98. {
  99. Debug.Log("下载失败,请检查网络,或者下载地址是否正确 ");
  100. }
  101. if (actionResult != null)
  102. {
  103. actionResult(t);
  104. }
  105. }
  106. #endregion
  107. /// <summary>
  108. /// 加載 PageItem
  109. /// </summary>
  110. /// <returns></returns>
  111. private GameObject LoadAppItem()
  112. {
  113. GameObject tmp = Resources.Load<GameObject>(PageItemPath);
  114. if (tmp == null)
  115. {
  116. Debug.LogError("AppItem Load Failure!! AppItemPath may be Error.");
  117. return null;
  118. }
  119. return tmp;
  120. }
  121. /// <summary>
  122. /// 加載 MultiPageToggleItem
  123. /// </summary>
  124. /// <returns></returns>
  125. private GameObject LoadMultiPageToggleItem()
  126. {
  127. GameObject tmp = Resources.Load<GameObject>(MultiPageToggleItemPath);
  128. if (tmp == null)
  129. {
  130. Debug.LogError("AppItem Load Failure!! AppCountToggleItemPath may be Error.");
  131. return null;
  132. }
  133. return tmp;
  134. }
  135. /// <summary>
  136. /// 展示 Page Icon 列表
  137. /// 注意 有考虑到数据动态的怎加或减少的情况
  138. /// </summary>
  139. /// <param name="pageIconTextureList"></param>
  140. /// <returns></returns>
  141. private IEnumerator ShowPageIconList(List<Texture2D> pageIconTextureList)
  142. {
  143. yield return new WaitForEndOfFrame();
  144. PageIconListCount = pageIconTextureList.Count;
  145. mPageIconTextureList = pageIconTextureList;
  146. #region 当前页数和总页数
  147. // 设置初始当前页为 1
  148. currentPage = 1;
  149. // 判断总数量是否整除每页数量 (有余则总页数+1,整除则不用)
  150. totalPage = (int)(PageIconListCount / ShowPageIconItemCountPerPage) + (((PageIconListCount % ShowPageIconItemCountPerPage) == 0) ? 0 : 1);
  151. #endregion
  152. #region 底部任意切换页数的UI Toggel 显示生成
  153. // Page Toggle (底部任意切换页数的UI Toggel 显示生成)
  154. HideMultiPageToggleItems();
  155. if (PageToggleGroup.childCount >= totalPage)
  156. {
  157. for (int i = 0; i < totalPage; i++)
  158. {
  159. MultiPageToggleItem multiPageToggleItem = PageToggleGroup.GetChild(i).GetComponent<MultiPageToggleItem>();
  160. SetMultiPageToggleItemData(i, multiPageToggleItem);
  161. multiPageToggleItem.gameObject.SetActive(true);
  162. }
  163. }
  164. else
  165. {
  166. for (int i = 0; i < PageToggleGroup.childCount; i++)
  167. {
  168. MultiPageToggleItem multiPageToggleItem = PageToggleGroup.GetChild(i).GetComponent<MultiPageToggleItem>();
  169. SetMultiPageToggleItemData(i, multiPageToggleItem);
  170. multiPageToggleItem.gameObject.SetActive(true);
  171. }
  172. // 之前生成不够数量的 multiPageToggleItem 则生成
  173. for (int i = PageToggleGroup.childCount; i < totalPage; i++)
  174. {
  175. MultiPageToggleItem multiPageToggleItem = GameObject.Instantiate(mMultiPageToggleItem).GetComponent<MultiPageToggleItem>();
  176. SetMultiPageToggleItemData(i, multiPageToggleItem);
  177. }
  178. }
  179. #endregion
  180. #region Page Icon 的展示
  181. // Page Item (Page Icon 的展示(原理同上类似))
  182. currentIntiateItem = PageGrid_Image.childCount;
  183. if (PageIconListCount < ShowPageIconItemCountPerPage)
  184. {
  185. // 隱藏所有,保障多餘的不會顯示
  186. HidePageItems();
  187. }
  188. // 展示已有的不必重新生成的對象(currentIntiateItem 不會超過ShowAppItemCountPerPage)
  189. for (int i = 0; i < currentIntiateItem; i++)
  190. {
  191. if (PageIconListCount < currentIntiateItem)
  192. {
  193. if (i == PageIconListCount)
  194. {
  195. break;
  196. }
  197. }
  198. PageItem pageItem = PageGrid_Image.GetChild(i).GetComponent<PageItem>();
  199. pageItem.gameObject.SetActive(true);
  200. SetPageItemData(i, pageIconTextureList, pageItem);
  201. }
  202. // App總數大於生成的 生成的小於 ShowAppItemCountPerPage 個
  203. // 1、App總數大於 ShowAppItemCountPerPage
  204. // 2、App總數小於等於 ShowAppItemCountPerPage
  205. if (PageIconListCount > currentIntiateItem)
  206. {
  207. if (ShowPageIconItemCountPerPage < PageIconListCount)
  208. {
  209. for (int i = currentIntiateItem; i < ShowPageIconItemCountPerPage; i++)
  210. {
  211. PageItem pageItem = Instantiate(mPageItem).GetComponent<PageItem>();
  212. SetPageItemData(i, pageIconTextureList, pageItem);
  213. }
  214. }
  215. else
  216. {
  217. for (int i = currentIntiateItem; i < PageIconListCount; i++)
  218. {
  219. PageItem pageItem = Instantiate(mPageItem).GetComponent<PageItem>();
  220. SetPageItemData(i, pageIconTextureList, pageItem);
  221. }
  222. }
  223. }
  224. #endregion
  225. #region 页数和翻页按钮的更新
  226. //更新頁碼
  227. PageCount_Text.text = currentPage + "/" + totalPage;
  228. // 最前頁,把左翻頁隱藏
  229. LeftPage_Button.gameObject.SetActive(false);
  230. // 只有一页,右翻页也隐藏
  231. if (totalPage == 1)
  232. {
  233. RightPage_Button.gameObject.SetActive(false);
  234. }
  235. #endregion
  236. }
  237. #region 左右翻页操作数据显示
  238. /// <summary>
  239. /// 向前(左)翻頁
  240. /// </summary>
  241. private void LeftPageEvent()
  242. {
  243. // 最前一頁,不能左翻頁
  244. if ((currentPage) == 1)
  245. {
  246. return;
  247. }
  248. else
  249. {
  250. // 向左翻頁 右翻頁若隱藏即可顯示
  251. if (RightPage_Button.gameObject.activeSelf == false)
  252. {
  253. RightPage_Button.gameObject.SetActive(true);
  254. }
  255. currentPage = currentPage - 1;
  256. // Toggle 更新 (这里不会主动调用 toggle 事件,所以放心不用重复操作数据)
  257. PageToggleGroup.GetChild(currentPage - 1).GetComponent<MultiPageToggleItem>().SetToggleIsOn(true);
  258. // 最前頁,把左翻頁隱藏
  259. if (currentPage == 1)
  260. {
  261. LeftPage_Button.gameObject.SetActive(false);
  262. }
  263. // 刷新展示
  264. int startInt = ((currentPage - 1) * ShowPageIconItemCountPerPage);
  265. for (int i = 0; i < ShowPageIconItemCountPerPage; i++)
  266. {
  267. PageItem pageItem = PageGrid_Image.GetChild(i).GetComponent<PageItem>();
  268. pageItem.gameObject.SetActive(true);
  269. SetPageItemData(startInt + i, mPageIconTextureList, pageItem);
  270. }
  271. }
  272. //更新頁碼
  273. PageCount_Text.text = currentPage + "/" + totalPage;
  274. }
  275. /// <summary>
  276. /// 向后(右)翻頁
  277. /// </summary>
  278. private void RightPageEvent()
  279. {
  280. // 最后一頁,不能右翻頁
  281. if ((currentPage) == totalPage)
  282. {
  283. return;
  284. }
  285. else
  286. {
  287. // 向右翻頁 左翻頁若隱藏即可顯示
  288. if (LeftPage_Button.gameObject.activeSelf == false)
  289. {
  290. LeftPage_Button.gameObject.SetActive(true);
  291. }
  292. currentPage = currentPage + 1;
  293. // Toggle 更新 (这里不会主动调用 toggle 事件)
  294. PageToggleGroup.GetChild(currentPage - 1).GetComponent<MultiPageToggleItem>().SetToggleIsOn(true);
  295. int startInt = ((currentPage - 1) * ShowPageIconItemCountPerPage);
  296. // 翻到最后一頁(可能不是10個,要計算展示個數)
  297. if (currentPage == (totalPage))
  298. {
  299. // 隱藏所有,保障多餘的不會顯示
  300. HidePageItems();
  301. int endInt = PageIconListCount - startInt;
  302. for (int i = 0; i < endInt; i++)
  303. {
  304. PageItem pageItem = PageGrid_Image.GetChild(i).GetComponent<PageItem>();
  305. pageItem.gameObject.SetActive(true);
  306. SetPageItemData(startInt + i, mPageIconTextureList, pageItem);
  307. }
  308. // 把右翻頁隱藏
  309. RightPage_Button.gameObject.SetActive(false);
  310. }
  311. else
  312. { // 翻到不是最后一頁(10個展示即可)
  313. for (int i = 0; i < ShowPageIconItemCountPerPage; i++)
  314. {
  315. PageItem pageItem = PageGrid_Image.GetChild(i).GetComponent<PageItem>();
  316. pageItem.gameObject.SetActive(true);
  317. SetPageItemData(startInt + i, mPageIconTextureList, pageItem);
  318. }
  319. }
  320. }
  321. //更新頁碼
  322. PageCount_Text.text = currentPage + "/" + totalPage;
  323. }
  324. #endregion
  325. #region Toggle 翻页数据(这里有底部(子) MultiPageToggleItem 发送消息调用)
  326. /// <summary>
  327. /// Toggle翻页
  328. /// </summary>
  329. /// <param name="page"></param>
  330. public void ToggelPageEvent(int page)
  331. {
  332. if (currentPage == page)
  333. {
  334. return;
  335. }
  336. currentPage = page;
  337. // 最前頁,把左翻頁隱藏
  338. if (currentPage == 1)
  339. {
  340. LeftPage_Button.gameObject.SetActive(false);
  341. }
  342. else
  343. {
  344. LeftPage_Button.gameObject.SetActive(true);
  345. }
  346. if (currentPage == totalPage)
  347. {
  348. // 把右翻頁隱藏
  349. RightPage_Button.gameObject.SetActive(false);
  350. }
  351. else
  352. {
  353. RightPage_Button.gameObject.SetActive(true);
  354. }
  355. int startInt = ((currentPage - 1) * ShowPageIconItemCountPerPage);
  356. // 翻到最后一頁(可能不是10個,要計算展示個數)
  357. if (currentPage == (totalPage))
  358. {
  359. // 隱藏所有,保障多餘的不會顯示
  360. HidePageItems();
  361. int endInt = PageIconListCount - startInt;
  362. for (int i = 0; i < endInt; i++)
  363. {
  364. PageItem pageItem = PageGrid_Image.GetChild(i).GetComponent<PageItem>();
  365. pageItem.gameObject.SetActive(true);
  366. SetPageItemData(startInt + i, mPageIconTextureList, pageItem);
  367. }
  368. }
  369. else
  370. { // 翻到不是最后一頁(10個展示即可)
  371. for (int i = 0; i < ShowPageIconItemCountPerPage; i++)
  372. {
  373. PageItem pageItem = PageGrid_Image.GetChild(i).GetComponent<PageItem>();
  374. pageItem.gameObject.SetActive(true);
  375. SetPageItemData(startInt + i, mPageIconTextureList, pageItem);
  376. }
  377. }
  378. //更新頁碼
  379. PageCount_Text.text = currentPage + "/" + totalPage;
  380. }
  381. #endregion
  382. /// <summary>
  383. /// 设置生成的 pageItem 的数据
  384. /// </summary>
  385. /// <param name="i"></param>
  386. /// <param name="pageIconlist"></param>
  387. /// <param name="pageItem"></param>
  388. private void SetPageItemData(int i, List<Texture2D> pageIconlist, PageItem pageItem)
  389. {
  390. pageItem.transform.SetParent(PageGrid_Image, false);
  391. pageItem.transform.localScale = Vector3.one;
  392. pageItem.IconTexture = pageIconlist[i];
  393. }
  394. /// <summary>
  395. /// 设置 MultiPageToggleItem 实体数据信息
  396. /// </summary>
  397. /// <param name="i"></param>
  398. /// <param name="multiPageToggleItem"></param>
  399. private void SetMultiPageToggleItemData(int i, MultiPageToggleItem multiPageToggleItem)
  400. {
  401. multiPageToggleItem.transform.SetParent(PageToggleGroup, false);
  402. multiPageToggleItem.transform.localScale = Vector3.one;
  403. multiPageToggleItem.Page = i + 1;
  404. multiPageToggleItem.SetToggleGroup(PageToggleGroup.GetComponent<ToggleGroup>());
  405. // 为了默认第一个 toggle 是 isOn 显示的(预制体要提前 false)
  406. if (i != 0)
  407. {
  408. multiPageToggleItem.SetToggleIsOn(false);
  409. }
  410. else
  411. {
  412. multiPageToggleItem.SetToggleIsOn(true);
  413. }
  414. }
  415. /// <summary>
  416. /// 隐藏 PageToggleGroup 下所有 MultiPageToggleItem 实体
  417. /// </summary>
  418. private void HideMultiPageToggleItems()
  419. {
  420. for (int i = 0; i < PageToggleGroup.childCount; i++)
  421. {
  422. PageToggleGroup.GetChild(i).gameObject.SetActive(false);
  423. }
  424. }
  425. /// <summary>
  426. /// 隐藏 PageGrid_Image 下所有 PageItem 实体
  427. /// </summary>
  428. private void HidePageItems()
  429. {
  430. for (int i = 0; i < PageGrid_Image.childCount; i++)
  431. {
  432. PageGrid_Image.GetChild(i).gameObject.SetActive(false);
  433. }
  434. }
  435. #endregion
  436. #region Unity回调
  437. /// <summary>
  438. /// Awake this instance.
  439. /// </summary>
  440. void Awake()
  441. {
  442. // 下載 AppItem
  443. mPageItem = LoadAppItem();
  444. mMultiPageToggleItem = LoadMultiPageToggleItem();
  445. // 綁定左右翻页的监听事件
  446. LeftPage_Button.onClick.AddListener(LeftPageEvent);
  447. RightPage_Button.onClick.AddListener(RightPageEvent);
  448. }
  449. private void OnEnable()
  450. {
  451. //StartCoroutine(ShowPageIconList( GetWindiowsPCPicturesPath.GetPictutresPathsInTheFilePath(pageIconPath)));
  452. }
  453. #endregion
  454. }

 

2、PageItem.cs

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using UnityEngine.UI;
  7. public class PageItem : MonoBehaviour
  8. {
  9. public RawImage Icon_RawImage;
  10. // 字段
  11. private Texture2D iconTexture;
  12. private string iconpath;
  13. // 属性
  14. public Texture2D IconTexture { get => iconTexture;
  15. set {
  16. iconTexture = value;
  17. if (Icon_RawImage != null)
  18. {
  19. Icon_RawImage.texture = iconTexture;
  20. }
  21. }
  22. }
  23. public string Iconpath { get => iconpath; set {
  24. iconpath = value;
  25. GetTexture(iconpath,(texture2d)=> {
  26. IconTexture = texture2d;
  27. });
  28. }
  29. }
  30. #region 其他视情况而定的方法 协程加载指定路径的图片 (注意:动态多图加载,这种方式很有可能会让程序奔溃)
  31. /// <summary>
  32. /// 请求图片
  33. /// </summary>
  34. /// <param name="url">图片地址,like 'http://www.my-server.com/image.png '</param>
  35. /// <param name="action">请求发起后处理回调结果的委托,处理请求结果的图片</param>
  36. /// <returns></returns>
  37. public void GetTexture(string url, Action<Texture2D> actionResult)
  38. {
  39. StartCoroutine(_GetTexture(url, actionResult));
  40. }
  41. /// <summary>
  42. /// 请求图片
  43. /// </summary>
  44. /// <param name="url">图片地址,like 'http://www.my-server.com/image.png '</param>
  45. /// <param name="action">请求发起后处理回调结果的委托,处理请求结果的图片</param>
  46. /// <returns></returns>
  47. IEnumerator _GetTexture(string url, Action<Texture2D> actionResult)
  48. {
  49. UnityWebRequest uwr = new UnityWebRequest(url);
  50. DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
  51. uwr.downloadHandler = downloadTexture;
  52. yield return uwr.SendWebRequest();
  53. Texture2D t = null;
  54. if (!(uwr.isNetworkError || uwr.isHttpError))
  55. {
  56. t = downloadTexture.texture;
  57. }
  58. else
  59. {
  60. Debug.Log("下载失败,请检查网络,或者下载地址是否正确 ");
  61. }
  62. if (actionResult != null)
  63. {
  64. actionResult(t);
  65. }
  66. }
  67. #endregion
  68. }

 

3、MultiPageToggleItem.cs

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class MultiPageToggleItem : MonoBehaviour
  4. {
  5. public Toggle MultiPage_Toggle;
  6. private int page;
  7. public int Page { get => page; set => page = value; }
  8. // Start is called before the first frame update
  9. void Start()
  10. {
  11. // 绑定事件
  12. MultiPage_Toggle.onValueChanged.AddListener((isOn) => {
  13. if (isOn == true)
  14. {
  15. Debug.Log("发送消息 ToggelPageEvent " + Page);
  16. SendMessageUpwards("ToggelPageEvent", Page);
  17. }
  18. });
  19. }
  20. /// <summary>
  21. /// 设置 ToggleGroup
  22. /// </summary>
  23. /// <param name="toggleGroup"></param>
  24. public void SetToggleGroup(ToggleGroup toggleGroup)
  25. {
  26. MultiPage_Toggle.group = toggleGroup;
  27. }
  28. /// <summary>
  29. /// 设置 isON
  30. /// </summary>
  31. /// <param name="isOn"></param>
  32. public void SetToggleIsOn(bool isOn)
  33. {
  34. MultiPage_Toggle.isOn = isOn;
  35. }
  36. }

 

4、GetWindiowsPCPicturesPath.cs

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using System.IO;
  6. using UnityEngine;
  7. public class GetWindiowsPCPicturesPath {
  8. public static void DebugPaths() {
  9. List<string> filePaths = GetPictutresPathsInTheFilePath(@"D:\Tmp\Images");
  10. for (int i = 0; i < filePaths.Count; i++)
  11. {
  12. Debug.Log(filePaths[i]);
  13. }
  14. }
  15. /// <summary>
  16. /// win 中获取指定文件夹下的图片,不包括子文件夹的图片
  17. /// </summary>
  18. /// <param name="path"></param>
  19. /// <returns>图片路径集合列表</returns>
  20. public static List<string> GetPictutresPathsInTheFilePath(string filePath)
  21. {
  22. List<string> filePaths = new List<string>();
  23. string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG";
  24. string[] ImageType = imgtype.Split('|');
  25. for (int i = 0; i < ImageType.Length; i++)
  26. {
  27. //获取指定文件夹下所有的图片路径
  28. string[] dirs = Directory.GetFiles(filePath, ImageType[i]);
  29. for (int j = 0; j < dirs.Length; j++)
  30. {
  31. filePaths.Add(dirs[j]);
  32. }
  33. }
  34. return filePaths;
  35. }
  36. }

 

七、参考工程

下载地址:https://download.csdn.net/download/u014361280/12639147

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/660441
推荐阅读
相关标签
  

闽ICP备14008679号