Unity的StreamingAssets文件夹在不同平台的不同读写方式

四个平台同理类推:android,pc,edit,iphone
第一使用条件编译指令:
优点是选择性编译,只会编译符合条件的
话不多说直接上代码
默认读取的文件名字是 string fileName

#if UNITY_EDITOR || UNITY_STANDALONE
            //编译器&pc
            url = "file://" + Application.dataPath + "/StreamingAssets/"+fileName;
#elif UNITY_IPHONE
             //iphone
            url = "file://" + Application.dataPath + "/Raw/"+fileName;
#elif UNITY_ANDROID
             //aAndroid
            url = "jar:file://" + Application.dataPath + "!/assets/"+fileName;
#endif

eg:如果是android平台,那么前面两个代码就不会被带走
2使用Application类进行判断

            //and
            if (Application.platform == RuntimePlatform.Android)
            {
                url = "jar:file://" + Application.dataPath + "!/assets/" + fileName;
            }
            else
            {
                //iPhone
                if(Application.platform ==RuntimePlatform.IPhonePlayer)
                {
                    url = "file://" + Application.dataPath + "/Raw/" + fileName;
                }
                else
                {
                    //edit&pc
                    url = "file://" + Application.dataPath + "/StreamingAssets/" + fileName;
                }
            }

最后顺便附上一个WWW类读取文件

WWW www = new WWW(url);
            while(true)
            {
                if(www.isDone)
                   return www.text;
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值