Created
February 13, 2020 13:58
-
-
Save tsweeper/fe8ba3dfa9630e0017655273a36342d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using DG.Tweening; | |
using LitJson; | |
using UnityEngine; | |
using UnityEngine.EventSystems; | |
using UnityEngine.SceneManagement; | |
using UnityEngine.UI; | |
using Vuplex.WebView; | |
using Vuplex.WebView.Demos; | |
public class WebViewManager : MonoBehaviour | |
{ | |
static public bool CONTROL_ENABLED = false; | |
const string TAG = "[WebViewManager] : "; | |
public Canvas canvas; | |
[HideInInspector] | |
public Vector3 originCanvasScale; | |
public string userAgent = "Mozilla/5.0 (Linux; Android 9; SM-G977N Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.108 Mobile Safari/537.36;AppVersion/1102/1020; Native;AppVersion/1102/1020; Native"; | |
[Serializable] | |
public class CustomWebViewConfig | |
{ | |
public string url = "https://deleted.unpublished-service.com/m/check-login"; | |
public string mainUrl = "https://deleted.unpublished-service.com/m/main"; | |
public Vector3 position = Vector3.zero; | |
public Vector2 size = new Vector2(720, 1280); | |
public Vector3 rotate = Vector3.zero; | |
} | |
public CustomWebViewConfig customWebViewConfig; | |
[Serializable] | |
public class CustomKeyboardConfig | |
{ | |
public bool isEnabled = true; | |
public Vector3 position = Vector3.zero; | |
public Vector3 rotation = Vector3.zero; | |
public Vector3 scale = Vector3.one*4; | |
} | |
public CustomKeyboardConfig customKeyboardConfig; | |
CanvasWebViewPrefab _canvasWebViewPrefab; | |
Keyboard _keyboard; | |
bool _isWebView = false; | |
private void Awake() | |
{ | |
originCanvasScale = canvas.transform.localScale; | |
} | |
private void Start() | |
{ | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
AndroidWebView.SetNativeVideoRenderingEnabled(false); | |
#endif | |
NativeManager.Instance.SetWebViewManager(this); | |
string userKey = PlayerPrefs.GetString("userKey", ""); | |
#if UNITY_EDITOR | |
userKey = "test"; | |
#endif | |
MakeWebView(); | |
//if (userKey.Equals("")) | |
//{ | |
// NativeManager.Instance.CallNative(NativeCommand.LOGIN, new object[] { customWebViewConfig.url }); | |
//} | |
//else | |
//{ | |
// MakeWebView(); | |
//} | |
} | |
public void MakeWebView() | |
{ | |
if (_isWebView) | |
{ | |
return; | |
} | |
_isWebView = true; | |
Debug.Log(TAG+ "MakeWebView"); | |
#if UNITY_EDITOR | |
// Simon: Vuplex Window plugins seems need both Agent and bool setting. | |
Web.SetUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36"); | |
Web.SetUserAgent(true); | |
#else | |
// But not in actual device | |
Web.SetUserAgent(userAgent); | |
#endif | |
_canvasWebViewPrefab = CanvasWebViewPrefab.Instantiate(); | |
_canvasWebViewPrefab.InitialUrl = customWebViewConfig.url; | |
_canvasWebViewPrefab.transform.SetParent(canvas.transform); | |
RectTransform rectTransform = _canvasWebViewPrefab.GetComponent<RectTransform>(); | |
rectTransform.anchorMin = Vector2.one * 0.5f; | |
rectTransform.anchorMax = Vector2.one * 0.5f; | |
rectTransform.localScale = Vector3.one; | |
rectTransform.anchoredPosition3D = customWebViewConfig.position; | |
rectTransform.sizeDelta = customWebViewConfig.size; // Simon: crash if sizeDelta is zero or null. | |
rectTransform.rotation = Quaternion.Euler(customWebViewConfig.rotate); | |
Debug.Log(TAG + "test123 111"); | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
AndroidWebView.SetClickCorrectionEnabled(false); | |
#endif | |
Debug.Log(TAG + "test123 222"); | |
_canvasWebViewPrefab.Initialized += (initializedSender, initializedEventArgs) => { | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
// (_canvasWebViewPrefab.WebView as AndroidWebView).ScriptAlert += WebView_ScriptAlert; | |
#endif | |
Sequence seq = DOTween.Sequence(); | |
seq.AppendInterval(1.0f); | |
seq.AppendCallback(()=> | |
{ | |
WebViewManager.CONTROL_ENABLED = true; | |
#if UNITY_ANDROID && !UNITY_EDITOR | |
AndroidWebView.SetClickCorrectionEnabled(false); | |
#endif | |
}); | |
_canvasWebViewPrefab.WebView.UrlChanged += WebView_UrlChanged; | |
_canvasWebViewPrefab.WebView.MessageEmitted += WebView_MessageEmitted; | |
if (customKeyboardConfig.isEnabled) | |
{ | |
MakeKeyboard(); | |
} | |
}; | |
} | |
//void WebView_ScriptAlert(object sender, ScriptAlertEventArgs e) | |
//{ | |
// Debug.Log(TAG+">>> recieved alert: " + e.Message); | |
//} | |
public CanvasWebViewPrefab GetCanvasWebViewPrefab() | |
{ | |
return _canvasWebViewPrefab; | |
} | |
void MakeKeyboard() | |
{ | |
_keyboard = Keyboard.Instantiate(); | |
var keyboardTransform = _keyboard.transform; | |
keyboardTransform.localPosition = customKeyboardConfig.position; | |
keyboardTransform.localScale = customKeyboardConfig.scale; | |
keyboardTransform.localRotation = Quaternion.Euler(customKeyboardConfig.rotation); | |
_keyboard.gameObject.SetActive(false); | |
_keyboard.InputReceived += (sender, eventArgs) => { | |
_canvasWebViewPrefab.WebView.HandleKeyboardInput(eventArgs.Value); | |
}; | |
#if UNITY_EDITOR | |
var hardwareKeyboardListener = HardwareKeyboardListener.Instantiate(); | |
hardwareKeyboardListener.InputReceived += (sender, eventArgs) => { | |
_canvasWebViewPrefab.WebView.HandleKeyboardInput(eventArgs.Value); | |
}; | |
#endif | |
_keyboard.transform.parent = canvas.transform; | |
} | |
private void WebView_UrlChanged(object sender, UrlChangedEventArgs e) | |
{ | |
if (_canvasWebViewPrefab.WebView != null) | |
{ | |
var serializedMessage = String.Format("{{ \"type\": \"SET_URL\", \"url\": \"{0}\" }}", e.Url); | |
Debug.Log(TAG + "WebView_UrlChanged === " + serializedMessage); | |
_canvasWebViewPrefab.WebView.PostMessage(serializedMessage); | |
if (e.Url.Equals(customWebViewConfig.mainUrl)) | |
{ | |
Debug.Log(TAG+ "WebView_UrlChanged main page"); | |
PlayerPrefs.SetString("userKey", "testlogin"); | |
PlayerPrefs.Save(); | |
//?? main 페이지에서 하는데 이상 | |
NativeManager.Instance.GetUserKey(); | |
} | |
} | |
} | |
private void WebView_MessageEmitted(object sender, EventArgs<string> e) | |
{ | |
var serializedMessage = e.Value; | |
var type = BridgeMessage.ParseType(serializedMessage); | |
Debug.Log(TAG + "WebView_MessageEmitted === " + type); | |
Debug.Log(TAG+ "serializedMessage === "+ serializedMessage); | |
if (type==null) | |
{ | |
Message(serializedMessage); | |
} | |
else | |
{ | |
if (type == "GO_BACK") | |
{ | |
_canvasWebViewPrefab.WebView.GoBack(); | |
} | |
else if (type == "GO_FORWARD") | |
{ | |
_canvasWebViewPrefab.WebView.GoForward(); | |
} | |
else if (type == "ACTIVE_ELEMENT_TYPE_CHANGED") | |
{ | |
if (customKeyboardConfig.isEnabled) | |
{ | |
_keyboard.gameObject.SetActive(!_keyboard.gameObject.activeInHierarchy); | |
} | |
} | |
} | |
} | |
string GetStringData(JsonData json, string key) | |
{ | |
string ret = null; | |
if (json.Keys.Contains(key)) | |
{ | |
if (json[key] != null) | |
{ | |
ret = json[key].ToString(); | |
} | |
} | |
return ret; | |
} | |
bool GetBoolData(JsonData json, string key) | |
{ | |
bool ret; | |
if (json[key].IsBoolean) | |
{ | |
ret = (bool)json[key]; | |
} | |
else if (json[key].IsString) | |
{ | |
ret = json[key].ToString().ToLower().Equals("true") ? true : false; | |
} | |
else | |
{ | |
ret = false; | |
} | |
return ret; | |
} | |
void Message(string msg) | |
{ | |
JsonData jsonData = JsonMapper.ToObject(msg); | |
string function = jsonData["function"].ToString(); | |
Debug.Log(TAG+ "Message function === " + function); | |
switch (function) | |
{ | |
case "onLogin": | |
{ | |
} | |
break; | |
case "appClose": | |
{ | |
} | |
break; | |
case "startUnity": | |
{ | |
string botId = GetStringData(jsonData, "botId"); | |
string character = GetStringData(jsonData, "character"); | |
string background = GetStringData(jsonData, "background"); | |
string startMode = GetStringData(jsonData, "startMode"); | |
string backgroundList = GetStringData(jsonData, "backgroundList"); | |
string missionName = GetStringData(jsonData, "missionName"); | |
string missionDesc = GetStringData(jsonData, "missionDesc"); | |
string chatMode = GetStringData(jsonData, "chatMode"); | |
string characterMode = GetStringData(jsonData, "characterMode"); | |
if (characterMode == null) | |
{ | |
characterMode = "unity"; | |
} | |
NativeManager.Instance.startUnity(botId,character,background,startMode, backgroundList, missionName,missionDesc, chatMode, characterMode); | |
} | |
break; | |
case "onTracking": | |
{ | |
} | |
break; | |
case "endUnity": | |
{ | |
NativeManager.Instance.endUnity(); | |
} | |
break; | |
case "onMessage": | |
{ | |
string text = GetStringData(jsonData, "text"); | |
string voice = GetStringData(jsonData, "voice"); | |
string length = GetStringData(jsonData, "length"); | |
bool noanswer = GetBoolData(jsonData, "noanswer"); | |
bool nextOutput = GetBoolData(jsonData, "nextOutput"); | |
bool endMission = GetBoolData(jsonData, "endMission"); | |
string youtube = GetStringData(jsonData, "youtube"); | |
string sentences = GetStringData(jsonData, "sentences"); | |
string youtubeStartTime = GetStringData(jsonData, "youtubeStartTime"); | |
string youtubeEndTime = GetStringData(jsonData, "youtubeEndTime"); | |
string characterExpression = GetStringData(jsonData, "characterExpression"); | |
string characterAnimation = GetStringData(jsonData, "characterAnimation"); | |
string background = GetStringData(jsonData, "background"); | |
bool freetalk = GetBoolData(jsonData, "freetalk"); | |
bool freetalkEnd = GetBoolData(jsonData, "freetalkEnd"); | |
NativeManager.Instance.onMessage(text, voice, length, noanswer, nextOutput,endMission, youtube, sentences, youtubeStartTime,youtubeEndTime,characterExpression,characterAnimation | |
, background, freetalk, freetalkEnd); | |
} | |
break; | |
case "onUserMessage": | |
{ | |
string text = GetStringData(jsonData, "text"); | |
string subtitle = GetStringData(jsonData, "subtitle"); | |
string voiceUrl = GetStringData(jsonData, "voiceUrl"); | |
NativeManager.Instance.onUserMessage(text, subtitle, voiceUrl); | |
} | |
break; | |
case "onSuggest": | |
{ | |
string tab = GetStringData(jsonData, "tab"); | |
string tags = GetStringData(jsonData, "tags"); | |
string text = GetStringData(jsonData, "text"); | |
NativeManager.Instance.onSuggest(tab, tags, text); | |
} | |
break; | |
case "onSuggestSubtitle": | |
{ | |
string texts = GetStringData(jsonData, "texts"); | |
string languages = GetStringData(jsonData, "languages"); | |
bool showText = GetBoolData(jsonData, "showText"); | |
NativeManager.Instance.onSuggestSubtitle(texts, languages, showText); | |
} | |
break; | |
case "onStartRecognize": | |
{ | |
string texts = GetStringData(jsonData, "texts"); | |
string speakGuide = GetStringData(jsonData, "speakGuide"); | |
NativeManager.Instance.onStartRecognize(texts, speakGuide); | |
} | |
break; | |
case "onGuage": | |
{ | |
} | |
break; | |
case "onAction": | |
{ | |
} | |
break; | |
case "onFeedback": | |
{ | |
} | |
break; | |
case "onIntroBot": | |
{ | |
} | |
break; | |
case "setViewMode": | |
{ | |
} | |
break; | |
case "practiceStart": | |
{ | |
string userKey = jsonData["userKey"].ToString(); | |
string courseIndex = jsonData["courseIndex"].ToString(); | |
string missionIndex = jsonData["missionIndex"].ToString(); | |
string chatMode = jsonData["chatMode"].ToString(); | |
string missionName = jsonData["missionName"].ToString(); | |
NativeManager.Instance.practiceStart(userKey, courseIndex, missionIndex, chatMode, missionName); | |
} | |
break; | |
case "onLoading": | |
{ | |
} | |
break; | |
case "stopLoading": | |
{ | |
} | |
break; | |
case "sendUserkey": | |
{ | |
string userKey = jsonData["userKey"].ToString(); | |
NativeManager.Instance.sendUserkey(userKey); | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
#region Android to Unity | |
public void Login(string str) | |
{ | |
MakeWebView(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment