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
            }
       }
   }
相关推荐
心疼你的一切17 小时前
使用Unity引擎开发Rokid主机应用的模型交互操作
游戏·ui·unity·c#·游戏引擎·交互
淡海水18 小时前
【URP】Unity[内置Shader]光照着色器Lit
unity·游戏引擎·shader·urp·着色器·lit
爱吃小胖橘18 小时前
Lua语法(2)
开发语言·unity·lua
心疼你的一切1 天前
使用Unity引擎开发Rokid主机应用的全面配置交互操作
学习·游戏·unity·c#·游戏引擎·交互
ellis19702 天前
LuaC API知识点汇总
unity·lua
maki0772 天前
虚幻版Pico大空间VR入门教程 05 —— 原点坐标和项目优化技巧整理
android·游戏引擎·vr·虚幻·pico·htc vive·大空间
maki0772 天前
VR大空间资料 01 —— 常用VR框架对比
android·ue5·游戏引擎·vr·虚幻·pico
maki0772 天前
VR大空间资料 03 —— VRGK使用体验和源码分析
android·vr·虚幻·源码分析·oculus·htc vive·vrgk
maki0773 天前
VR大空间资料 02 —— 常用Body IK对比
android·游戏引擎·vr·虚幻·pico·ik
雪下的新火3 天前
爆炸特效-Unity-04-shader&粒子系统
经验分享·笔记·unity·游戏引擎·shader·粒子系统