DiamondArrowSetup.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEditor.SceneManagement;
  7. using UnityEngine;
  8. using UnityEngine.SceneManagement;
  9. namespace DiamondArrow.Editor
  10. {
  11. public static class DiamondArrowSetup
  12. {
  13. public const string Root="Assets/DiamondArrow/";
  14. [MenuItem("Diamond Arrow/Create or refresh standalone scene")]
  15. public static void CreateScene()
  16. {
  17. if(EditorApplication.isPlaying)throw new InvalidOperationException("Exit Play Mode before creating the scene.");
  18. var icons=Import(Root+"Art/UI/NeonIcons.png",false);Import(Root+"Art/UI/NeonLogo.png",true);
  19. var theme=AssetDatabase.LoadAssetAtPath<NeonTheme>(Root+"Data/NeonTheme.asset");if(theme==null){theme=ScriptableObject.CreateInstance<NeonTheme>();AssetDatabase.CreateAsset(theme,Root+"Data/NeonTheme.asset");}
  20. theme.icons=icons;theme.logo=AssetDatabase.LoadAssetAtPath<Sprite>(Root+"Art/UI/NeonLogo.png");theme.font=Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");EditorUtility.SetDirty(theme);UpgradeVisuals();
  21. var previous=SceneManager.GetActiveScene();
  22. if(string.IsNullOrEmpty(previous.path)&&previous.isDirty)throw new InvalidOperationException("Save your modified untitled scene before creating the standalone scene.");
  23. var scene=EditorSceneManager.NewScene(NewSceneSetup.EmptyScene,string.IsNullOrEmpty(previous.path)?NewSceneMode.Single:NewSceneMode.Additive);SceneManager.SetActiveScene(scene);
  24. var cameraObject=new GameObject("DiamondArrowCamera",typeof(Camera),typeof(AudioListener));cameraObject.tag="MainCamera";cameraObject.transform.position=new Vector3(0,0,-20);var camera=cameraObject.GetComponent<Camera>();camera.orthographic=true;camera.orthographicSize=6.67f;camera.clearFlags=CameraClearFlags.SolidColor;camera.backgroundColor=new Color(.025f,0,.05f);camera.nearClipPlane=.1f;camera.farClipPlane=100;
  25. var lightObject=new GameObject("DiamondArrowLight",typeof(Light));lightObject.GetComponent<Light>().type=LightType.Directional;lightObject.GetComponent<Light>().intensity=.7f;
  26. var go=new GameObject("DiamondArrowGame");var game=go.AddComponent<DiamondArrowGame>();game.theme=theme;game.levelData=AssetDatabase.LoadAssetAtPath<TextAsset>(Root+"Data/Levels.json");
  27. PrefabUtility.SaveAsPrefabAssetAndConnect(go,Root+"Prefabs/DiamondArrowGame.prefab",InteractionMode.AutomatedAction);
  28. EditorSceneManager.SaveScene(scene,Root+"Scenes/DiamondArrow.unity");
  29. if(previous.IsValid()&&previous.isLoaded&&!previous.isDirty)EditorSceneManager.CloseScene(previous,true);
  30. AssetDatabase.SaveAssets();SetResolution(750,1334);Selection.activeObject=go;
  31. Debug.Log("Diamond Arrow: independent scene created. Open Assets/DiamondArrow/Scenes/DiamondArrow.unity and press Play.");
  32. }
  33. static Texture2D Import(string path,bool sprite)
  34. {
  35. AssetDatabase.ImportAsset(path,ImportAssetOptions.ForceSynchronousImport);var imp=(TextureImporter)AssetImporter.GetAtPath(path);imp.textureType=sprite?TextureImporterType.Sprite:TextureImporterType.Default;imp.spriteImportMode=SpriteImportMode.Single;imp.alphaIsTransparency=true;imp.mipmapEnabled=false;imp.textureCompression=TextureImporterCompression.Uncompressed;imp.maxTextureSize=4096;imp.npotScale=TextureImporterNPOTScale.None;imp.filterMode=FilterMode.Bilinear;imp.wrapMode=TextureWrapMode.Clamp;imp.SaveAndReimport();return AssetDatabase.LoadAssetAtPath<Texture2D>(path);
  36. }
  37. [MenuItem("Diamond Arrow/Refresh neon materials")]
  38. public static void UpgradeVisuals()
  39. {
  40. var shader=Shader.Find("DiamondArrow/Neon Surface");if(shader==null)throw new InvalidOperationException("Neon Surface shader has not imported.");
  41. string path=Root+"Art/Materials/NeonSurface.mat";var material=AssetDatabase.LoadAssetAtPath<Material>(path);
  42. if(material==null){material=new Material(shader);AssetDatabase.CreateAsset(material,path);}else material.shader=shader;
  43. var theme=AssetDatabase.LoadAssetAtPath<NeonTheme>(Root+"Data/NeonTheme.asset");theme.surfaceMaterial=material;
  44. if(File.Exists(Root+"Art/UI/NeonClose.png")){Import(Root+"Art/UI/NeonClose.png",true);theme.closeButton=AssetDatabase.LoadAssetAtPath<Sprite>(Root+"Art/UI/NeonClose.png");}
  45. AvatarSetup.Refresh();
  46. AudioSetup.Refresh();
  47. EditorUtility.SetDirty(theme);EditorUtility.SetDirty(material);AssetDatabase.SaveAssets();
  48. }
  49. [MenuItem("Diamond Arrow/Game View/750 x 1334")]
  50. public static void ReferenceResolution()=>SetResolution(750,1334);
  51. [MenuItem("Diamond Arrow/Game View/1170 x 2532")]
  52. public static void TallResolution()=>SetResolution(1170,2532);
  53. public static void SetResolution(int width,int height)
  54. {
  55. var assembly=typeof(UnityEditor.Editor).Assembly;var sizesType=assembly.GetType("UnityEditor.GameViewSizes");var singleton=typeof(ScriptableSingleton<>).MakeGenericType(sizesType);var sizes=singleton.GetProperty("instance",BindingFlags.Public|BindingFlags.Static).GetValue(null);
  56. var group=sizesType.GetMethod("GetGroup").Invoke(sizes,new object[]{(int)GameViewSizeGroupType.iOS});
  57. var groupType=group.GetType();int built=(int)groupType.GetMethod("GetBuiltinCount").Invoke(group,null);int count=built+(int)groupType.GetMethod("GetCustomCount").Invoke(group,null);int selected=-1;
  58. for(int i=0;i<count;i++){var size=groupType.GetMethod("GetGameViewSize").Invoke(group,new object[]{i});if((int)size.GetType().GetProperty("width").GetValue(size)==width&&(int)size.GetType().GetProperty("height").GetValue(size)==height){selected=i;break;}}
  59. if(selected<0){var type=assembly.GetType("UnityEditor.GameViewSize");var enumType=assembly.GetType("UnityEditor.GameViewSizeType");var size=Activator.CreateInstance(type,new object[]{Enum.ToObject(enumType,1),width,height,"Diamond Arrow "+width+"x"+height});groupType.GetMethod("AddCustomSize").Invoke(group,new[]{size});selected=count;}
  60. var viewType=assembly.GetType("UnityEditor.GameView");var view=EditorWindow.GetWindow(viewType);viewType.GetProperty("selectedSizeIndex",BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance).SetValue(view,selected);view.Repaint();
  61. }
  62. [MenuItem("Diamond Arrow/Validate all levels")]
  63. public static void ValidateLevels()
  64. {
  65. var book=LevelBook.Parse(AssetDatabase.LoadAssetAtPath<TextAsset>(Root+"Data/Levels.json").text);var errors=book.Validate();if(errors.Count>0)throw new InvalidOperationException(string.Join("\n",errors));Debug.Log("Validated 30 solvable levels, "+book.levels.Sum(l=>l.arrows.Length)+" arrows.");
  66. }
  67. }
  68. }