OutPointManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using System.Text;
  6. using System;
  7. public class OutPointManager : MonoBehaviour
  8. {
  9. private static OutPointManager instance;
  10. void Awake()
  11. {
  12. if (instance == null)
  13. {
  14. instance = this;
  15. }
  16. }
  17. public static OutPointManager GetInstance()
  18. {
  19. return instance;
  20. }
  21. [Tooltip("导出文件名字")]
  22. public string jsonName;
  23. public StringBuilder SB = new StringBuilder();
  24. public void StartWriteFile()
  25. {
  26. this.WriteSceneCom();
  27. //ADD Here
  28. SB.Append("}");
  29. var name = "E:/Work/ShootOut3D/Shootout/ShootLaya/bin/res/LevelCfg" + "/" + jsonName + ".json";
  30. WriterFile(name, SB.ToString());
  31. }
  32. //写入场景组件
  33. private void WriteSceneCom()
  34. {
  35. SB.Append("{\"sceneCom\":[");
  36. var goArr = GameController.GetInstance().sceneEnity;
  37. for (int i = 0; i < goArr.Length; i++)
  38. {
  39. var go = goArr[i];
  40. SB.Append("{\"name\":\"" + go.name+"\",");
  41. SB.Append("\"pos\":");
  42. outVector3(SB, go.transform.position);
  43. SB.Append(",");
  44. SB.Append("\"scale\":");
  45. outVector3ForScale(SB, go.transform.localScale);
  46. SB.Append(",");
  47. SB.Append("\"rotation\":");
  48. outQuaterion(SB, go.transform.rotation);
  49. if (i + 1 == goArr.Length)
  50. {
  51. SB.Append("}");
  52. }
  53. else
  54. {
  55. SB.Append("},");
  56. }
  57. }
  58. SB.Append("]");
  59. }
  60. public static void WriterFile(string path, string info)
  61. {
  62. if (File.Exists(path))
  63. {
  64. File.Delete(path);
  65. }
  66. FileStream fs = File.Create(path);
  67. if (fs != null)
  68. {
  69. StreamWriter sw = new StreamWriter(fs);
  70. if (sw != null)
  71. {
  72. sw.Write(info);
  73. sw.Flush();
  74. }
  75. fs.Flush();
  76. sw.Close();
  77. fs.Close();
  78. }
  79. }
  80. static void outVector3(StringBuilder SB, Vector3 p)
  81. {
  82. SB.Append("[");
  83. SB.Append(-p.x);
  84. SB.Append(",");
  85. SB.Append(p.y);
  86. SB.Append(",");
  87. SB.Append(p.z);
  88. SB.Append("]");
  89. }
  90. static void outVector3ForScale(StringBuilder SB, Vector3 p)
  91. {
  92. SB.Append("[");
  93. SB.Append(p.x);
  94. SB.Append(",");
  95. SB.Append(p.y);
  96. SB.Append(",");
  97. SB.Append(p.z);
  98. SB.Append("]");
  99. }
  100. static void outQuaterion(StringBuilder SB,Quaternion q)
  101. {
  102. SB.Append("[");
  103. SB.Append(-q.x);
  104. SB.Append(",");
  105. SB.Append(q.y);
  106. SB.Append(",");
  107. SB.Append(q.z);
  108. SB.Append(",");
  109. SB.Append(-q.w);
  110. SB.Append("]");
  111. }
  112. }