unity+Android+PC加载本地图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LoadImage : MonoBehaviour {
    Texture2D tempImage;
    private RawImage RawImageRef;
    // Use this for initialization
    void Start () {
        RawImageRef = gameObject.GetComponent<RawImage>();
        StartCoroutine(OnLoadImage("uploadFile/projectImg/22.jpg", RawImageRef));
    }
    
    // Update is called once per frame
    void Update () {
        
    }
    IEnumerator OnLoadImage(string path, RawImage image)
    {

        string filePath = "file://" + Application.persistentDataPath + "/document/" + path;
        //string filePath = "file://" + "/sdcard/TKData/SaveData/" + "document/" + path;//手机
        WWW www = new WWW(filePath);
        yield return www;
        if (www.error != null)
        {
            Debug.LogError(filePath + www.error);

            image.gameObject.SetActive(false);
        }
        else
        {
            Debug.LogError(filePath);
            image.gameObject.SetActive(true);
            tempImage = www.texture;
            image.texture = tempImage;
        }

    }
}

将图片放入本地指定文件夹即可

unity设置如下

原文地址:https://www.cnblogs.com/WalkingSnail/p/10997657.html