Quest 2 VR程序读取本地图片

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

csharp 复制代码
public class AnotherClass : BaseFlowLogic
{
	private bool hasInitialize = false;

    private void FixedUpdate()
    {
        if (!hasInitialize) {
            if (GlobalCoordinator.Instance.allOriginalImagesAndMasks.Count == GlobalCoordinator.Instance.itemsShouldInallOriginalImagesAndMasks) {
            // logic
            }
       }
   }
相关推荐
Artistation Game12 小时前
九、怪物行为逻辑
游戏·unity·游戏引擎
百里香酚兰12 小时前
【AI学习笔记】基于Unity+DeepSeek开发的一些BUG记录&解决方案
人工智能·学习·unity·大模型·deepseek
dangoxiba15 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十三集:制作小骑士的接触地刺复活机制以及完善地图的可交互对象
游戏·unity·visualstudio·c#·游戏引擎
小殷要努力刷题!18 小时前
国创——VR虚拟陪伴
vr
摩尔线程18 小时前
使用MTVerseXR SDK实现VR串流
3d·xr·图形渲染·vr·摩尔线程
先生沉默先1 天前
使用Materialize制作unity的贴图,Materialize的简单教程,Materialize学习日志
学习·unity·贴图
十画_8242 天前
Visual Studio 小技巧记录
unity·visual studio
red_redemption2 天前
cpp,git,unity学习
git·unity·游戏引擎
tealcwu2 天前
【Unity踩坑】Unity更新Google Play结算库
unity·游戏引擎
实时云渲染dlxyz66882 天前
VR视频怎样进行加密和一机一码的使用?--加密(一)
vr·vr视频加密·vr视频·加密视频·一机一码