前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity 关于低版本是否可以引用高版本构建内容的可行性验证

Unity 关于低版本是否可以引用高版本构建内容的可行性验证

作者头像
CoderZ
发布2022-09-08 09:35:43
1.1K0
发布2022-09-08 09:35:43
举报

🎈 简介

本篇内容以Unity的一个相对较低的版本(2017.4.40)和一个相对较高的版本(2020.3.33),来验证在低版本中是否可以使用高版本中构建的内容,包括如下内容:

•在Unity2017.4.40(使用C#6)中是否可以引入Unity2020.3.33(使用C#8)构建的dll并正常使用;•在Unity2017.4.40中是否可以加载在Unity2020.3.33中构建的AssetsBundle资源;

🎈 低版本是否可以引用高版本构建的dll

在Unity2020.3.33中,我们开启一个协程,使用UnityWebRequest发起网络请求来获取百度知道网页(www.baidu.com)上的内容,代码示例如下:[1]

代码语言:javascript
复制
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);
            }
        }
    }
}

运行结果如下:

Console Log

其中using语句在C# 8.0中有了新的写法(C# 8.0中的新增功能 - C#指南[2]),如下图所示:

using 声明

我们在示例代码使用新的using声明

代码语言:javascript
复制
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中会通过如下方式判断:

代码语言:javascript
复制
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)了:

代码语言:javascript
复制
//
// 摘要:
//     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来判断请求是否成功:

代码语言:javascript
复制
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中去使用,接口代码如下:

代码语言:javascript
复制
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));
            }
        }
    }
}

📍 如何构建dll动态库

打开Visual Studio创建新项目,模板选择如图所示:

创建新项目

C# 8.0对应框架.Net Framework 4.8(C#版本 与 .NET Framework 对应关系及各版本语法差异[3])

配置新项目

创建后将我们的示例代码拷入其中,发现UnityEngine的部分报错,因为我们还没有引用UnityEngine.dll

封装接口

UnityEngine.dll所在文件夹目录如下,需要到Unity Editor的安装目录下去找:

UnityEngine.dll

添加引用:

添加引用

引用添加完成,再次右键项目,点击生成,然后将生成的dll动态库导入到Unity2017.4.40创建的项目中。

生成dll动态库

📍 如何将Unity使用的C#语言版本改为6.0

参考文章:配置Unity2017和VS2015使用C# 6.0[4]

•在Player Sttings中将Scripting Runtime Version修改为Experimental(.Net 4.6 Equivalent)

Player Settings

•在Visual Studio中打开工具 - 选项 - 适用于Unity的工具 - 杂项,将访问项目属性修改为true;

访问项目属性

•重启再打开属性设置目标框架为.Net Framework 4.6.1,编写测试脚本:

代码语言:javascript
复制
using UnityEngine;
using SKFramework.Test;

public class Example : MonoBehaviour 
{
    private void Start()
    {
        new TEST().Execute(this);
    }
}

运行结果:

运行结果

🎈 低版本是否可以加载高版本构建的AssetsBundle

•在Unity2020.3.33中创建一个Prefab预制体:

Prefab

•新建Assets Bundle

Assets Bundle

•构建Assets Bundle,使用工具代码如下:

代码语言:javascript
复制
#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

•将构建出的ab包导入到Unity2017.4.40项目中的Streaming Assets文件夹中:

Streaming Assets

编写测试脚本:

代码语言:javascript
复制
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);
        }
    }
}

运行结果:

Console Log

🎈 结论

以上述的结果来看,在相对较低的版本中,无论是引入相对较高的版本生成的dll,还是加载相对较高版本构建的ab包,都会出现些许问题,是否有相应解决方案尚需确定。

References

[1] www.baidu.com)上的内容,代码示例如下:: http://www.baidu.com)上的内容,代码示例如下: [2] C# 8.0中的新增功能 - C#指南: https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-8#using-declarations [3] C#版本 与 .NET Framework 对应关系及各版本语法差异: https://blog.csdn.net/chunmiao3032/article/details/119974405 [4] 配置Unity2017和VS2015使用C# 6.0: http://imweirui.com/blog/2017/12/13/%E9%85%8D%E7%BD%AEunity2017%E5%92%8Cvs2015%E4%BD%BF%E7%94%A8c-6-0/

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-09-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 当代野生程序猿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 🎈 简介
  • 🎈 低版本是否可以引用高版本构建的dll
    • 📍 如何构建dll动态库
      • 📍 如何将Unity使用的C#语言版本改为6.0
        • References
    • 🎈 低版本是否可以加载高版本构建的AssetsBundle
    • 🎈 结论
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档