I have integrated Unity in an Android activity (similar to this).
I now need to dynamically load a scene depending on user input.
I've tried replicating how Unity loads a scene, in the only way that I know an Android Activity is able to communicate with the UnityPlayer, but obviously doesn't work:
UnityPlayer.UnitySendMessage("Application", "LoadLevel", "9");
I know there are other ways of loading a scene in Unity, but I don't know how to call those from Android itself (also consider that UnitySendMessage only allows one parameter).
If you plan to load Unity activity from Android, you should create an empty scene. Call this this Main Menu, then make it the default scene that loads through the Build Settings. Make it index 0.
The goal of this empty scene is to load specific scene when script attached to it is called.
In this Main Menu scene, create a GameObject called SceneLoader then attach the script below to it:
public class SceneLoader : MonoBehaviour
{
public void loadScene(string sceneIndex)
{
UnityEngine.SceneManagement.SceneManager.LoadScene(Convert.ToInt16(sceneIndex));
}
}
You should also create a GameObject called SceneLoader in every other scene and attach the script above to all of them.
Now, load Unity Activity which automatically loads the default/empty scene. Since there is only one GameObject/SceneLoader in it, it will load very fast.
You can now load other scenes from Java with:
UnityPlayer.UnitySendMessage("SceneLoader", "loadScene", "9");
Related
I'm trying to create a react-native apps with three js using expo-gl, expo-three frameworks.
Following is the list of imports..
import { ExpoWebGLRenderingContext, GLView } from 'expo-gl';
import ExpoTHREE, { Renderer, TextureLoader } from 'expo-three';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import * as React from 'react';
import {
AmbientLight,
HemisphereLight,
BoxBufferGeometry,
Fog,
GridHelper,
Mesh,
MeshStandardMaterial,
PerspectiveCamera,
PointLight,
Scene,
SpotLight,
Camera,
InstancedMesh,
} from 'three';
import OrbitControlsView from 'expo-three-orbit-controls';
Apart from the basic scene, camera and light setup I'm trying to load a glb model using the ExpoTHREE.loadAsync method as below...
const loadGlb = async ()=>{
const obj = await ExpoTHREE.loadAsync(
[require('./assets/suzanne.glb')],
null,
null}).
then((e)=>{
scene.add(e.scene);
e.scene.traverse((f)=>{if(f.isMesh){f.material = new THREE.MeshNormalMaterial();}});
})
.catch((err)=>{console.log(err)});
}
loadGlb();
Using the ref: https://www.npmjs.com/package/expo-three
The model loads when I run the code on the desktop browser but not on my android phone using expo app. Please let me know what am I doing wrong.
You can access the app here https://expo.io/#praful124/expo3
I had the same problem and ended up running into the fact that it does not support textures inside the glb file. But if you find a solution, I will be very happy if you share them.
The game freezes permanently when I try to load the next scene but when I load it with "Additive" mode it's loaded fine (using SceneManager.LoadScene("NextScene", LoadSceneMode.Additive)).
In editor everything works even with mode "Single", the problem only appears with Android.
I don't want to use the "Additive" mode because it loads the content of the new scene above the old one.
I try:
using "adb logcat" to see if any exception occurs but there are no exceptions.
load an empty scene as a new scene but the problem still appears.
load using SceneManager.LoadSceneAsync("NextScene") but the problem still appear.
load with "Additive" mode and after the next scene is loaded I try to Unload the old scene using SceneManager.UnloadScene("OldScene") but it freezes again.
So the problem appears when Unity try to unload the old scene.
(I use Unity 2018.4.10f1 LTS).
Thanks in advance for any help.
Try to delete the objects from the last scene
In for loop to see what object response for the error
I try to build the game with PC and I face the same problem.
I see what in the output_log.txt file in "C:/Users/.../AppData/Local/Temp/.../My game name/Crashes" and the first readable messages after the crash are like this:
"ERROR: SymGetSymFromAddr64, GetLastError: 'Attempt to access invalid address.' (Address: 00007FFEF20E5212)"
without any other errors or exceptions by my game,
so I try to test the game with an updated version of Unity (2018.4.10f1 --> 2018.4.20f1),
Abracadabra everything works fine with the new version!!
and the problem doesn't appear anymore!!
I think it was a memory management problem in Unity 2018.4.10f1 but I don't know why.
I am trying to build an Android app that uses Unity for UI rendering where the user can perform some actions from outside the scene.
What this means is that say I have an activity where I have a unity scene as a subview. The rest of the screen is composed of native android views (buttons etc). I want to use these views to make changes to the unity scene such as to show a text field, add text, hide text, add text effects etc.
So far I have added the unity scene as a subview by following this answer and it works fine. I am new to Unity and I am not sure how I can manage and change the scene from views outside the scene. Is this even possible? I know if I add buttons to the scene itself when I create the scene in Unity then I can use those buttons to alter the scene.
But is this possible by UI components that are not a part of the scene? If yes how can I do this? I have tried to find info on this but couldn't find much. Thanks for reading.
Make communicator between Unity side and Android.
It's possible to do that technically, if you make following function in unity. I've just wrote here how unity and android communicate so
before use this code you need to see brief of integration between Unity and Android to use native plugin from Unity so see this link to do that.
https://docs.unity3d.com/Manual/PluginsForAndroid.html
After you understand that how to make plugin for android, then see codes. It will help you.
GameObject Name: LoadGameSceneObject, loadScene.cs
public class loadScene : MonoBehaviour
{
public static void LoadScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
}
SceneLoadActivie.java
public static void SendMessageToUnity(String gameObjectName, String methodName, String message) throws Exception
{
Log.d("unity", gameObjectName + " " + methodName + " " + message);
final Class<?> player = Class.forName("com.unity3d.player.UnityPlayer");
player.getMethod("UnitySendMessage", String.class, String.class,
String.class).invoke(null, gameObjectName, methodName, message);
}
...
// call this function.
public void LoadScene()
{
SceneLoadActivie.SendMessageToUnity("LoadGameSceneObject", "LoadScene", "SomeScene");
}
I made a Activity using Titanium Mobile Module, and it has simple white background.
Can I pass a window (Titanium object) from this Activity to another js files to add (draw) new objects (like buttons) ?
Yes, you can.
If you are using Alloy for it, just do anything similar as
//index.js
function doClick(e) {
require('utils').createButton($.index);
}
And
//utils.js inside /lib
exports.createButton = function createButton(window){
var button = Ti.UI.createButton({
height:'20dp',
width:'20dp',
title:'Test'
});
window.add(button);
}
However, keep in mind, to your memory consumption, because you are sending a big object $.index as a local variable.
I have a game that is using GUITextures for buttons but it isn't working on different resoltutions. So I was told NGUI would do it very easily. But I already have the code written for the GUITexture buttons.
How do I do it for NGUI buttons using the same textures? I've searched everywhere and can not find any answers....the NGUI forums are not much help.
Your Gameobject should have any type of collider component then in script do the following:
//Use UIEventListener to bind your gameobject with your desired method
public GameObject YOUR_BUTTON_GAMEOBJECT;
UIEventListener.Get(YOUR_BUTTON_GAMEOBJECT).onClick += YOUR_METHOD_NAME;
//METHOD SIGNATURES
void YOUR_METHOD_NAME(GameObject gameObject){
//On Click Stuff
}
Hope this helps