Unity 批量动态添加图片,可后台替换
string filePath = Application.streamingAssetsPath + "/"+ str; if (Directory.Exists(filePath)) {//判断这个路径是否存在 DirectoryInfo direction = new DirectoryInfo(filePath); floders = direction.GetFiles("*.png", SearchOption.AllDirectories); } Debug.Log(floders.Length); StartCoroutine(GetTexture(true, allPeople)); IEnumerator GetTexture(bool boo,GameObject myOJB) { for (int i = 0; i < pathList.Count; i++) { UnityWebRequest uwr = UnityWebRequest.Get(pathList[i]); DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true); uwr.downloadHandler = downloadTexture; yield return uwr.SendWebRequest(); if (uwr.isNetworkError || uwr.isHttpError) { print(uwr.error); } else { Texture2D t = downloadTexture.texture; myOJB.transform.GetChild(i).GetComponent<Image>().sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), Vector2.one); } } }
posted on 2022-06-17 17:21 zqiang0803 阅读(186) 评论(0) 编辑 收藏 举报