Unity 3D에서 WebView를 쓸 때 유용한 소스 입니다.
일본 개발자분이 오픈소스로 라이센스로 공개한 것 같더군요...
[소스]
https://github.com/gree/unity-webview
- Java로 작성된 Android 용 Native Plugin
- Objective-C로 작성된 iOS 용 Native Plugin
- Objective-C로 작성된 OS X 용 Native Plugin
- Native Plugin을 사용하기위한 C # Plugin
[예제 프로젝트]
https://github.com/keijiro/unity-webview-integration
unity-webview-integration 소스는 웹 페이지를 WebView로 호출에서 아래와 같이 unity와 기능을 연동한 예제 입니다.
javascript 예제여서 좀 아쉽네요. C#으로 수정해봐야 겠습니다.
http://keijiro.github.com/unity-webview-integration/index.html#
...
<title>Unity - WebView Integration Test Page</title>
<p><ul>
<li><a href="#" onclick='unity.callback("/spawn")'>Spawn a box</a></li>
<li><a href="#" onclick='unity.callback("/spawn", {color:"red"})'>Spawn a red box</a></li>
<li><a href="#" onclick='unity.callback("/spawn", {color:"blue"})'>Spawn a blue box</a></li>
<li><a href="#" onclick='unity.callback("/spawn", {color:"red", scale:0.5})'>Spawn a small red box</a></li>
<li><a href="#" onclick='unity.callback("/spawn", {color:"blue", scale:0.5})'>Spawn a small blue box</a></li>
<li><a href="#" onclick='unity.callback("/note", {text:"こんにちは"})'>「こんにちは」</a></li>
<li><a href="#" onclick='unity.callback("/note", {text:"ごきげんいかが"})'>「ごきげんいかが」</a></li>
<li><a href="#" onclick='unity.callback("/note", {text:"!@#$%^&*()-+"})'>"!@#$%^&*()-+"</a></li>
<li><a href="#" onclick='unity.callback("/close")'>Close</a></li>
<li><a href="page2.html">Go to page 2.</a></li>
</ul></p>
...
TestInterface.js (unity 3d script)
...
// Process messages coming from the web view.
private function ProcessMessages() {
while (true) {
// Poll a message or break.
var message = WebMediator.PollMessage();
if (!message) break;
if (message.path == "/spawn") {
// "spawn" message.
if (message.args.ContainsKey("color")) {
var prefab = (message.args["color"] == "red") ? redBoxPrefab : blueBoxPrefab;
} else {
prefab = Random.value < 0.5 ? redBoxPrefab : blueBoxPrefab;
}
var box = Instantiate(prefab, redBoxPrefab.transform.position, Random.rotation) as GameObject;
if (message.args.ContainsKey("scale")) {
box.transform.localScale = Vector3.one * float.Parse(message.args["scale"] as String);
}
} else if (message.path == "/note") {
// "note" message.
note = message.args["text"] as String;
} else if (message.path == "/print") {
// "print" message.
var text = message.args["line1"] as String;
if (message.args.ContainsKey("line2")) {
text += "\n" + message.args["line2"] as String;
}
Debug.Log(text);
Debug.Log("(" + text.Length + " chars)");
} else if (message.path == "/close") {
// "close" message.
DeactivateWebView();
}
}
}
...
// C#으로 변환한 소스 입니다. by asuss님(출처 : http://reysion.tistory.com/34 의 댓글)
using UnityEngine;
using System.Collections;
public class TestWebView : MonoBehaviour {
public GUISkin guiSkin;
public GameObject redBoxPrefab;
public GameObject blueBoxPrefab;
private string note;
// Show the web view (with margins) and load the index page.
void ActivateWebView() {
WebMediator.LoadUrl("http://keijiro.github.com/unity-webview-integration/index.html");
WebMediator.SetMargin(12, Screen.height / 2 + 12, 12, 12);
WebMediator.Show();
}
// Hide the web view.
void DeactivateWebView() {
WebMediator.Hide();
// Clear the state of the web view (by loading a blank page).
WebMediator.LoadUrl("about:blank");
}
// Process messages coming from the web view.
void ProcessMessages() {
while (true) {
// Poll a message or break.
WebMediatorMessage message = WebMediator.PollMessage();
if (message == null)
break;
if (message.path == "/spawn") {
// "spawn" message.
GameObject prefab = null;
if (message.args.ContainsKey("color")) {
prefab = (message.args["color"] == "red") ? redBoxPrefab : blueBoxPrefab;
} else {
prefab = Random.value < 0.5 ? redBoxPrefab : blueBoxPrefab;
}
var box = Instantiate(prefab, redBoxPrefab.transform.position, Random.rotation) as GameObject;
if (message.args.ContainsKey("scale")) {
box.transform.localScale = Vector3.one * float.Parse(message.args["scale"] as string);
}
} else if (message.path == "/note") {
// "note" message.
note = message.args["text"] as string;
} else if (message.path == "/print") {
// "print" message.
var text = message.args["line1"] as string;
if (message.args.ContainsKey("line2")) {
text += "\n" + message.args["line2"] as string;
}
Debug.Log(text);
Debug.Log("(" + text.Length + " chars)");
} else if (message.path == "/close") {
// "close" message.
DeactivateWebView();
}
}
}
void Start() {
WebMediator.Install();
}
void Update() {
if (WebMediator.IsVisible()) {
ProcessMessages();
} else if (Input.GetButtonDown("Fire1") && Input.mousePosition.y < Screen.height / 2) {
ActivateWebView();
}
}
void OnGUI() {
float sw = Screen.width;
float sh = Screen.height;
GUI.skin = guiSkin;
if(note != null)
GUI.Label(new Rect(0, 0, sw, sh/2), note);
GUI.Label(new Rect(0, sh/2, sw, sh/2), "TAP HERE", "center");
}
}
출처 : http://reysion.tistory.com/34