Playerprefs workaround

Am i doing the playerprefs work around right… (i have no obj-c knowledge)

Unity side:

function OnGUI () {
	if (GUI.Button (Rect (10,10,160,40), "Save")) { 
      PlayerPrefs.SetString("save", "y"); 
   }
}

xcode side:

- (void) CheckPlayerPrefs
{
	if ([@ "y" isEqualToString : save]) { 
		UnityPause(true);
		[[NSUserDefaults standardUserDefaults] setObject : @ "" forKey : @ "save"];
		// obj-c code goes here
		UnityPause(false);
}

I am not also not sure where to but the xcode side code… like in what function

thanks

Forgive my ignorance, but what exactly are you attempting to workaround? PlayerPrefs work as expected on the iPhone.

I am trying to make it so you can take a screenshot and save it to the users photo gallery… i should probably change the name of this thread to “OBJ-C workaround using player prefs”

see here:
http://forum.unity3d.com/viewtopic.php?t=20955

The code you have posted is equivalent to the code I am using to pause Unity and load a separate view created in Obj-C. The only thing you’ll want to do before the isEqualToString check is define the variable ‘save’ as follows:

NSString* save = [[NSUserDefaults standardUserDefaults] stringForKey:@ "save"];

As for where to put this code on the Xcode side, the easiest thing to do is to put it in the Repaint function in AppController.mm.

it doesnt work for me…

heres my code

	NSString* save = [[NSUserDefaults standardUserDefaults] stringForKey:@ "save"]; 
	if ([@ "y" isEqualToString : save]) { 
		UnityPause(true); 
		[[NSUserDefaults standardUserDefaults] setObject : @ "" forKey : @ "save"]; 
		printf_console("-> yay it worked!\n");
		UnityPause(false);

it says

warning: Unable to read symbols for "" (file not found).

i think its looking on my hard drive not on the iphone

what am i doing wrong?
i stuck it right before the end of repaint.

Hmm… your code is exactly the same as mine (I just have it at the beginning of the Repaint function instead of the end).

Where exactly is this warning appearing? Does the application build and run in Xcode despite this warning?

I don’t see anything in the code you’ve posted that should be causing any warnings or errors, provided you close that if block with a } before the end of the function.

sounds more like the problem is your usage from the script. You just don’t put a valid file name in to save (“” to be exact)

that or it triggers the safe attempt without beeing asked to

On the Xcode side you’d check the NSUserDefaults like this:

- (void) checkPlayerPrefs: (NSTimer *) timer
{
    if ([[NSUserDefaults standardUserDefaults] integerForKey: @"save"] == 1)
    {
        // save screen shot here

        // clear flag
        [[NSUserDefaults standardUserDefaults] setInteger: 0 forKey: @"save"];
    }
}

Then from applicationDidFinishLaunching you’d make a timer to periodically check for changes, e.g.

[NSTimer scheduledTimerWithTimeInterval: 1.0f
    target: self 
    selector: @selector(checkPlayerPrefs:) 
    userInfo: nil repeats: YES];

Then from your Unity script you’d do

PlayerPrefs.SetString("save", 1);

Zibba, thanks so much, this solved my problem too! Been searching for this for weeks!