No image tracking subsystem found when you call ARTrackedImageManager.CreateRuntimeLibrary - c#

I am trying to create a RuntimeReferenceImageLibrary and I have always this error:
NotSupportedException: No image tracking subsystem found. This usually
means image tracking is not supported.
UnityEngine.XR.ARFoundation.ARTrackedImageManager.CreateRuntimeLibrary
(UnityEngine.XR.ARSubsystems.XRReferenceImageLibrary
serializedLibrary) (at
Library/PackageCache/com.unity.xr.arfoundation#4.2.6/Runtime/AR/ARTrackedImageManager.cs:95)
Is there any way to change my subsystem and make it possible ?
I used the same project with the same XRReferenceImageLibrary and the ARTrackedImageManager and if you add the picture in the editor everything is working fine.
The problem is when you want to create a RuntimeReferenceImageLibrary for adding pictures in runtime.
//Definition
public XRReferenceImageLibrary serializedLibrary; //Set up in the editor
private ARTrackedImageManager trackedImageManager; //Set up in the awake function
I called this function in the Start function and also I tried to call it afterwards. Always I got the same result
private void TryOut()
{
if (trackedImageManager != null)
{
if (serializedLibrary != null)
{
if (trackedImageManager.enabled == true)
{
Debug.Log("Image manager and XR Reference found");
RuntimeReferenceImageLibrary runtimeLibrary = trackedImageManager.CreateRuntimeLibrary(serializedLibrary);
}
}
}
}

Related

Android problem changing app language programmatically

I've built an android application (using Xamarin c#) and it contains a Settings screen where users can change app language.
This language setting is saved also on the server by calling an API.
After changing the language I refresh texts into the settings screen and into other opened activities and all seems work.
The problem is when I close and reopen the app: I sign in the user and I read language data from the server.
I programmatically change the locale language of the app into my main activity: for example, I set a language different than the default smartphone language.
This activity contains 4 fragments into a ViewPager, but sometimes these fragments are in the "old" language and "new" language is not correctly applied.
So, when I open a new activity, the language shown is the "old" and not the "new".
Am I doing something wrong?
Can anyone help me?
Following my code:
In my AppBaseActivity class, OnCreate event:
base.OnCreate(bundle);
if (UserSettings != null
&& !string.IsNullOrWhiteSpace(UserSettings.LanguageCode))
{
LocaleManager.ChangeAppLanguage(this, UserSettings.LanguageCode);
}
In my AppBaseActivity class, OnResume event:
base.OnResume();
if (UserSettings != null
&& !string.IsNullOrWhiteSpace(UserSettings.LanguageCode))
{
LocaleManager.ChangeAppLanguage(this, UserSettings.LanguageCode);
}
My LocaleManager.ChangeAppLanguage method:
public static bool ChangeAppLanguage(Context context, String lang, bool saveInSharedPreferences)
{
Locale newLocale = new Locale(lang);
if (Locale.Default.Language != newLocale.Language)
{
Locale.Default = newLocale;
Android.Content.Res.Configuration config = new Android.Content.Res.Configuration();
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
config.SetLocale(newLocale);
}
else
{
config.Locale = newLocale;
}
context.Resources.UpdateConfiguration(config, context.Resources.DisplayMetrics);
if (saveInSharedPreferences)
return Helper.SaveInSharedPreferences(context, context.ApplicationContext.PackageName + ".languageCode", lang);
return true;
}
return false;
}
you should load the new Context in the AttachBaseContext method of your AppBaseActivity,you could refer to here

Question about PhotonNetwork.CurrentRoom.CustomProperties

When the game starts in multiplayer, the master client sends a PunRPC to have all clients run a function. This function tries to get the room properties to see if the game is active, if so it does something. For some reason a client gets a null reference error, but the master client does not. The strange thing is, a debug of the hash table for room properties is visible but I cannot get a specific item in it.
Tried Debugging the hash table to make sure that the key was set when the code was run. It was. "(System.String)ag=(System.Boolean)True ag=activeGame" This shows in Debug.Log(hash); But (bool)hash[rpk.activeGame] gets a null reference error. But only on the client side not the master client. So the key also works.
// Call all the clients to set up the room settings in the sub menu.
[PunRPC]
private void GameRoomSetup (string pOne, string pTwo, int pOneColor, int pTwoColor)
{
GameObject gameMenu = GameObject.Find ("GameMenu");
gameMenu.GetComponent<SubMenu> ().UpdatePlayers (pOne, pTwo, pOneColor, pTwoColor);
gameMenu.GetComponent<SubMenu> ().StartGameSetup ();
// If you are a player, change the active buttons that are visible.
if (PhotonNetwork.NickName == pOne || PhotonNetwork.NickName == pTwo)
{
gameMenu.GetComponent<GameButtonManager> ().GameStart ();
}
hash = PhotonNetwork.CurrentRoom.CustomProperties;
Debug.Log(hash);
if ((bool)hash[rpk.activeGame]) // Error on this line on client but not on master client. Says null reference.
{
GameObject.Find ("SoundManager").GetComponent<SoundManagerScript> ().PlayBackgroundTwo ();
GameObject.Find ("GameMenu").GetComponent<SubMenu> ().ChangeSubMenuActive (false);
}
}
I'm trying to run my if statement as a client but I get an error.
Thank you for choosing Photon!
To get a custom property, I recommend you use TryGetValue method as follows:
hash = PhotonNetwork.CurrentRoom.CustomProperties;
object temp;
string key = rpk.activeGame;
if (hash.TryGetValue(key, out temp))
{
if (temp is bool)
{
bool activeGame = (bool)temp;
}
else
{
// unexpected custom property value type
}
}
else
{
// custom property not found
}
If the custom property is not available yet, wait for the callback IInRoomCallbacks.OnRoomPropertiesUpdate(Hashtable propertiesThatChanged) (reference API).
Other notes and recommendations:
private void GameRoomSetup (string pOne, string pTwo, int pOneColor, int pTwoColor)
Not sure if it's supported or if it's a good idea to pass multiple parameters to a PUN RPC method.
To debug log a Dictionary or a Hashtable you could make use of SupportClass.DictionaryToString() method.
so instead of
Debug.Log(hash);
use
Debug.Log(SupportClass.DictionaryToString(hash));
Avoid calling expensive methods like GameObject.Find:
GameObject gameMenu = GameObject.Find ("GameMenu");
Also here you have duplicate calls to gameMenu.GetComponent<SubMenu>(), at least call it once and cache the component result found if any.
gameMenu.GetComponent<SubMenu> ().UpdatePlayers (pOne, pTwo, pOneColor, pTwoColor);
gameMenu.GetComponent<SubMenu> ().StartGameSetup ();
Comparing strings should not be done using == operator. At least use Equal method and proper StringComparison type. Read "How to compare strings in C#".
// If you are a player, change the active buttons that are visible.
if (PhotonNetwork.NickName == pOne || PhotonNetwork.NickName == pTwo)
{
gameMenu.GetComponent<GameButtonManager> ().GameStart ();
}
Besides, why do you use the Nickname to check if it's player one or two? maybe use ActorNumber or a custom player index. Or use the player count if the room is for 2 players only.

System.InvalidOperationException: 'Cannot assign a native control without an Element;

I am working on a project "CoManga" and I wanted to add advertisements in it. Implementing ads on UWP seemed straight forward, like Android and iOS. However, I'm stuck now.
Anyways, I followed this tutorial by James Montemagno and added everything. I even see the test advertisements, which is great. However, when I try to move away from that page (when I press "BACK Button") and go to previous page, I get an error.
This is the error :
Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.'.
It is thrown at line number 50, where I set the SetNativeControl(adView);. I've commented it out right now, but as soon as I un-comment it, I see this error.
Can someone help me out here with this.
Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.
The reason is that xamarin Element has released but SetNativeControl invoked again cause the native control can't find the matched xamarin Element when page going back. So you could set a flag (isRegist) to record the registed ad.
public class AdViewRenderer : ViewRenderer<AdControlView, AdControl>
{
string bannerId = "test";
AdControl adView;
string applicationID = "3f83fe91-d6be-434d-a0ae-7351c5a997f1";
bool isRegist = false;
protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)
{
base.OnElementChanged(e);
if (Control == null && isRegist != true)
{
CreateNativeAdControl();
SetNativeControl(adView);
isRegist = true;
}
}
private void CreateNativeAdControl()
{
if (adView != null)
return;
var width = 300;
var height = 50;
if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop")
{
width = 728;
height = 90;
}
// Setup your BannerView, review AdSizeCons class for more Ad sizes.
adView = new AdControl
{
ApplicationId = applicationID,
AdUnitId = bannerId,
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom,
Height = height,
Width = width
};
}
}

Unity3D C# - NullReferenceException although Text is attached and methods work

have no idea what is going wrong. There is a problem by outsourcing some code in another class. If the following code is in the same class, it works fine.
SearchClient.cs
void callExposeAPi (string id)
{
ExposeClient exposeClient = (new GameObject("ExposeClient")).AddComponent<ExposeClient>();
exposeClient.loadExpose(id);
}
ExposeClient.cs
public Text _baserentText; // is attached to Text in Unity
public void loadExpose(string id)
{
[some API stuff...]
Debug.Log(result.exposeexpose.realEstate.baseRent); // 480
makeUseOfExposeUI(result.exposeexpose.realEstate);
}
void makeUseOfExposeUI (Realestate realestate)
{
Debug.Log(realestate.baseRent); // 480
_baserentText.text = realestate.baseRent.ToString();
}
I see what is happening, in your callExposeAPi method you are creating a new instance of ExposeClient and then by looking at your comments in ExposeClient.cs you mentioned "is attached to Text in Unity" well when you assigned variables through the editor the association of _baserentText occurs with the object you manually associated through the editor, if you dynamically create an instance this association would have to be done in a different way or you could do something like this:
void callExposeAPi (string id)
{
ExposeClient exposeClient = GameObject.Find("ExposeClient").GetComponent<ExposeClient>();
exposeClient.loadExpose(id);
}
The difference here is that you are using a game object which already contains an ExposeClient script attached and its serialized field "_baserentText" has already been defined which will not throw a null exception.

Checking current scene name using SceneManager

So I've decided to split my game into separate scenes(main menu, gameover, etc.) And I'm trying to use an if statement in my code to check if the current scene is "gameover" so I can perform some action. I know Unity now uses SceneManager instead of Application, but what's the equivalent of this function
if(Application.loadedLevelName == "gameover")
I've tried SceneManager.GetActiveScene == "gameover"
But I just get errors like can't use == here.
I have imported SceneManager aswell
You're getting an error here because SceneManager.GetActiveScene() returns an object of type SceneManager.Scene, not a string. However, according to the documentation, this gives you access to the public Scene.name, which is a string.
So the non-deprecated equivalent of:
if (Application.loadedLevelName == "gameover") {
// ...
}
Would be:
if (SceneManager.GetActiveScene().name == "gameover") {
// ...
}

Categories