102 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			102 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
|   | using System.Collections; | ||
|  | using System.Collections.Generic; | ||
|  | using UnityEngine; | ||
|  | using UnityEngine.Networking; | ||
|  | using UnityEngine.UI; | ||
|  | 
 | ||
|  | public class ImportImages : MonoBehaviour | ||
|  | { | ||
|  |     [SerializeField] private DownloadMod DM; | ||
|  |     [SerializeField] private GameObject ImagePrefab; | ||
|  |     [SerializeField] private Transform ImageContaier; | ||
|  |     [SerializeField] private List<GameObject> ImageList; | ||
|  |     private string FullImport; | ||
|  | 
 | ||
|  |     // Start is called before the first frame update | ||
|  |     void Start() { | ||
|  |         for (int i = 0; i < ImageList.Count; i++) { | ||
|  |             DestroyImmediate(ImageList[i]); | ||
|  |         } | ||
|  |         ImageList.Clear(); | ||
|  |          | ||
|  |     } | ||
|  | 
 | ||
|  |     // Update is called once per frame | ||
|  |     void Update() | ||
|  |     { | ||
|  |          | ||
|  |     } | ||
|  | 
 | ||
|  |     public void FillMe(string A) { | ||
|  |         FullImport = A; | ||
|  |         string[] Images = A.Split(","); | ||
|  |         string ID = DM.CurrentJSON.Mod.Id.ToString(); | ||
|  |          | ||
|  |         for (int i = 1; i < Images.Length; i++) { | ||
|  |             string Link = $"https://staticdelivery.nexusmods.com/mods/{Images[i]}"; | ||
|  |             GameObject imagePrefab = Instantiate(ImagePrefab, ImageContaier); | ||
|  |             RawImage Screenie = imagePrefab.GetComponent<ImagePrefab>().FillMe(Link, DM, true); | ||
|  |             ImageList.Add(imagePrefab); | ||
|  |             StartCoroutine(LoadWebP(Link, Screenie)); | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     public void OpenModPage() { | ||
|  |         Application.OpenURL(DM.CurrentJSON.Mod.Url+"?GetImages=true"); | ||
|  |     }	 | ||
|  |      | ||
|  |     private IEnumerator LoadImage (string B, RawImage A) { | ||
|  | 		UnityWebRequest WWW = UnityWebRequestTexture.GetTexture(B); | ||
|  | 		yield return WWW.SendWebRequest(); | ||
|  | 		if (WWW.result != UnityWebRequest.Result.ConnectionError) { | ||
|  | 			A.texture = DownloadHandlerTexture.GetContent(WWW); | ||
|  | 			A.SetNativeSize(); | ||
|  | 			A.SizeToParent(); | ||
|  | 		} | ||
|  | 	} | ||
|  |      | ||
|  | 	private IEnumerator LoadWebP(string url, RawImage rawImage) { | ||
|  | 		using (UnityWebRequest www = UnityWebRequest.Get(url)) { | ||
|  | 			yield return www.SendWebRequest(); | ||
|  | 
 | ||
|  | 			if (www.result != UnityWebRequest.Result.Success) { | ||
|  | 				Debug.LogError(www.error); | ||
|  | 			} else { | ||
|  | 				byte[] webPData = www.downloadHandler.data; | ||
|  | 				if (webPData == null || webPData.Length ==   0) { | ||
|  | 					Debug.LogError("No WebP data received."); | ||
|  | 					yield break; | ||
|  | 				} | ||
|  | 				WebpImporter.ByteWebpTexture2D(webPData, (texture) => { | ||
|  | 					rawImage.texture = texture; | ||
|  | 				}); | ||
|  | 
 | ||
|  | 				rawImage.SetNativeSize(); | ||
|  | 				rawImage.SizeToParent(); | ||
|  | 			} | ||
|  | 		} | ||
|  | 	} | ||
|  | 
 | ||
|  |     public void SaveImages() { | ||
|  |         string[] Images = FullImport.Split(",");         | ||
|  |         for (int i = 1; i < Images.Length; i++) { | ||
|  |             string Link = $"https://staticdelivery.nexusmods.com/mods/{Images[i]}"; | ||
|  |             GameObject imagePrefab = Instantiate(ImagePrefab, DM.ImageContainer); | ||
|  |             RawImage Screenie = imagePrefab.GetComponent<ImagePrefab>().FillMe(Link, DM); | ||
|  |             DM.ImagePrefabList.Add(imagePrefab); | ||
|  |             Screenie.texture = ImageList[i-1].GetComponent<ImagePrefab>().Image.texture; | ||
|  | 			Screenie.SetNativeSize(); | ||
|  | 			Screenie.SizeToParent(); | ||
|  |         //    StartCoroutine(LoadWebP(Link, Screenie)); | ||
|  |         } | ||
|  |          | ||
|  |         DM.UpdateImagesJSON(); | ||
|  |         gameObject.SetActive(false); | ||
|  | 
 | ||
|  |         for (int i = 0; i < ImageList.Count; i++) { | ||
|  |             DestroyImmediate(ImageList[i]); | ||
|  |         } | ||
|  |         ImageList.Clear(); | ||
|  |     } | ||
|  | } |