赞
踩
using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.Networking; public class GlobalCoordinator : MonoBehaviour { public static GlobalCoordinator Instance { get; private set; } public Dictionary<string, Texture2D> allOriginalImagesAndMasks = new Dictionary<string, Texture2D>(); private string[] allImageId = { "004", "005", "010", "022", "044", "046", "055", "058", "066" }; public int itemsShouldInallOriginalImagesAndMasks; private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); // This keeps the object alive across scenes. } else { Destroy(gameObject); } } private void OnEnable() { itemsShouldInallOriginalImagesAndMasks = allImageId.Length; // load all data for (int ind = 0; ind < allImageId.Length; ind++) { string imageId = allImageId[ind]; string imageUrl = "images/" + imageId + ".png"; LoadImagesFromDirectory(imageUrl); } } public void LoadImagesFromDirectory(string url) { string streamingAssetsPath = Application.streamingAssetsPath + "/textures/" + url; Debug.Log(streamingAssetsPath); #if UNITY_EDITOR string filePath = "file://" + streamingAssetsPath; #else string filePath = "jar:file://" + streamingAssetsPath; #endif StartCoroutine(LoadTextureInAndroid(url, filePath)); } IEnumerator LoadTextureInAndroid(string url, string filePath) { // Load the PNG file and store it in the textures array UnityWebRequest www = UnityWebRequestTexture.GetTexture(filePath); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.LogError("<pf> Failed to load texture: " + www.error); } else { Texture2D imageTexture = DownloadHandlerTexture.GetContent(www); imageTexture.name = System.IO.Path.GetFileNameWithoutExtension(filePath); allOriginalImagesAndMasks[url] = imageTexture; Debug.Log("check whether allOriginalImagesAndMasks stored"); Debug.Log(url); Debug.Log(allOriginalImagesAndMasks[url]); } } }
In another class that will use allOriginalImagesAndMasks:
public class AnotherClass : BaseFlowLogic
{
private bool hasInitialize = false;
private void FixedUpdate()
{
if (!hasInitialize) {
if (GlobalCoordinator.Instance.allOriginalImagesAndMasks.Count == GlobalCoordinator.Instance.itemsShouldInallOriginalImagesAndMasks) {
// logic
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。