Unity打包发布后正确读取StreamingAssets文件夹下文件跟调用手机邮箱像指定邮箱地址发邮件

因为项目需要调手机浏览器从StreamingAssets文件夹打开Html文件,花了一天时间查看Unity官方API跟一些论坛帖子,做的一个DEMO
在这里插入图片描述

代码在这里插入代码片
using UnityEngine;
using System.IO;
using UnityEngine.UI;
using System.Collections;

public class NativeShareAndroid : MonoBehaviour
{
public Text waitObject;

public TextAsset fileInfo;

void Awake()
{
  
    StartCoroutine(url());
}





public void OnShareNative()
{//这里是邮箱内容
    string email = "848840773@qq.com";
    string subject = MyEscapeURL("");//邮件预设主题
    string body = MyEscapeURL("");//邮件预设内容
    Application.OpenURL("mailto:" + email + "?subject=" + subject + "&body=" + body);

    waitObject.text = "链接中...";
}
string MyEscapeURL(string url)
{
    //%20是空格在url中的编码,这个方法将url中非法的字符转换成%20格式
    return WWW.EscapeURL(url).Replace("+", "%20");
}
public static string GetPath()
{

#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
return “file:///”;
#else
return “file://”;
#endif

}

public void OnMousOPH5()
{
    string filepath = "/TEST.html";
    string path = "";
    if (filepath.EndsWith("html"))
    {
        path = Application.persistentDataPath + filepath;
    }
    else
    {
        path = Application.persistentDataPath + filepath + ".html";
    }
    //直接用附带的浏览器打开Html文件
    Application.OpenURL(path);
}


public IEnumerator url()
{
    yield return new WaitForSeconds(0.5f);//先等待0.5S 等截面渲染出来再执行
    string surl = GetStreamingAssetsPath("TEST.html");
   // Application.streamingAssetsPath + "/TEST.html"



    WWW aa = new WWW(surl);
    Debug.Log(aa);

    yield return aa;

    if (aa.isDone)
    {
        //拷贝数据库到指定路径
        string path = Application.persistentDataPath + "/" + "TEST.html";
        waitObject.text = Application.persistentDataPath;
        File.WriteAllBytes(path, aa.bytes);
    }

  
   
}



public static string fileIsExists(string path)
{
    if (!File.Exists(path))
    {
       
        return "找不到配置文件:" + path;
    }
    else
    {
        return "找到配置文件:" + path;
    }

    
}




public static string GetStreamingAssetsPath(string path)
{//当前系统判断
    if (Application.platform == RuntimePlatform.Android)
    {
        return Application.streamingAssetsPath + "/" + path;
    }
    else if (Application.platform == RuntimePlatform.IPhonePlayer)
    {
        return GetPath() + Application.streamingAssetsPath + "/" + path;
    }
    else if (Application.platform == RuntimePlatform.OSXEditor)
    {
        return GetPath() + Application.streamingAssetsPath + "/" + path;
    }
    else if (Application.platform == RuntimePlatform.WindowsPlayer)
    {
        return "file://" + Application.streamingAssetsPath + "/" + path;
    }
    else if (Application.platform == RuntimePlatform.WindowsEditor)
    {


        ///编辑模式下测试路径
        return Application.streamingAssetsPath + "/" + path;
    }

    return "";
}

}

直接复制拿去吧或者下载DMOE,地址:https://download.csdn.net/download/qq_41111369/11081871

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity是一种跨平台的游戏引擎,很多游戏都使用它来开发。在Unity中,打包读取StreamingAssets是一种常见的方式,它可以让开发者将数据文件一起打包到应用程序中,再通过代码来读取这些数据文件UnityStreamingAssets文件夹是存储相关文件的一个文件夹,所有在该文件夹中的文件都将被打包到应用程序中。为了读取这些文件,可以使用Unity的WWW类或者UnityWebRequest类。这两个类都可以从本地文件系统或者网络地址读取文件,然后将文件内容转化为字符串或字节流。 如果想要从StreamingAssets读取文件,可以使用以下代码: ``` string filePath = Path.Combine(Application.streamingAssetsPath, "FileName.extension"); ``` 其中FileName.extension是文件名和文件类型,可以是txt、json、xml等等。读取StreamingAssets中的文件可以使用以下代码: ```csharp IEnumerator ReadStreamingAssetsFile(string path) { string filePath = Path.Combine(Application.streamingAssetsPath, path); UnityWebRequest www = UnityWebRequest.Get(filePath); www.SendWebRequest(); while (!www.isDone) { yield return null; } if (!www.isNetworkError && www.isDone) { byte[] bytes = www.downloadHandler.data; string jsonString = Encoding.UTF8.GetString(bytes); //Do something with the json string } } ``` 这是一种通过UnityWebRequest类来从StreamingAssets读取文件的方法。先根据文件路径获取完整路径,然后使用UnityWebRequest来获取文件内容,并将其转化为字节数组或字符串,最后进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值