Skip to content

Commit 3c2db59

Browse files
committed
ProjectSettingから解像度の設定をした構造体ファイルの生成処理を追加
1 parent 8d2b243 commit 3c2db59

14 files changed

Lines changed: 350 additions & 1 deletion
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using UnityEditor;
2+
3+
using UnityEngine;
4+
5+
namespace ADONEGames.ResolutionCalcCache.Editor
6+
{
7+
/// <summary>
8+
/// PlayerSettingsに解像度を設定した構造体を生成する項目を追加する
9+
/// </summary>
10+
internal class ProjectSetting : SettingsProvider
11+
{
12+
private const string ProjectSettingPath = "Project/ADONEGames-Tools/Generator/ResolutionCalcCache-ResolutionData";
13+
14+
/// <summary>
15+
/// Constructor
16+
/// </summary>
17+
/// <param name="path"></param>
18+
/// <param name="scopes"></param>
19+
private ProjectSetting( string path, SettingsScope scopes = SettingsScope.Project ) : base( path, scopes )
20+
{
21+
ProjectSettingData.instance.SetDefaultDirectory();
22+
}
23+
24+
/// <summary>
25+
/// GUI
26+
/// </summary>
27+
/// <param name="searchContext"></param>
28+
public override void OnGUI( string searchContext )
29+
{
30+
ProjectSettingData.instance.OnGUI();
31+
32+
using( new EditorGUI.DisabledScope( ProjectSettingData.instance.ResolutionDataFolder == null ) )
33+
using( new EditorGUILayout.HorizontalScope() )
34+
{
35+
GUILayout.FlexibleSpace();
36+
37+
// 生成ボタン
38+
if( !GUILayout.Button( "Generate" ) )
39+
return;
40+
41+
// 生成
42+
var result = new ResolutionDataTemplateEditor( ProjectSettingData.instance ).Generate();
43+
44+
{
45+
// 通知
46+
var assembly = typeof( EditorWindow ).Assembly;
47+
var type = assembly.GetType( "UnityEditor.ProjectSettingsWindow" );
48+
49+
EditorWindow.GetWindow( type ).ShowNotification( new GUIContent( result.result ? $"{result.writePath}\n\nSuccessful file generation" : $"{result.writePath}\n\nFile generation failure" ) );
50+
}
51+
52+
53+
// 更新
54+
AssetDatabase.Refresh();
55+
}
56+
57+
EditorGUILayout.Space();
58+
59+
EditorGUILayout.LabelField( "" );
60+
}
61+
62+
/// <summary>
63+
/// SettingsProviderを生成する
64+
/// </summary>
65+
[SettingsProvider]
66+
public static SettingsProvider CreateMyCustomSettingsProvider()
67+
{
68+
return new ProjectSetting( ProjectSettingPath );
69+
}
70+
}
71+
}

Assets/ResolutionCalcCache/Editor/ProjectSetting.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using UnityEditor;
2+
3+
using UnityEngine;
4+
5+
6+
namespace ADONEGames.ResolutionCalcCache.Editor
7+
{
8+
/// <summary>
9+
/// PlayerSettingsに解像度を設定した構造体を生成する項目を追加する
10+
/// </summary>
11+
[FilePath( "ProjectSettings/ResolutionCalcCache.asset", FilePathAttribute.Location.ProjectFolder )]
12+
internal class ProjectSettingData : ScriptableSingleton<ProjectSettingData>, IResolutionData
13+
{
14+
[SerializeField]
15+
private DefaultAsset resolutionDataFolder;
16+
17+
[SerializeField]
18+
private string @namespace;
19+
20+
[SerializeField]
21+
private string @class;
22+
23+
[SerializeField]
24+
private int width;
25+
26+
[SerializeField]
27+
private int height;
28+
29+
30+
/// <summary>
31+
/// Folder Asset
32+
/// </summary>
33+
public DefaultAsset ResolutionDataFolder { get => resolutionDataFolder; set => SetResolutionDataFolder( value ); }
34+
35+
/// <summary>
36+
/// コード生成時の名前空間
37+
/// </summary>
38+
public string Namespace { get => @namespace; set => @namespace = value; }
39+
40+
/// <summary>
41+
/// コード生成時のクラス名
42+
/// </summary>
43+
public string Class { get => @class; set => @class = value; }
44+
45+
/// <summary>
46+
/// コード生成時の幅
47+
/// </summary>
48+
public int Width { get => width; set => width = value; }
49+
50+
/// <summary>
51+
/// コード生成時の高さ
52+
/// </summary>
53+
public int Height { get => height; set => height = value; }
54+
55+
/// <summary>
56+
/// フォルダの設定
57+
/// </summary>
58+
/// <param name="folder">Folder Asset</param>
59+
private void SetResolutionDataFolder( DefaultAsset folder )
60+
{
61+
if( folder == null )
62+
{
63+
resolutionDataFolder = null;
64+
65+
return;
66+
}
67+
68+
var path = AssetDatabase.GetAssetPath( folder );
69+
70+
var isDirectory = System.IO.Directory.Exists( path );
71+
72+
resolutionDataFolder = isDirectory ? folder : null;
73+
}
74+
75+
/// <summary>
76+
/// デフォルトのフォルダを設定する
77+
/// </summary>
78+
public void SetDefaultDirectory()
79+
{
80+
if( ResolutionDataFolder != null ) return;
81+
82+
SetFolderAsset( "Assets" );
83+
}
84+
85+
/// <summary>
86+
/// フォルダを設定する
87+
/// </summary>
88+
/// <param name="path"></param>
89+
public void SetFolderAsset( string path )
90+
{
91+
var isDirectory = System.IO.Directory.Exists( path );
92+
ResolutionDataFolder = isDirectory ? AssetDatabase.LoadAssetAtPath<DefaultAsset>( path ) : null;
93+
}
94+
95+
/// <summary>
96+
/// GUIを描画する
97+
/// </summary>
98+
public void OnGUI()
99+
{
100+
var folder = EditorGUILayout.ObjectField( "ResolutionDataFolder", ResolutionDataFolder, typeof( DefaultAsset ), false ) as DefaultAsset;
101+
102+
using( new EditorGUI.DisabledScope( true ) )
103+
EditorGUILayout.TextField( AssetDatabase.GetAssetPath( folder ) );
104+
105+
EditorGUILayout.Space();
106+
107+
108+
Namespace = EditorGUILayout.TextField( "Namespace", Namespace );
109+
Class = EditorGUILayout.TextField( "Class", Class );
110+
Width = EditorGUILayout.IntField( "Width", Width );
111+
Height = EditorGUILayout.IntField( "Height", Height );
112+
113+
if( ResolutionDataFolder != folder )
114+
ResolutionDataFolder = folder;
115+
}
116+
}
117+
}

Assets/ResolutionCalcCache/Editor/ProjectSettingData.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/ResolutionCalcCache/Editor/ResolutionCalcCache_Editor.asmdef

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"references": [
55
"GUID:395043984bcb042b5933ffa07d9689cd"
66
],
7-
"includePlatforms": [],
7+
"includePlatforms": [
8+
"Editor"
9+
],
810
"excludePlatforms": [],
911
"allowUnsafeCode": false,
1012
"overrideReferences": false,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////
2+
// This file is automatically generated.
3+
// Modifying it is not recommended.
4+
///////////////////////////////////////////////////////////////////////////////////////////
5+
using ADONEGames.ResolutionCalcCache;
6+
7+
namespace #NAMESPACE#
8+
{
9+
/// <summary>
10+
/// Serializable resolution data
11+
/// </summary>
12+
public struct #CLASS# : IResolutionData
13+
{
14+
/// <inheritdoc />
15+
int IResolutionData.Width => #WIDTH#;
16+
17+
/// <inheritdoc />
18+
int IResolutionData.Height => #HEIGHT#;
19+
}
20+
}

Assets/ResolutionCalcCache/Editor/ResolutionDataTemplate.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using UnityEditor;
2+
3+
namespace ADONEGames.ResolutionCalcCache.Editor
4+
{
5+
/// <summary>
6+
/// ResolutionDataのテンプレートを生成するクラス
7+
/// </summary>
8+
internal readonly struct ResolutionDataTemplateEditor
9+
{
10+
private const string ResolutionDataTemplatePathKeyword = "/ResolutionCalcCache/Editor/ResolutionDataTemplate";
11+
12+
private const string NamespaceKeyword = "#NAMESPACE#";
13+
private const string ClassKeyword = "#CLASS#";
14+
private const string WidthKeyword = "#WIDTH#";
15+
private const string HeightKeyword = "#HEIGHT#";
16+
17+
private readonly ProjectSettingData _data;
18+
private readonly string _dataPath;
19+
private readonly string _filePath;
20+
private readonly string _templatePath;
21+
22+
/// <summary>
23+
/// Constructor
24+
/// </summary>
25+
public ResolutionDataTemplateEditor( ProjectSettingData data )
26+
{
27+
_data = data;
28+
_dataPath = AssetDatabase.GetAssetPath( _data.ResolutionDataFolder );
29+
_filePath = $"{_data.Class}.cs";
30+
_templatePath = SearchTemplatePath();
31+
32+
return;
33+
34+
// Local Functions
35+
string SearchTemplatePath()
36+
{
37+
var paths = AssetDatabase.GetAllAssetPaths();
38+
39+
foreach( var path in paths )
40+
{
41+
if( !path.Contains( ResolutionDataTemplatePathKeyword ) ) continue;
42+
43+
return path;
44+
}
45+
46+
return string.Empty;
47+
}
48+
}
49+
50+
/// <summary>
51+
/// Generate ResolutionData
52+
/// </summary>
53+
public (string writePath, bool result) Generate()
54+
{
55+
// テンプレートが見つからない場合は生成しない
56+
if( string.IsNullOrEmpty( _templatePath ) || string.IsNullOrEmpty( _dataPath ) )
57+
return (string.Empty, false);
58+
59+
var text = System.IO.File.ReadAllText( _templatePath );
60+
61+
text = text.Replace( NamespaceKeyword, _data.Namespace );
62+
text = text.Replace( ClassKeyword, _data.Class );
63+
text = text.Replace( WidthKeyword, _data.Width.ToString() );
64+
text = text.Replace( HeightKeyword, _data.Height.ToString() );
65+
66+
var writePath = $"{_dataPath}/{_filePath}";
67+
System.IO.File.WriteAllText( writePath, text );
68+
69+
return (writePath, true);
70+
}
71+
}
72+
}

Assets/ResolutionCalcCache/Editor/ResolutionDataTemplateEditor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/ResolutionCalcCache/Runtime/Data.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)