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
            }
       }
   }
相关推荐
Thomas_YXQ3 小时前
Unity3D Huatuo技术原理剖析详解
unity·unity3d·游戏开发·性能调优·热更新
zh路西法7 小时前
【C++决策和状态管理】从状态模式,有限状态机,行为树到决策树(二):从FSM开始的2D游戏角色操控底层源码编写
c++·游戏·unity·设计模式·状态模式
橘子遇见BUG10 小时前
Unity Shader学习日记 part 3 线性代数--矩阵变换
学习·线性代数·unity·矩阵·图形渲染
Artistation Game13 小时前
一、c#基础
游戏·unity·c#·游戏引擎
成都渲染101云渲染666614 小时前
云渲染,Enscape、D5、Lumion渲染提速教程
运维·服务器·unity·电脑·图形渲染·blender·houdini
HUIBUR科技16 小时前
从虚拟到现实:AI与AR/VR技术如何改变体验经济?
人工智能·ar·vr
超龄魔法少女2 天前
[Unity] ShaderGraph动态修改Keyword Enum,实现不同效果一键切换
unity·技术美术·shadergraph
蔗理苦2 天前
2024-12-24 NO1. XR Interaction ToolKit 环境配置
unity·quest3·xr toolkit
花生糖@2 天前
Android XR 应用程序开发 | 从 Unity 6 开发准备到应用程序构建的步骤
android·unity·xr·android xr
向宇it2 天前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎