public Transform cameraTransform;void Start(){}public void SaveCameraPosition(string viewname){if (!string.IsNullOrEmpty(viewname)){CameraView cameraView = new CameraView();cameraView.name = viewname;cameraView.position = cameraTransform.position.ToString();cameraView.rotation = cameraTransform.rotation.eulerAngles.ToString();string oko = JsonUtility.ToJson(cameraView);Application.ExternalCall("ReciveViwe", oko.ToString()) ;}}public void ParsingCameraViewdata(string json){CameraView jsonData = JsonMapper.ToObject<CameraView>(json);Debug.Log("pos:"+jsonData.position+"rot:"+ jsonData.rotation);Vector3 po = StringToVector3(jsonData.position);Vector3 ro = StringToVector3(jsonData.rotation);MaxCamera.instance_.ModelMove(po, Quaternion.Euler(ro.x, ro.y, 0), 0.5f);}public Vector3 StringToVector3(string sVector){if (sVector.StartsWith("(") && sVector.EndsWith(")")){sVector = sVector.Substring(1, sVector.Length - 2);}string[] sArray = sVector.Split(',');return new Vector3(float.Parse(sArray[0]),float.Parse(sArray[1]),float.Parse(sArray[2]));}public class CameraView{public string name;public string position;public string rotation;}
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class CameraSaveLoad : MonoSingleton<CameraSaveLoad>
{public Transform cameraTransform; public string saveFileName = "camera_view.json"; public InputField viewNameInput; public Button surebtn;public Dictionary<string, CameraView> CamerViewDic = new Dictionary<string, CameraView>();void Start(){LoadDeviceDic();surebtn.onClick.AddListener(SaveCameraPosition);}public class CameraView{public string name;public string position;public string rotation;}public Vector3 StringToVector3(string sVector){if (sVector.StartsWith("(") && sVector.EndsWith(")")){sVector = sVector.Substring(1, sVector.Length - 2);}string[] sArray = sVector.Split(',');return new Vector3(float.Parse(sArray[0]),float.Parse(sArray[1]),float.Parse(sArray[2]));}public void SaveCameraPosition(){string viewName = viewNameInput.text;if (!string.IsNullOrEmpty(viewName)){CameraView cameraView = new CameraView{name = viewName,position = cameraTransform.position.ToString(),rotation = cameraTransform.rotation.eulerAngles.ToString()};AddCameratrToDic(viewName, cameraView);}}
public void AddCameratrToDic(string viewname, CameraView viewdate){if (!CamerViewDic.ContainsKey(viewname)){CamerViewDic.Add(viewname, viewdate);SaveCamerViewDicTojson(CamerViewDic);}else{CamerViewDic[viewname] = viewdate;SaveCamerViewDicTojson(CamerViewDic);}}public void Delel(){string filePath = Path.Combine(Application.persistentDataPath, "camera_view.json");if (File.Exists(filePath)){File.Delete(filePath);Debug.Log("文件已删除:" + filePath);}}public void SaveCamerViewDicTojson(Dictionary<string, CameraView> valuePairs){JsonData jsonData = JsonMapper.ToJson(valuePairs);Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");var ss = reg.Replace(jsonData.ToString(), delegate (Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });string filePath = Path.Combine(Application.streamingAssetsPath, "camera_view.json");File.WriteAllText(filePath, jsonData.ToString());Debug.Log("Dictionary data saved to: " + filePath);}public void LoadDeviceDic(){string filePath = Path.Combine(Application.streamingAssetsPath, "camera_view.json");if (File.Exists(filePath)){string json = File.ReadAllText(filePath);CamerViewDic = JsonMapper.ToObject<Dictionary<string, CameraView>>(json);Debug.Log("Dictionary data loaded from: " + filePath);foreach (var pair in CamerViewDic){Vector3 po = StringToVector3(pair.Value.position);Debug.Log("Key: " + pair.Key + ", Value: " + pair.Value + ", " + pair.Value);}}else{Debug.LogError("Dictionary data file not found!");}}}