55 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			55 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using System.Globalization;
 | |
| using UnityEngine;
 | |
| 
 | |
| namespace WZ
 | |
| {
 | |
|     public static class DataUtils
 | |
|     {
 | |
|         [System.Serializable]
 | |
|         private class Wrapper<T>
 | |
|         {
 | |
|             public T[] items;
 | |
|         }
 | |
| 
 | |
|         public static int AndroidVersionCode()
 | |
|         {
 | |
|             AndroidJavaClass contextCls = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 | |
|             AndroidJavaObject context = contextCls.GetStatic<AndroidJavaObject>("currentActivity");
 | |
|             AndroidJavaObject packageMngr = context.Call<AndroidJavaObject>("getPackageManager");
 | |
|             string packageName = context.Call<string>("getPackageName");
 | |
|             AndroidJavaObject packageInfo =
 | |
|                 packageMngr.Call<AndroidJavaObject>("getPackageInfo", packageName, 0);
 | |
|             return packageInfo.Get<int>("versionCode");
 | |
|         }
 | |
|         public static T[] FromJsonArray<T>(string json)
 | |
|         {
 | |
|             string wrappedJson = $"{{\"items\":{json}}}";
 | |
|             Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(wrappedJson);
 | |
|             return wrapper.items;
 | |
|         }
 | |
| 
 | |
|         public static double StringToDouble(string str)
 | |
|         {
 | |
|             double result = 0;
 | |
|             if (double.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
 | |
|             {
 | |
|                 Debug.Log("转换成功: " + result);
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 Debug.Log("转换失败:字符串格式不正确");
 | |
|             }
 | |
|             return result;
 | |
|         }
 | |
|         
 | |
|         public static void AddIfNotExists<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
 | |
|         {
 | |
|             if (!dictionary.ContainsKey(key))
 | |
|                 dictionary.Add(key, value);
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 |