Multiple Unity Scene Script for iOS Build - javascript

Two Scenes in Unity. Have to write a script for controlling unity scenes(2) in native iOS app, suppose 2 buttons in a native app when I click 1st button have to show scene1 and the 2nd button for scene2 like so. I found a script for android but need to write it for iOS.
Attached Script for Android,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.VR;
using System;
public class AnimatorScript : MonoBehaviour
{
public Animator animation;
AndroidJavaObject intent , currentActivity , extras;
bool hasExtra;
string arguments;
// Use this for initialization
void Start ()
{
try
{
AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
intent = currentActivity.Call<AndroidJavaObject>("getIntent");
hasExtra = intent.Call<bool> ("hasExtra", "test_val");
Debug.Log(hasExtra);
Debug.Log("start");
}
catch(Exception e)
{
Debug.Log(e);
}
}
public void loadScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
// Update is called once per frame
void Update ()
{
if (hasExtra)
{
Debug.Log("has extra");
extras = intent.Call<AndroidJavaObject>("getExtras");
arguments = extras.Call<string> ("getString", "test_val");
if(string.Equals(arguments,"scene1"))
{
animation.Play("dancing_warrior_sun-001");
}
if(string.Equals(arguments,"scene2"))
{
// animation.Play("seating_poses-001");
SceneManager.LoadScene(1);
}
else Debug.Log("No Animation Found");
Debug.Log(arguments);
}
else
{
Debug.Log("no extra");
}
}
}

There are two ways of Scene change/switch you can do,
1) In Unity place button by UI, and write script for scene change as follows,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenuActions : MonoBehaviour {
public void MENU_ACTION_NextButton(string sceneName)
{
Application.LoadLevel(sceneName);
}
}
Drag this script to Main camera and click button, at the bottom of inspector click to add this main camera and name your scene name that have to be changed.Do this for scene 2 with same script and name the scene as well.
2) If you have button in your native app then write scripts as follows and try to write script for void update as well,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using UnityEngine.SceneManagement;
public class SceneChanger : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//SceneManager.LoadScene(scene);
// try your script................
}
public void ChangeScene( string scene ) {
// Application.LoadLevel(scene);
SceneManager.LoadScene(scene);
}
}
Hope this will give you the concept..

Building an application with a native plugin for iOS
You can follow the official document, use UnitySendMessage from iOS to call the scene-control script at Unity.

Related

SWT Browser not displaying pages using D3

I would like to display a html page that uses D3 in an SWT Browser.
If I load a URL like www.google.com it loads correctly and allows me to search etc. If I try loading a page that contains D3 I find that nothing gets displayed and no errors are output.
Example
public class D3BrowserExample {
public static void main(String args[]) {
Display display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
browser.addTitleListener(new TitleListener() {
#Override
public void changed(TitleEvent event) {
shell.setText(event.title);
}
});
browser.setBounds(0, 0, 1200, 900);
shell.pack();
shell.open();
browser.setJavascriptEnabled(true);
browser.setUrl("https://observablehq.com/#d3/force-directed-graph");
browser.addProgressListener(new ProgressListener() {
#Override
public void completed(ProgressEvent event) {
System.out.println("Page loaded");
}
#Override
public void changed(ProgressEvent event) {
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Is it possible to execute D3 using an SWT Browser? Also how can I get the Javascript output to debug the issue?
Update
I've tried this on another Windows 10 PC with version 4.3 of SWT. The complete method is called for the progress listener but nothing displays.
Eclipse Bugzilla
https://bugs.eclipse.org/bugs/show_bug.cgi?id=568789

Mapbox NavigationViewOptions and NavigationLauncherOptions

I'm new with Mapbox and I already have a few days trying to figure out how can I add a progressChangeListener to my NavigationLauncherOptions.
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
Point originPoint = Point.fromLngLat(-80.311641,25.910195);
Point destinationPoint = Point.fromLngLat(-80.312159, 25.911922);
NavigationRoute.builder(MainActivity.this)
.accessToken(Mapbox.getAccessToken())
.origin(originPoint)
.destination(destinationPoint)
.build()
.getRoute(new Callback<DirectionsResponse>() {
#Override
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> response) {
currentRoute = response.body().routes().get(0);
NavigationLauncherOptions options = NavigationLauncherOptions.builder()
.directionsRoute(currentRoute)
.shouldSimulateRoute(true)
.build();
NavigationLauncher.startNavigation(MainActivity.this, options);
NavigationViewOptions.Builder optionsNavigate = NavigationViewOptions.builder();
optionsNavigate.progressChangeListener(new ProgressChangeListener() {
#Override
public void onProgressChange(Location location, RouteProgress routeProgress) {
Log.v("RES", String.valueOf(routeProgress.currentState()));
if (routeProgress.currentState().equals(RouteProgressState.ROUTE_ARRIVED)) {
// Execute arrival logic
}
}
});
}
#Override
public void onFailure(Call<DirectionsResponse> call, Throwable throwable) {
}
});
I try the code attached but it doesn't work.
Any help is really appreciated.
Thank you
As noted here in Mapbox's route progress documentation for the Navigation UI SDK:
This listeners is only available if you are adding NavigationView to
your Activity or Fragment layout XML via NavigationViewOptions. You
are not able to add them to NavigationLauncherOptions.
As such, I'd recommend adding the NavigationView via NavigationViewOptions rather than NavigationLauncherOptions and following the detailed guidance in Mapbox's route progress documentation, if you need access to the progress listeners. The mapbox/mapbox-navigation-android example app also provides some useful examples.

How do i retrieve page information from inside of Unity?

I need to communicate Unity WebAssemly with page in both directions.
This is how I execute Unity functions from page:
<script>
var gameInstance = UnityLoader.instantiate("gameContainer", "https://propper_url/Build/_Builds.json", {onProgress: UnityProgress});
function unityObjectFun(index)
{
gameInstance.SendMessage ("ObjectInstance", "objectFunctionName", index);
}
</script>
<a onclick="unityObjectFun(1)">invoke</a>
<div class="webgl-content">
<div id="gameContainer" myValue="need this to read from Unity"></div>
</div>
But how can I do in reverse. Is it possible to retrieve myValue from Unity.
Alternatively is it possible to notify page that Unity loaded scene and started playing it so I could send to it myValue?
To perform any JavaScript code on a page from the Unity WebAssembly there are two things required:
Create the Javascript code as plugin.
Import the code in Unity script.
The whole procedure is described in this guide: https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html.
The plugin code must reside in Assets/Plugins/pluginname.jslib path (the extension is important) and have similar structure:
mergeInto(LibraryManager.library, {
Hello: function () {
window.alert("Hello, world!");
},
HelloReturn: function () {
return "Hello, world!";
}
}
To use it there is required import:
using System.Runtime.InteropServices;
public class NewBehaviourScript : MonoBehaviour {
[DllImport("__Internal")]
private static extern void Hello();
[DllImport("__Internal")]
private static extern string HelloReturn();
void Start() {
Hello();
Debug.log(HelloReturn());
}
}

Awesomium .NET. Wpf. TitleChanged event

I have a very basic application, that shows website content within the WPF app. Everything works fine, except TitleChangedEvent. Here is the code sample (XAML):
<Window xmlns:awe="http://schemas.awesomium.com/winfx" x:Class="ShopChat.Desktop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:webControls="clr-namespace:System.Web.UI.WebControls;assembly=System.Web"
Title="{Binding ElementName=WebControl, Path=Title}" MinHeight="480" MinWidth="640">
<Grid>
<awe:WebControl x:Name="WebControl"/>
</Grid>
And this is main window code-behind:
public MainWindow()
{
InitializeComponent();
string url = #"http://shopchat.dev";
try
{
url = ConfigurationManager.AppSettings.Get("Url");
}
catch (Exception)
{
}
WebControl.Source = new Uri(url);
WebControl.TitleChanged += WebControl_OnTitleChanged;
this.WindowTitle = "Quickchat";
}
public string WindowTitle
{
get { return (string)GetValue(WindowTitleProperty); }
set { SetValue(WindowTitleProperty, value); }
}
// Using a DependencyProperty as the backing store for WindowTitle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty WindowTitleProperty =
DependencyProperty.Register("WindowTitle", typeof(string), typeof(MainWindow), new PropertyMetadata(null));
private void WebControl_OnTitleChanged(object sender, TitleChangedEventArgs e)
{
this.WindowTitle = e.Title;
}
I've also tried to bind to window title directly using Binding ElementName=WebControl. That didn't help me either.
JavaScript client code is very simple: it changes the document title on timer (setInterval).
What am I doing wrong?
try like this code
public MainWindow()
{
InitializeComponent();
DataContext = this;
MyTitle = "Title";
}
Then you just need in the XAML
Title="{Binding MyTitle}"
Then you don't need the dependency property.
Then I would like to use this INotifyPropertyChanged with a standard property.
The issue was solved. TitleChanged event seemed to be insufficient. I've incorporated the usage of global js object to get the necessary behavior.

How to catch element click by ID in Android webview?

I have the viewPager component which is containing the several webviews with HTML content from remote server.
Is it simple HTML code without possibility to change the HTMl output on the server side.
I would like to ask, how can i catch the click(tap) event on the specified element with the given ID in Android?
ViewPager
private void initViewPager() {
pager = (ViewPager) findViewById(R.id.my_pager);
adapter = new FragmentStatePagerAdapter(
getSupportFragmentManager()
) {
#Override
public int getCount() {
// This makes sure getItem doesn't use a position
// that is out of bounds of our array of URLs
Logger.d(String.valueOf(mWelcomeController.loadedPagesToDisplay.size()));
return mWelcomeController.loadedPagesToDisplay.size();
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
Logger.d(mWelcomeController.loadedPagesToDisplay.toString());
return BrowserFragment.newInstance(
mWelcomeController.loadedPagesToDisplay.get(position)
);
}
};
//Let the pager know which adapter it is supposed to use
pager.setAdapter(adapter);
}
Because I cannot modify the HTML output on the server side (maybe inject some attributes into DOM on device ?) I cannot use something like that:
http://www.scriptscoop.com/t/21b53b896c9e/javascript-how-to-detect-button-click-in-webview-android.html
Detect click on HTML button through javascript in Android WebView.
I would like just something like this:
Find the given element in the HTML code
Update the HTML code (add
onclick event)
Catch this event in native code
For that you need to parse the html, a good html parser for Java (and therefor also Android) is Jsoup.
You can do something like:
// Connect to the web site
Document doc = Jsoup.connect(url).get();
Element button = doc.select("#buttonid");
button.html("new stuff here");
//parse back and put in webview
String finaloutput = doc.html();
1.
// setting
wv.addJavascriptInterface(new MyJsToAndroid(),"my");
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);
2.
// JsCallBack
class MyJsToAndroid extends Object{
#JavascriptInterface
public void myClick(String idOrClass) {
Log.d(TAG, "myClick-> " + idOrClass);
}
}
3.
// JS--
public static String addMyClickCallBackJs() {
String js = "javascript:";
js += "function myClick(event){" +
"if(event.target.className == null){my.myClick(event.target.id)}" +
"else{my.myClick(event.target.className)}}";
js += "document.addEventListener(\"click\",myClick,true);";
return js;
}
4.
#Override
public void onPageFinished(WebView wv, String url) {
//...
wv.evaluateJavascript(addMyClickCallBackJs(),null);
//...
}
So, look at the 2 log.

Categories

Resources