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
            }
       }
   }
相关推荐
光锥智能1 天前
NVIDIA 发布 Nemotron 3 Nano Omni 模型
vr
mxwin1 天前
Unity Shader 半透明物体为什么不能写入深度缓冲?
unity·游戏引擎·shader
晚枫歌F1 天前
三层时间轮的实现
网络·unity·游戏引擎
咸鱼永不翻身1 天前
Lua脚本事件检查工具
unity·lua·工具
leo__5201 天前
单载波中继系统资源分配算法MATLAB仿真程序
算法·matlab·unity
星幻元宇VR1 天前
VR单人地震体验平台助力防灾减灾教育
科技·学习·安全·vr·虚拟现实
努力长头发的程序猿1 天前
Unity使用ScriptableObject序列化资源
unity·游戏引擎
mxwin1 天前
Unity Shader 手写基于 PBR 的 URP Lit Shader 核心光照计算
unity·游戏引擎·shader
小贺儿开发1 天前
Unity3D 智能云端数字标牌系统
unity·阿里云·人机交互·视频·oss·广告·互动
魔士于安1 天前
Unity windows 同步 异步 打开文件文件夹工具
游戏·unity·游戏引擎·贴图·模型