c# - 在 Unity 中从 Android Studio 读取意图
问题描述
我有一个导出到 Android Studio 的 Unity 游戏。我有一个保存的游戏列表,其中存储了玩家玩过的每个游戏的最后一个场景。基本上存储玩家的进度。
从 Unity 播放到 Android Studio 的最后一个场景的编写效果很好。但是,我不知道如何在 Unity 中读取 Android Studio。我有一个 putExtra 意图,应该将场景索引传递给 Unity,但我不知道如何。我还应该提一下,这个 java 脚本是我写的唯一一个,这意味着我没有这里没有提到的其他方法。
我在网上找到了一个可以做到这一点的代码,但我不知道如何修改它,以便它可以与我的 Java 脚本一起使用。如果有人可以提供帮助,我将不胜感激!
我的 Java 脚本(在 Android Studio 中):
public class MainActivity extends AppCompatActivity {
private int lastscene = 99;
Button btnstart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//gets the PlayerPrefs from Unity
SharedPreferences sharedPreferences = getSharedPreferences(getPackageName() + ".v2.playerprefs", Context.MODE_PRIVATE);
//Saves the last played scene (which is stores in the PlayerPrefs we got) in "lastscene"
lastscene = sharedPreferences.getInt("Scene",99);
//The reading from Unity works! Toasts the last played scene when you open the Android Studio app
Toast.makeText(getApplicationContext(), ""+lastscene,
Toast.LENGTH_LONG).show();
btnstart = (Button) findViewById(R.id.btnstart);
//Once you press the button, Unity should open and start playing from the last scene that we saved into Android Studio.
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, UnityPlayerActivity.class);
i.putExtra("listScene", lastscene);
startActivity(i);
}
});
}
}
我在网上找到的 C#,不知道如何使它工作(Unity):
private void Awake ()
{
getIntentData ();
}
private bool getIntentData () {
#if (!UNITY_EDITOR && UNITY_ANDROID)
return CreatePushClass (new AndroidJavaClass ("com.unity3d.player.UnityPlayer"));
#endif
return false;
}
public bool CreatePushClass (AndroidJavaClass UnityPlayer) {
#if UNITY_ANDROID
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject> ("currentActivity");
AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject> ("getIntent");
AndroidJavaObject extras = GetExtras (intent);
if (extras != null) {
string ex = GetProperty (extras, "listScene");
return true;
}
#endif
return false;
}
private AndroidJavaObject GetExtras (AndroidJavaObject intent) {
AndroidJavaObject extras = null;
try {
extras = intent.Call<AndroidJavaObject> ("getExtras");
} catch (Exception e) {
Debug.Log (e.Message);
}
return extras;
}
private string GetProperty (AndroidJavaObject extras, string name) {
string s = string.Empty;
try {
s = extras.Call<string> ("getString", name);
} catch (Exception e) {
Debug.Log (e.Message);
}
return s;
}
感谢我从哪里获取 Unity 脚本:Credit
解决方案
推荐阅读
- rest - 使用 CoinMarketCap 根据名称过滤某些硬币
- c - 串行驱动程序和 tty 驱动程序之间的区别
- vb.net - 在 TableLayoutPanel 中使用 DrawString 截断文本
- windows - C2337 'importidl': 将解决方案升级到 VS2019 ( 16.9.2 ) 后找不到属性错误
- c# - 使用 JSON 模式,在将 JSON 解析为 JObject 时如何过滤掉其他属性?
- python - 如何将 iterrows 的行输出连接到另一个具有相同列的 pandas DataFrame?
- r - 在 R 中制作一个平铺图,但每列都有一个颜色键
- mysql - SQL - 使用共享索引连接多个表
- javascript -
如何指定每种产品的累积率 - python - 在 Pyomo 目标中应用不等式