Unity读取Android SDcard文件

本文介绍在Unity中通过两种方法加载SDCard上的图片资源:一是使用IO方式,涉及FileStream和Texture2D等类;二是使用WWW方式,通过协程加载图片并转换为Sprite。文章还提及了权限设置及Manifest文件的修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、添加权限
权限添加 :Player settings – Other settings – write permission的设置 Sdcard。这个是在Unity编辑器里打包的情况。

如果导出到studio 里面的话,可自行修改Manifest文件。
在这里插入图片描述
二、两种方式

1.IO方式 加载sdcard上的图片资源
加载的 /storage/emulated/0/ProjectName/image.jpg,

image = this.GetComponentInChildren<Image>(); 
   
      Debug.Log("IO加载用时: image = this.GetComponent<Image> ==========  " + image); 
   
      Debug.Log("IO加载:  Application.dataPath "  + Application.dataPath ); 
      // Application.dataPath /data/app/com.putao.ptx.core-1/base.apk 
   
      Debug.Log("IO加载:  Application.persistentDataPath " + Application.persistentDataPath); 
      // Application.persistentDataPath /storage/emulated/0/Android/data/com.putao.ptx.core/files  
   
   
      Debug.Log("IO加载:GameObject.Find" + GameObject.Find("Canvas/Avator").GetComponent<Image>()); 
   
       // /data/user/0/com.putao.paichallenge/cache/20170524_130527.jpg 
   
   
      // path =======  "/storage/emulated/0/ProjectName/image.jpg" 
      FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); 
      fileStream.Seek(0, SeekOrigin.Begin); 
      //创建文件长度缓冲区 
      byte[] bytes = new byte[fileStream.Length]; 
      //读取文件 
      fileStream.Read(bytes, 0, (int)fileStream.Length); 
      //释放文件读取流 
      fileStream.Close(); 
      fileStream.Dispose(); 
      fileStream = null; 
   
      //创建Texture 
      int width = 300; 
      int height = 372; 
      Texture2D texture = new Texture2D(width, height); 
      texture.LoadImage(bytes); 
   
   
      //创建Sprite      
      Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width,texture.height), new Vector2(0.5f, 0.5f)); 
   
      image.sprite = sprite; 

log上可以看出来 android上 getExternalFilesDir("") 和 Unity里面的Application.persistentDataPath是一致的

/storage/emulated/0/Android/data/com.putao.ptx.core/files

2.WWW方式加载本地图片
url= “file://”+ “/storage/emulated/0/ProjectName/image.jpg”

public void LoadByWWW(String path) 
    { 
        StartCoroutine(doLoadByWWW(path)); 
   
    } 
   
    IEnumerator doLoadByWWW(String path) 
    { 
        string url = "file://" + "/storage/emulated/0/PaiChallenge/image.jpg"; 
        Debug.Log("doLoadByWWW == url ==================   " + url); 
   
        WWW w = new WWW(url); 
   
        yield return w; 
   
        if (w.isDone) 
        { 
            Sprite sprite = Sprite.Create(w.texture, new Rect(0, 0, w.texture.width, w.texture.height), new Vector2(0.5f, 0.5f)); 
   
            GameObject.Find("Canvas/Avator").GetComponent<Image>().sprite = sprite; 
   
        } 
Unity是一款跨平台的游戏开发引擎,用于创建各种类型的游戏和应用程序。要在Unity中获取Android 10以上的MEID码(Mobile Equipment Identifier),可以通过使用Android的Java层代码与Unity进行交互来实现。 首先,在Unity项目中创建一个新的C#脚本文件,将其命名为"AndroidMEID.cs"。然后,在脚本中编写以下代码: ```c# using UnityEngine; public class AndroidMEID : MonoBehaviour { private AndroidJavaClass unityPlayer; private AndroidJavaObject currentActivity; private AndroidJavaObject telephonyManager; private string meid; void Start() { unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); telephonyManager = currentActivity.Call<AndroidJavaObject>("getSystemService", "phone"); meid = telephonyManager.Call<string>("getMeid"); Debug.Log("MEID: " + meid); } } ``` 这段代码使用Android的Java层代码获取TelephonyManager对象,然后调用getMeid方法获取MEID码,并将其存储在字符串变量"meid"中。最后,使用Debug.Log方法在Unity的控制台中输出MEID码。 将脚本文件附加到一个空的GameObject上,然后在Unity编辑器中运行项目。当游戏或应用程序在Android设备上运行时,脚本会在Start方法中获取并输出MEID码。 需要注意的是,为了在Android设备上获取MEID码,应用程序需要具有相应的权限。因此,在Unity的"Player Settings"中,确保在"Other Settings"下的"Write Access"选项中勾选了"External(SDCard)",并且在"Android Permissions"中添加了"READ_PHONE_STATE"权限。 希望以上回答能够对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值