133 lines
4.6 KiB
C#
133 lines
4.6 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
using Newtonsoft.Json;
|
|
using System.Threading.Tasks;
|
|
using static DeviceQuery;
|
|
using System.Xml.Serialization;
|
|
using UnityEditor;
|
|
using Unity.VisualScripting;
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
public DeviceQuery.Root root;
|
|
public List<GameObject> gameObjects;
|
|
private void OnEnable()
|
|
{
|
|
Debug.Log(transform.Find("22 (2)").childCount);
|
|
GameObject go = GameObject.Instantiate(Resources.Load<GameObject>("23 (1)"));
|
|
//go.name = go.name.Replace("(Clone)", "");
|
|
//var deviceQuery = go.AddComponent<DeviceQuery>();
|
|
//deviceQuery.deviceList = root.data[0];
|
|
go.transform.SetParent(gameObjects[0].transform);
|
|
Renderer renderer =go.GetComponentInChildren<Renderer>();
|
|
Vector3 center = renderer.bounds.center;
|
|
go.transform.position = gameObjects[0].transform.position;
|
|
go.transform.rotation = gameObjects[0].transform.rotation;
|
|
//go.transform.position = new Vector3(go.transform.position.x, go.transform.position.y- center.y, go.transform.position.z);
|
|
}
|
|
// 将 PlayerPrefs 数据保存为 JSON 文件
|
|
public static void SavePlayerPrefsToJson(string 文件路径)
|
|
{
|
|
Dictionary<string, object> playerPrefsData = new Dictionary<string, object>();
|
|
|
|
// 遍历所有可能的键,将存在的值存储在字典中
|
|
foreach (string key in GetAllPossibleKeys())
|
|
{
|
|
if (PlayerPrefs.HasKey(key))
|
|
{
|
|
if (PlayerPrefs.GetInt(key) == 1)
|
|
playerPrefsData.Add(key, PlayerPrefs.GetInt(key));
|
|
else if (PlayerPrefs.GetFloat(key) != 0f)
|
|
playerPrefsData.Add(key, PlayerPrefs.GetFloat(key));
|
|
else
|
|
playerPrefsData.Add(key, PlayerPrefs.GetString(key));
|
|
}
|
|
}
|
|
|
|
// 将字典转换为 JSON 字符串
|
|
string jsonData = JsonUtility.ToJson(playerPrefsData, true);
|
|
|
|
// 使用 Application.persistentDataPath 保存 JSON 数据到指定文件路径
|
|
string 完整路径 = Application.persistentDataPath + "/" + 文件路径;
|
|
PlayerPrefs.SetString(文件路径, jsonData);
|
|
}
|
|
|
|
// 获取所有可能的 PlayerPrefs 键
|
|
private static string[] GetAllPossibleKeys()
|
|
{
|
|
List<string> keys = new List<string>();
|
|
|
|
// 在这里添加所有可能的键,例如:
|
|
keys.Add("HighScore");
|
|
keys.Add("PlayerName");
|
|
// ...
|
|
|
|
return keys.ToArray();
|
|
}
|
|
|
|
// 从 JSON 文件中读取数据并设置到 PlayerPrefs
|
|
public static void LoadPlayerPrefsFromJson(string 文件路径)
|
|
{
|
|
// 检查 PlayerPrefs 中是否存在 JSON 数据
|
|
if (PlayerPrefs.HasKey(文件路径))
|
|
{
|
|
// 从 PlayerPrefs 中获取 JSON 数据
|
|
string jsonData = PlayerPrefs.GetString(文件路径);
|
|
|
|
// 将 JSON 字符串转换为字典
|
|
Dictionary<string, object> playerPrefsData = JsonUtility.FromJson<Dictionary<string, object>>(jsonData);
|
|
|
|
// 将字典中的值设置到 PlayerPrefs
|
|
foreach (var kvp in playerPrefsData)
|
|
{
|
|
string key = kvp.Key;
|
|
object value = kvp.Value;
|
|
|
|
if (value is int intValue)
|
|
PlayerPrefs.SetInt(key, intValue);
|
|
else if (value is float floatValue)
|
|
PlayerPrefs.SetFloat(key, floatValue);
|
|
else if (value is string stringValue)
|
|
PlayerPrefs.SetString(key, stringValue);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("在 PlayerPrefs 中未找到指定文件的 JSON 数据:" + 文件路径);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询所有设备
|
|
/// </summary>
|
|
/// <param name="token"></param>
|
|
/// <returns></returns>
|
|
public async Task<DeviceQuery.Root> initAsync(string token)
|
|
{
|
|
var jsonResult = await CombineJSON.GetJson_POST("https://jsonplaceholder.typicode.com/posts", token);
|
|
|
|
root = JsonConvert.DeserializeObject<DeviceQuery.Root>(jsonResult);
|
|
|
|
return root;
|
|
}
|
|
|
|
[MenuItem("我的菜单/从选定对象创建预制")]
|
|
private static void CreatePrefabFromSelectedObject()
|
|
{
|
|
GameObject selectedObject = Selection.activeGameObject;
|
|
if (selectedObject != null)
|
|
{
|
|
if (PrefabUtility.IsPartOfPrefabAsset(selectedObject))
|
|
{
|
|
Debug.Log("Selected object is already a prefab asset.");
|
|
return;
|
|
}
|
|
|
|
string prefabPath = "Assets/Resources/NewPrefab.prefab";
|
|
GameObject prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(selectedObject, prefabPath, InteractionMode.UserAction);
|
|
Debug.Log("Prefab created: " + prefabPath);
|
|
}
|
|
}
|
|
}
|