当前位置:   article > 正文

Quest 2 VR程序读取本地图片

Quest 2 VR程序读取本地图片
  1. 建议在一个类里面先读取完成,再做其他事情。
  2. 图片放在Assets/StreamingAssets 下,因为 Application.streamingAssetsPath 就是对应这个路径
  3. 在Quest 2下,需要使用"jar:file://" 文件名前缀
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]);
        }
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

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
            }
       }
   }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/508224
推荐阅读
相关标签
  

闽ICP备14008679号