No playerprefs in Widgets?

I can’t seem to get my game to use playerprefs as a widget… shouldn’t it work?

The game works fine stand alone.

What about web players? Doesnt seem to work in that either.

Am I missing something?

For web player content (Dashboard Widgets are a form of web player content, it’s browser-driven and requires the Unity Web Player) the PlayerPrefs data isn’t persistent between sessions. To have information persist you’ll have to make a call to some external JavaScript to store your preference information as cookies. Check out “CookieCutter” on the Wiki:

CookieCutter

We’ll update the docs to be a bit clearer about the behavior of PlayerPrefs in the various environments.

Thanks HiggyB… but this brings up a couple of questions…

a) It says that it wont work on local webplayers, so what does that mean for widgets?

b) Is there a way inside the unity runtime to detect if it is playing in a stand-alone player or webplayer? So I can swap between cookies and playerprefs?

I will try implementing the cookies when I get into work.
Thanks.

The PlayerPrefs class is limited in its use in any web player environment, including Dashboard Widgets. The limitation is that it will allow you to read/write preference information but that information will be flushed (erased) at the end of each session - the info is not persistent. Therefore if you’re content is running in a web player and you want your preferences data to be persistent between game play sessions then you’ll have to use browser cookies instead.

Yes, there sure is. You can do this by comparing Application.platform with the various RuntimePlatform values. For example:

if (Application.platform == RuntimePlatform.OSXDashboardPlayer) {
  // do some Widget specific code...
} else {
  // do some other code...
}

Have a look at the docs and you’ll see you can check for the content being run in any of the various run-time environments.

Sorry, I didnt make myself clear enough… the cookicutter wiki says that the cookies wont work in local webplayers in safari. That you have to be running a personal webserver. So what does that mean for Widgets?

EDIT: Don’t worry, let me try it out before I start worrying if it is going to work ok or not.

Thanks re the runtime checking… thats handy to know!

Yeah, my quick thoughts are these:

  • The Wiki doesn’t say all local web player files have problems, only those running in Safari (due to an apparent Safari limitation)

  • Dashboard Widgets do run within a special Safari window so they might be affected by this, but regardless Apple dealt with that by exposing special objects to JS. In this case the notable object is the widget object:

widget.setPreferenceForKey(preference, key);
widget.preferenceForKey(key);

The above was lifted from here. So if CookieCutter doesn’t work you can always try the above. What’s more is that you might be able call the above straight from your Unity code:

// set a prefs value
Application.ExternalEval("widget.setPreferenceForKey('PrefValue', 'PrefName');");

// get a prefs value
var tValue = Application.ExternalEval("widget.preferenceForKey('PrefName');");

NIIIIIIIIIIIIIIIIIIIIIICE!
Thanks HiggyB :slight_smile:

Not a problem, glad I could be of some use… 8)

Hello Tom

I am unable to make your code work.

// set a prefs value 
Application.ExternalEval("widget.setPreferenceForKey('PrefValue', 'PrefName');"); 

// get a prefs value 
var tValue = Application.ExternalEval("widget.preferenceForKey('PrefName');");

According to the Unity Reference, Application.ExternalEval returns a void value.

How is it possible to get a value from an external snippet then?

It also says that the Application.ExternalEval only works on Web Player.

Please help? I also need to save preferences from a Widget.

Regards
Andres

You can include (or inject) JavaScript in the Widget that calls back to your game with the result. It’s a bit more complicated to do this asynchronously, but quite doable.

d.

Hello David

After several trials I found that the widget.setPreferenceForKey and widget.preferenceForKey do work from a Widget. And I was able to save the preference but still unable to read it.

I tried the inject method you mention (which is not really necessary because the callback can be done in just one line of code) but I am still not able to make it work.

My guess is that the usual code (document.getElementById(‘XX’) or GetUnity() )
fails to recognize the Unity entity (or ID) to send the message back to Unity.

Any help is very welcome.

Regards,
Andres

I do stuff like this in the widget’s Javascript:

document.getElementById("UnityEmbed").SendMessage("SomeObject", "SomeFunction", data);

And then in the Unity plugin stuff, add

embed id="UnityEmbed"

because I don’t think it’s there by default. I use Application.ExternalCall from Unity to call a function in the widget, and do the widget.preferenceForKey stuff there, rather than use ExternalEval.

–Eric

Thanks Eric

Finally I see light at the end of the tunnel : )

Please tell me how to add the code in the Unity plugin.

Regards,
Andres

document.write('<embed id=“UnityEmbed” class=“button” src=“widget.unityweb” type=“application/x-unity”…

–Eric

Thanks again Eric

Please excuse me but I am not familiar on how to add that code.

Can you please be more specific?

What is the full code and where exactly should I place it?

Thank you
Andres

Just search in the html file for something unique from that line (like “widget.unityweb”). You want to add the embed id=“UnityEmbed” part. Of course, you can use whatever you want for the id. Just make sure it matches what you use with document.getElementById().

–Eric

Hello Eric

I am looking within the compiled widget package and opening the UnityWidget.html file, and there is not such thing.

Should I use the full function writeUnityTags from this page?..
http://unity3d.com/support/documentation/Manual/Publishing%20active%20content.html

Please help !!!

Andres

Never mind Eric

I finally found it.

Thanks very much for your help.

Andres

In effort to update PlayerPrefsx with widget prefssupport, I’ve spent much too long trying to get reading of existing widget prefs to work. Can anyone suggest a good code flow for setting a value to a stored widget pref value explicitly via the callback?

To say, as previously stated, someVar = ExternalCall (…); doesn’t work since there needs to be a callback, but from that point, no matter which approach I try, I always end up at a dead end. Any tips from those who have succeeded here?

What is the least painful code path to explicitly setting a var to a stored widget pref value even in vague-ish / high level terms?

Probably easiest just to wait a little while for 2.1 and bypass this whole thing instead.

–Eric