Hi,
I would like to connect my facebook project to Unity via a plugin. The project in Eclipse works fine, without Unity, there are no issues. But in Unity, there is a warning and the app crashes :
In Player Settings in Unity, the orientation is in “portrait”. In the manifest also :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.firstandroidapp">
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
android:screenOrientation="portrait" >
<activity
android:name="com.firstandroidapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:value="@string/app_id" android:name="com.facebook.sdk.ApplicationId"/>
<activity android:name="com.facebook.LoginActivity"></activity>
</application>
</manifest>
And the code from Eclipse : I pasted the “res” folder under Plugins > Android to avoid any errors with the R class, and added getIdentifier in the mainActivity : (but here, I only call testFunc() from unity ) :
public class MainActivity extends UnityPlayerActivity {
public static Context ctx;
private LoginButton loginButton;
private ProfilePictureView profilePictureView;
private TextView greeting;
private GraphUser user;
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
public void testFunc(){
Log.i("test func","test func");
}
public String getStr(String n) {
return getString(getResources().getIdentifier( n, "string", getPackageName()));
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
int login_int = getResources().getIdentifier("login", "id", getPackageName());
loginButton = (LoginButton) findViewById(login_int);
loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
MainActivity.this.user = user;
updateUI();
}
});
int greeting_int = getResources().getIdentifier("greeting", "id", getPackageName());
greeting = (TextView) findViewById(greeting_int);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
updateUI();
}
private void updateUI() {
Session session = Session.getActiveSession();
boolean enableButtons = (session != null session.isOpened());
if (enableButtons user != null) {
greeting.setText("hello : "+user.getFirstName());
} else {
greeting.setText(null);
}
}
@Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
updateUI();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data, dialogCallback);
}
private FacebookDialog.Callback dialogCallback = new FacebookDialog.Callback() {
@Override
public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
Log.d("HelloFacebook", String.format("Error: %s", error.toString()));
}
@Override
public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
Log.d("HelloFacebook", "Success!");
}
};
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
and the code in Unity :
if(GUI.Button(new Rect(20,250,150,150), "TEST")) {
Debug.Log("hello facebook");
using (AndroidJavaClass javaClass = new AndroidJavaClass("com.firstandroidapp.MainActivity"))
{
using (AndroidJavaObject activity = javaClass.GetStatic<AndroidJavaObject>("ctx"))
{
activity.Call("testFunc");
}
}
}
I know a plugin for facebook exists, but I had too many errors I could not fix, so I would like to try to create my own plugin (I only look for simple actions, such as connect and post a status).
Thanks for your help