/Assets/Editor/EasyRoads3D/NewEasyRoads3D.cs

https://bitbucket.org/vhsousa/edj-asterix-obelix-roman-defense · C# · 155 lines · 130 code · 25 blank · 0 comment · 19 complexity · 413c2e38c908c38ca8d9c35879fc291f MD5 · raw file

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System.IO;
  5. using System;
  6. public class NewEasyRoads3D : EditorWindow
  7. {
  8. private static NewEasyRoads3D instance;
  9. private Vector3 scroll;
  10. public GUISkin cGS;
  11. public GUISkin dGS;
  12. private string objectname = "RoadObject01";
  13. private bool closedTrack;
  14. public NewEasyRoads3D()
  15. {
  16. objectname = GetNewRoadName();
  17. if( instance != null ){
  18. DestroyImmediate(this);
  19. return;
  20. }
  21. instance = this;
  22. title = "New EasyRoads3D Object";
  23. position = new Rect((Screen.width - 300.0f) / 2.0f, (Screen.height - 150.0f) / 2.0f, 300.0f, 150.0f);
  24. minSize = new Vector2(300.0f, 150.0f);
  25. maxSize = new Vector2(300.0f, 150.0f);
  26. }
  27. public void OnDestroy(){
  28. instance = null;
  29. }
  30. public static NewEasyRoads3D Instance{
  31. get
  32. {
  33. if( instance == null ){
  34. new NewEasyRoads3D();
  35. }
  36. return instance;
  37. }
  38. }
  39. public void OnGUI()
  40. {
  41. if(cGS == null){
  42. dGS = GUI.skin;
  43. cGS = (GUISkin)Resources.Load("EasyRoads3D/ER3DSkin", typeof(GUISkin));
  44. }
  45. GUI.skin = cGS;
  46. GUI.Box(new Rect (5, 20, 282, 70),"", "box");
  47. GUI.skin = dGS;
  48. GUILayout.BeginArea (new Rect (5, 5, 286, 250));
  49. GUILayout.Label("Set a name for the new EasyRoads3D Road Object");
  50. EditorGUILayout.Space();
  51. GUILayout.BeginArea (new Rect (50, 40, 200, 150));
  52. EditorGUILayout.BeginHorizontal();
  53. GUILayout.Label("Object name",GUILayout.Width(75));
  54. objectname = GUILayout.TextField(objectname,GUILayout.Width(125));
  55. EditorGUILayout.EndHorizontal();
  56. GUILayout.EndArea();
  57. EditorGUILayout.Space();
  58. EditorGUILayout.Space();
  59. EditorGUILayout.Space();
  60. EditorGUILayout.Space();
  61. EditorGUILayout.Space();
  62. EditorGUILayout.Space();
  63. EditorGUILayout.BeginHorizontal();
  64. GUILayout.FlexibleSpace();
  65. if(GUILayout.Button ("Create Object", GUILayout.Width(125))){
  66. if(objectname == ""){
  67. EditorUtility.DisplayDialog("Alert", "Please fill out a name for the new road object!", "Close");
  68. }else{
  69. bool flag = false;
  70. string[] dirs = Directory.GetDirectories(Directory.GetCurrentDirectory() + "/EasyRoads3D");
  71. foreach(string nm in dirs){
  72. string[] words = nm.Split('\\');
  73. words = words[words.Length - 1].Split('/');
  74. string nm1 = words[words.Length - 1];
  75. if(nm1.ToUpper() == objectname.ToUpper()){
  76. EditorUtility.DisplayDialog("Alert", "An EasyRoads3D object with the name '"+objectname+"' already exists!\r\n\r\nPlease use an unique name!", "Close");
  77. flag = true;
  78. break;
  79. }
  80. }
  81. if(!flag){
  82. GameObject go = (GameObject)MonoBehaviour.Instantiate(Resources.Load("EasyRoads3D/EasyRoad3DObject", typeof(GameObject)));
  83. instance.Close();
  84. go.name = objectname;
  85. go.transform.position = Vector3.zero;
  86. RoadObjectScript script = go.GetComponent<RoadObjectScript>();
  87. script.closedTrack = false;
  88. script.autoUpdate = true;
  89. script.surrounding = 15.0f;
  90. script.indent = 3.0f;
  91. script.geoResolution = 5.0f;
  92. Selection.activeGameObject = go;
  93. }
  94. }
  95. }
  96. EditorGUILayout.EndHorizontal();
  97. GUILayout.EndArea();
  98. }
  99. public string GetNewRoadName(){
  100. string path = Directory.GetCurrentDirectory() + "/EasyRoads3D";
  101. if( !Directory.Exists(path)){
  102. try{
  103. Directory.CreateDirectory( path);
  104. }
  105. catch(System.Exception e){
  106. Debug.Log("Could not create directory: " + path + " " + e);
  107. return "";
  108. }
  109. }
  110. string[] dirs = Directory.GetDirectories(@Directory.GetCurrentDirectory() + "/EasyRoads3D");
  111. int c = 0;
  112. int num;
  113. foreach(string nm in dirs){
  114. string[] words = nm.Split('\\');
  115. words = words[words.Length - 1].Split('/');
  116. string nm1 = words[words.Length - 1];
  117. if(nm.IndexOf("RoadObject") != -1){
  118. string str = nm1.Replace("RoadObject","");
  119. if(int.TryParse(str, out num)){
  120. if(num > c) c = num;
  121. }
  122. }
  123. }
  124. c++;
  125. string n;
  126. if(c < 10) n = "RoadObject0" + c.ToString();
  127. else n = "RoadObject" + c.ToString();
  128. return n;
  129. }
  130. }