Last active
September 20, 2018 05:48
-
-
Save GeneralD/cc94a8c0b9b1c22c7902f781265cc958 to your computer and use it in GitHub Desktop.
Unity RectTransformでWebView制御 ref: https://qiita.com/GeneralD/items/34bfbc1e1e90b7725413
This file contains 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 UnityEngine; | |
public static class ComponentExtensions { | |
public static T GetOrAddComponent<T>(this Component component) where T : Component => component.GetComponent<T>() ?? component.gameObject.AddComponent<T>(); | |
} |
This file contains 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.Linq; | |
using UniRx; | |
using UniRx.Triggers; | |
using UnityEngine; | |
using UnityEngine.UI; | |
[RequireComponent(typeof(RectTransform))] | |
public class WebViewRectAllocator : MonoBehaviour { | |
public StringReactiveProperty URL = new StringReactiveProperty(); | |
[SerializeField] private Button goBackButton; | |
[SerializeField] private Button goForwardButton; | |
private void Awake() { | |
var webViewObject = this.GetOrAddComponent<WebViewObject>(); | |
webViewObject.Init(msg => Debug.Log(msg)); | |
URL.Where(url => !string.IsNullOrEmpty(url)) | |
.Subscribe(webViewObject.LoadURL); | |
GetComponentsInParent<CanvasGroup>() | |
.Select(cg => cg.ObserveEveryValueChanged(c => c.alpha >= 1f)) | |
.CombineLatestValuesAreAllTrue() | |
.CombineLatest(this.ObserveEveryValueChanged(self => self.enabled), (l, r) => l && r) | |
.Subscribe(webViewObject.SetVisibility) | |
.AddTo(this); | |
var rect = GetComponent<RectTransform>(); | |
rect.OnRectTransformDimensionsChangeAsObservable() | |
.Merge(Observable.Return(Unit.Default)) | |
.Select(_ => new Vector3[4]) | |
.Do(rect.GetWorldCorners) | |
.Select(corners => new Vector4(corners.Min(c => c.x), corners.Max(c => c.x), corners.Min(c => c.y), corners.Max(c => c.y))) | |
.SelectMany(lrbt => Observable.Return(new Vector3[4]) | |
.Do(rect.GetComponentInParent<Canvas>().GetComponent<RectTransform>().GetWorldCorners) | |
.Select(corners => new Vector4(corners.Min(c => c.x), corners.Max(c => c.x), corners.Min(c => c.y), corners.Max(c => c.y))) | |
.Select(canvasLRBT => canvasLRBT - lrbt)) | |
.Select(lrbt => new { left = (int)-lrbt.x, right = (int)lrbt.y, bottom = (int)-lrbt.z, top = (int)lrbt.w }) | |
.CatchIgnore() | |
.Do(t => Debug.LogFormat("WebView margins: top={0}, left={1}, right={2}, bottom={3}", t.top, t.left, t.right, t.bottom)) | |
.Subscribe(t => webViewObject.SetMargins(t.left, t.top, t.right, t.bottom)) | |
.AddTo(this); | |
var checkInterval = 2f; | |
if (GoBackButton != null) Observable | |
.Interval(TimeSpan.FromSeconds(checkInterval)) | |
.Where(_ => webViewObject.GetVisibility()) | |
.Select(_ => webViewObject.CanGoBack()) | |
.BindToButtonOnClick(GoBackButton, _ => webViewObject.GoBack()); | |
if (GoForwardButton != null) Observable | |
.Interval(TimeSpan.FromSeconds(checkInterval)) | |
.Where(_ => webViewObject.GetVisibility()) | |
.Select(_ => webViewObject.CanGoForward()) | |
.BindToButtonOnClick(GoForwardButton, _ => webViewObject.GoForward()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment