本篇内容以Unity的一个相对较低的版本(2017.4.40)
和一个相对较高的版本(2020.3.33)
,来验证在低版本中是否可以使用高版本中构建的内容,包括如下内容:
在Unity2020.3.33中,我们开启一个携程,使用UnityWebRequest
发起网络请求来获取百度知道网页(www.baidu.com)上的内容,代码示例如下:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
namespace SKFramework.Test
{
public class TEST : MonoBehaviour
{
private void Start()
{
StartCoroutine(TestCoroutine());
}
private IEnumerator TestCoroutine()
{
string url = "www.baidu.com";
using (UnityWebRequest request = UnityWebRequest.Get(url))
{
yield return request.SendWebRequest();
Debug.Log(request.downloadHandler.text);
}
}
}
}
运行结果如下:
其中using语句在C# 8.0
中有了新的写法(C# 8.0中的新增功能 - C#指南),如下图所示:
我们在示例代码使用新的using声明
:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
namespace SKFramework.Test
{
public class TEST : MonoBehaviour
{
private void Start()
{
StartCoroutine(TestCoroutine());
}
private IEnumerator TestCoroutine()
{
string url = "www.baidu.com";
using UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
Debug.Log(request.downloadHandler.text);
}
}
}
在yield return request.SendWebRequest
发起网络请求后,一般会先判断请求是否成功,在以往的API中会通过如下方式判断:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
namespace SKFramework.Test
{
public class TEST : MonoBehaviour
{
private void Start()
{
StartCoroutine(TestCoroutine());
}
private IEnumerator TestCoroutine()
{
string url = "www.baidu.com";
using UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log(request.downloadHandler.text);
}
else
{
Debug.LogError(string.Format("发起网络请求失败:{0}", request.error));
}
}
}
}
但是当我们查看其定义可以发现它已经弃用(Obsolete)
了:
//
// 摘要:
// Returns true after this UnityWebRequest encounters a system error. (Read Only)
[Obsolete("UnityWebRequest.isNetworkError is deprecated. Use (UnityWebRequest.result == UnityWebRequest.Result.ConnectionError) instead.", false)]
public bool isNetworkError => result == Result.ConnectionError;
//
// 摘要:
// Returns true after this UnityWebRequest receives an HTTP response code indicating
// an error. (Read Only)
[Obsolete("UnityWebRequest.isHttpError is deprecated. Use (UnityWebRequest.result == UnityWebRequest.Result.ProtocolError) instead.", false)]
public bool isHttpError => result == Result.ProtocolError;
因为在新版本使用新增属性result
来判断请求是否成功:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
namespace SKFramework.Test
{
public class TEST : MonoBehaviour
{
private void Start()
{
StartCoroutine(TestCoroutine());
}
private IEnumerator TestCoroutine()
{
string url = "www.baidu.com";
using UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
Debug.Log(request.downloadHandler.text);
}
else
{
Debug.LogError(string.Format("发起网络请求失败:{0}", request.error));
}
}
}
}
下面我们将其封装为一个接口并构建dll导入到Unity2017.4.40中去使用,接口代码如下:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
namespace SKFramework.Test
{
public class TEST
{
public void Execute(MonoBehaviour executer)
{
executer.StartCoroutine(TestCoroutine());
}
private IEnumerator TestCoroutine()
{
string url = "www.baidu.com";
using UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
Debug.Log(request.downloadHandler.text);
}
else
{
Debug.LogError(string.Format("发起网络请求失败:{0}", request.error));
}
}
}
}
打开Visual Studio
创建新项目,模板选择如图所示:
C# 8.0
对应框架.Net Framework 4.8
(C#版本 与 .NET Framework 对应关系及各版本语法差异)
创建后将我们的示例代码拷入其中,发现UnityEngine的部分报错,因为我们还没有引用UnityEngine.dll
UnityEngine.dll
所在文件夹目录如下,需要到Unity Editor的安装目录下去找:
添加引用:
引用添加完成,再次右键项目,点击生成,然后将生成的dll动态库导入到Unity2017.4.40创建的项目中。
在Player Sttings
中将Scripting Runtime Version
修改为Experimental(.Net 4.6 Equivalent)
;
在Visual Studio
中打开工具 - 选项 - 适用于Unity的工具 - 杂项,将访问项目属性
修改为true;
重启再打开属性设置目标框架为.Net Framework 4.6.1,编写测试脚本:
using UnityEngine;
using SKFramework.Test;
public class Example : MonoBehaviour
{
private void Start()
{
new TEST().Execute(this);
}
}
运行结果:
Assets Bundle
:Assets Bundle
,使用工具代码如下:#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEngine;
namespace SK.Framework.Tools
{
public class SimpleAssetsBundle : EditorWindow
{
[MenuItem("SKFramework/Tools/Simple AssetsBundle")]
public static void Open()
{
var window = GetWindow<SimpleAssetsBundle>();
window.titleContent = new GUIContent("Simple AssetsBundle");
window.minSize = new Vector2(300f, 100f);
window.maxSize = new Vector2(1080f, 100f);
window.Show();
}
//打包路径
private string path;
//打包选项
private BuildAssetBundleOptions options;
//目标平台
private BuildTarget target;
private const float labelWidth = 80f;
private void OnEnable()
{
path = EditorPrefs.HasKey(EditorPrefsKeys.path) ? EditorPrefs.GetString(EditorPrefsKeys.path) : Application.streamingAssetsPath;
options = EditorPrefs.HasKey(EditorPrefsKeys.options) ? (BuildAssetBundleOptions)EditorPrefs.GetInt(EditorPrefsKeys.options) : BuildAssetBundleOptions.None;
target = EditorPrefs.HasKey(EditorPrefsKeys.target) ? (BuildTarget)EditorPrefs.GetInt(EditorPrefsKeys.target) : BuildTarget.StandaloneWindows;
}
private void OnGUI()
{
//路径
GUILayout.BeginHorizontal();
GUILayout.Label("Path", GUILayout.Width(labelWidth));
string newPath = EditorGUILayout.TextField(path);
if (newPath != path)
{
path = newPath;
EditorPrefs.SetString(EditorPrefsKeys.path, path);
}
//浏览 选择路径
if (GUILayout.Button("Browse", GUILayout.Width(60f)))
{
newPath = EditorUtility.OpenFolderPanel("AssetsBundle构建路径", Application.dataPath, string.Empty);
if (!string.IsNullOrEmpty(newPath) && newPath != path)
{
path = newPath;
EditorPrefs.SetString(EditorPrefsKeys.path, path);
}
}
GUILayout.EndHorizontal();
//选项
GUILayout.BeginHorizontal();
GUILayout.Label("Options", GUILayout.Width(labelWidth));
var newOptions = (BuildAssetBundleOptions)EditorGUILayout.EnumPopup(options);
if (newOptions != options)
{
options = newOptions;
EditorPrefs.SetInt(EditorPrefsKeys.options, (int)options);
}
GUILayout.EndHorizontal();
//平台
GUILayout.BeginHorizontal();
GUILayout.Label("Target", GUILayout.Width(labelWidth));
var newTarget = (BuildTarget)EditorGUILayout.EnumPopup(target);
if (newTarget != target)
{
target = newTarget;
EditorPrefs.SetInt(EditorPrefsKeys.target, (int)target);
}
GUILayout.EndHorizontal();
GUILayout.FlexibleSpace();
//构建按钮
if (GUILayout.Button("Build"))
{
//检查路径是否有效
if (!Directory.Exists(path))
{
Debug.LogError(string.Format("无效路径 {0}", path));
return;
}
//提醒
if (EditorUtility.DisplayDialog("提醒", "构建AssetsBundle将花费一定时间,是否确定开始?", "确定", "取消"))
{
//开始构建
BuildPipeline.BuildAssetBundles(path, options, target);
}
}
}
private class EditorPrefsKeys
{
public static string path = "SIMPLEASSETSBUNDLE_PATH";
public static string options = "SIMPLEASSETSBUNDLE_OPTIONS";
public static string target = "SIMPLEASSETSBUNDLE_TARGET";
}
}
}
#endif
Streaming Assets
文件夹中:编写测试脚本:
using UnityEngine;
using SKFramework.Test;
using System.Collections;
using UnityEngine.Networking;
public class Example : MonoBehaviour
{
private void Start()
{
//new TEST().Execute(this);
StartCoroutine(ExampleCoroutine());
}
private IEnumerator ExampleCoroutine()
{
string url = Application.streamingAssetsPath + "/player";
WWW www = new WWW(url);
yield return www;
if (www.error == null)
{
var ab = www.assetBundle;
Debug.Log(ab == null);
}
else
{
Debug.LogError(www.error);
}
}
}
运行结果:
以上述的结果来看,在相对较低的版本中,无论是引入相对较高的版本生成的dll,还是加载相对较高版本构建的ab包,都会出现些许问题,是否有相应解决方案尚需确定。