因为项目需要调手机浏览器从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