4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unity スクリプトで Android のテーマを動的に指定する

Posted at

AndroidManifest.xml や、Android の native plugin でやっても良いが、Unity からも指定できるよというメモ。
以下、android.support.v7.appcompat やカスタムテーマを指定する前提。

#if UNITY_ANDROID
  AndroidJavaObject activity = new AndroidJavaClass ("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
  AndroidJavaObject resource = activity.Call<AndroidJavaObject>("getResources");
  string packageName = activity.Call<string>("getPackageName");
  int themeid = resource.Call<int>("getIdentifier", "Theme.AppCompat.Light", "style", packageName);
  activity.Call("setTheme", themeid);
#endif

補足

Unity からは、 R.style.Theme_AppCompat_Light をそのまま指定できないので、リソースID を取得して指定している。
Java だとこう。

getResources().getIdentifier("Theme.AppCompat.Light", "style", getPackageName());

ref: http://qiita.com/t-kashima/items/9462af782fb5f1a2a7da

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?