(014)Unity3d 打包检查引用丢失脚本

新建 BuildAPKCheck 脚本, 放在项目Editor (防止打包安卓工程的时候报错) 路径下即可:

using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;

public class BuildAPKCheck : MonoBehaviour, IPreprocessBuildWithReport
{
    public int callbackOrder { get; }

    public void OnPreprocessBuild(BuildReport report)
    {
        // clearConsole();
        findMissingReferencesInScene();
    }
    
    private void findMissingReferencesInScene()
    {
        List<Component> components = new List<Component>();
        
        // TODO: ADD NEED CHECK COMPONENT
        UIManager uiManager = GameObject.FindObjectOfType<UIManager>();
        FightUIController fightUIController = GameObject.FindObjectOfType<FightUIController>();
        HomeController homeController = GameObject.FindObjectOfType<HomeController>();

        if (uiManager) components.Add(uiManager);
        if (fightUIController) components.Add(fightUIController);
        if (homeController) components.Add(homeController);

        bool hasError = false;
        for (var j = 0; j < components.Count; j++)
        {
            var c = components[j];

            using (var so = new SerializedObject(c))
            {
                using (var sp = so.GetIterator())
                {
                    while (sp.NextVisible(true))
                    {
                        if (sp.propertyType == SerializedPropertyType.ObjectReference)
                        {                          
                            if (sp.objectReferenceValue == null)
                            {
                                hasError = true;
                                Debug.LogError($"Game场景中, {c.name}的字段 {sp.name} 丢失引用: {sp.propertyType}", c);
                            }
                        }
                    }
                }
            }
        }

        if (hasError)
        {
            throw new BuildFailedException("打包失败!");
        }
    }

    private static void clearConsole()
    {
        var logEntries = Type.GetType("UnityEditor.LogEntries, UnityEditor.dll");
        if (logEntries == null) return;

        var clearMethod = logEntries.GetMethod("Clear",
            BindingFlags.Static | BindingFlags.Public);
        if (clearMethod == null) return;

        clearMethod.Invoke(null, null);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity3D 中,可以使用 WebCamTexture 类来访问摄像头。WebCamTexture 可以从摄像头捕获视频流,并将其作为纹理传递给 Unity3D。以下是在 Unity3D打包 WebGL 并调用手机摄像头的步骤: 1. 在 Unity3D 中创建一个新的场景。 2. 在场景中创建一个 Plane 对象,并将其放置在场景中心。 3. 在 Inspector 窗口中,将 Plane 对象的 Scale 设置为 (10, 1, 10)。 4. 在场景中创建一个 Cube 对象,将其放置在 Plane 对象上方,并将其旋转 45 度。 5. 在 Cube 对象上添加一个新的脚本,并将其命名为 WebcamTextureScript。 6. 在脚本中编写以下代码: ``` using UnityEngine; using System.Collections; public class WebcamTextureScript : MonoBehaviour { // The webcam texture private WebCamTexture webcamTexture; // Use this for initialization void Start () { // Get the webcam device WebCamDevice[] devices = WebCamTexture.devices; if (devices.Length > 0) { // Create a new webcam texture webcamTexture = new WebCamTexture(devices[0].name); // Set the texture on the material GetComponent<Renderer>().material.mainTexture = webcamTexture; // Start the webcam webcamTexture.Play(); } } } ``` 7. 在 Unity3D 菜单中选择 File > Build Settings。 8. 在 Build Settings 窗口中,选择 WebGL 平台,并点击 Build 按钮。 9. 在生成的项目目录中,找到 index.html 文件并编辑它。 10. 在文件中找到以下代码: ``` <script src="Build/UnityLoader.js"></script> <script> var gameInstance = UnityLoader.instantiate("gameContainer", "Build/Build.json", {onProgress: UnityProgress}); </script> ``` 11. 在上面的代码后面添加以下代码: ``` <script> navigator.mediaDevices.getUserMedia({video: true}).then(function(stream) { var video = document.querySelector('video'); video.srcObject = stream; video.onloadedmetadata = function(e) { video.play(); }; }).catch(function(err) { console.log(err.name + ": " + err.message); }); </script> ``` 12. 保存并关闭 index.html 文件。 13. 在浏览器中打开 index.html 文件,应该可以看到摄像头的视频流在 Unity3D 场景中。 注意:调用摄像头需要 HTTPS 协议或 localhost 环境。如果您使用的是 localhost 环境,则需要在浏览器中输入 https://localhost:port 打开项目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值