You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity导出HTML游戏导入Xcode后无法在UIWebView加载,求解决方法

How to Load Unity-Exported HTML Games in iOS Apps (Replace Deprecated UIWebView)

Hey there! Let's break down why your UIWebView approach isn't working and fix it with the right modern solution. First off, UIWebView has been deprecated by Apple since iOS 12—it's no longer maintained, and has known issues with modern web content like Unity WebGL builds. The replacement, WKWebView, is designed for this exact use case and will resolve most of your loading problems. Here's a step-by-step guide to get your game running:

1. Switch to WKWebView (Mandatory First Step)

WKWebView is the supported way to load web content on iOS now. You can set it up either via Storyboard or code:

  • Storyboard: Drag a WKWebView from the object library onto your view controller, set constraints to fill the screen, and create an IBOutlet for it.
  • Code: Use the snippet below to initialize it programmatically (more flexible for full-screen game content).

2. Properly Import Unity's Exported Files into Xcode

This is where many people trip up—your Unity files need to be in the correct structure so the HTML can reference assets properly:

  • Drag your Build folder, index.html, and Template folder directly into your Xcode project navigator.
  • When the import dialog pops up:
    • Check Copy items if needed (so files are copied into your project bundle).
    • Select Create folder references (this preserves the folder structure, which Unity's HTML relies on for relative paths).
  • Double-check in Build Phases > Copy Bundle Resources that all these files/folders are listed—if not, add them manually.

3. Load the HTML File with Correct Path Handling

WKWebView needs explicit access to the local file system to load relative assets. Use this code to load your index.html properly:

import UIKit
import WebKit

class GameViewController: UIViewController, WKNavigationDelegate {
    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Configure WKWebView for Unity WebGL
        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true
        preferences.javaScriptCanOpenWindowsAutomatically = true
        
        let config = WKWebViewConfiguration()
        config.preferences = preferences
        
        // Initialize full-screen web view
        webView = WKWebView(frame: view.bounds, configuration: config)
        webView.navigationDelegate = self
        webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(webView)
        
        // Load the local index.html
        guard let htmlURL = Bundle.main.url(forResource: "index", withExtension: "html") else {
            print("Error: Couldn't locate index.html in app bundle")
            return
        }
        
        // Use loadFileURL to grant access to all sibling files/folders (Build, Template)
        webView.loadFileURL(htmlURL, allowingReadAccessTo: htmlURL.deletingLastPathComponent())
    }
    
    // Debug helper: Catch and print navigation errors
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print("Failed to load content: \(error.localizedDescription)")
        // Check the error message—common issues include missing assets or path mismatches
    }
}

4. Fix Common Unity WebGL Specific Issues

  • Deployment Target: Set your Xcode project's deployment target to iOS 11 or higher—Unity WebGL uses ES6 features that WKWebView fully supports at this version.
  • ATS Settings: Since you're loading local files (file:// protocol), you don't need to modify App Transport Security (ATS) settings. If your game makes network requests later, you'll need to add exceptions in Info.plist, but that's not required for local loading.
  • Unity Export Settings: Double-check your Unity build settings for WebGL:
    • Ensure Compression Format is set to Disabled or Gzip (Brotli might cause issues on older iOS versions).
    • Verify that Export Project was checked when building, so you get the full Build and Template folders.

5. Debugging Tips

If the game still won't load:

  • Use the didFailProvisionalNavigation delegate method above to print errors—this will tell you if assets are missing, paths are wrong, or there's a JavaScript error.
  • Open index.html in a desktop browser first to confirm the Unity build works locally before moving to iOS.
  • Check the Xcode console for any warnings about missing files or permission issues.

内容的提问来源于stack exchange,提问作者user3126034

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:37:15