블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2204)N
Unity3D (519)N
Programming (462)N
Unreal (3)
Gamebryo (56)
Tip & Tech (175)
협업 (30)
3DS Max (3)
Game (12)
Utility (104)
Etc (91)
Link (29)
Portfolio (19)
Subject (90)
iOS,OSX (35)
Android (3)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (1)
Memories (19)
Interest (37)
Thinking (34)
한글 (25)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (16)
Exercise (3)
나만의 맛집 (2)
냥이 (9)
육아 (2)
Total1,222,210
Today40
Yesterday392
Statistics Graph

달력

« » 2018.09
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            

공지사항

태그목록

Tistory Cumulus Flash tag cloud by BLUEnLIVE requires Flash Player 9 or better.

Unity3D WebView

Unity3D/Plugins / 2015.01.21 17:46

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

Posted by blueasa
TAG

댓글을 달아 주세요