Unity实现网页端 打开本地文件选择图片上传到阿里云的OSS

不知道怎么使用阿里云OSS的,请点击这里

更多OSS详细的设置请看另一篇文章,请点击这里
这两个文章的功能都是一样的,只是另一个文字,打包网页后,不能调用Win系统对话框选择图片,只能把程序里的image的图片上传到OSS,
当前文章可以实现打开Win系统对话框选择本地图片上传到OSS

第一步 在Plugins文件下创建 OSSFileUploader.jslib(用记事本创建,更改后缀名就行)文件

mergeInto(LibraryManager.library, {
   
   
    // Global variables for callback functions
    uploadCallback: null,
    statusCallback: null,

    // Initialize uploader
    InitializeUploader: function() {
   
   
        // Create hidden file input element
        var fileInput = document.createElement('input');
        fileInput.type = 'file';
        fileInput.id = 'unityFileInput';
        fileInput.style.display = 'none';
        document.body.appendChild(fileInput);

        // Listen for file selection events
        fileInput.addEventListener('change', async function(event) {
   
   
            var file = event.target.files[0];
            if (file) {
   
   
                console.log('Unity selected file:', file.name);
                
                // Get global scope safely within the event handler
                var globalScope;
                if (typeof window !== 'undefined') {
   
   
                    globalScope = window;
                } else if (typeof global !== 'undefined') {
   
   
                    globalScope = global;
                } else if (typeof self !== 'undefined') {
   
   
                    globalScope = self;
                } else {
   
   
                    globalScope = {
   
   };
                }
                
                // Call Unity callback function
                if (globalScope.uploadCallbackObjectName && globalScope.uploadCallbackMethodName) {
   
   
                    SendMessage(globalScope.uploadCallbackObjectName, globalScope.uploadCallbackMethodName, file.name);
                }
                
                // Start upload - inline all functionality here
                try {
   
   
                    // Notify Unity of status update
                    if (globalScope.statusCallbackObjectName && globalScope.statusCallbackMethodName) {
   
   
                        SendMessage(globalScope.statusCallbackObjectName, globalScope.statusCallbackMethodName, "Getting upload signature...");
                    }

                    // Generate timestamped filename
                    var now = new Date();
                    var timestamp = now.getFullYear() +
                        String(now.getMonth() + 1).padStart(2, '0') +
                        String(now.getDate()).padStart(2, '0') +
                        String(now.getHours()).padStart(2, '0') +
                        String(now.getMinutes()).padStart(2, '0') +
                        String(now.getSeconds()
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值