Skip to content

Commit 0f09d30

Browse files
committed
Merge pull request #36 from eral/ConfigAssetProblem
変換UI周りの調整
2 parents e8768a7 + 29143ce commit 0f09d30

File tree

7 files changed

+217
-279
lines changed

7 files changed

+217
-279
lines changed

Editor/Config/Config.cs

Lines changed: 100 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using UnityEngine;
1+
using UnityEngine;
22
using UnityEditor;
33
using System;
44
using System.Collections.Generic;
@@ -13,29 +13,35 @@ namespace MMD
1313
[Serializable]
1414
public class Config : ScriptableObject
1515
{
16-
public InspectorConfig inspector_config;
17-
public DefaultPMDImportConfig pmd_config;
18-
public DefaultVMDImportConfig vmd_config;
16+
static Config config_ = null;
17+
public InspectorConfig inspector_config = null;
18+
public PMDImportConfig pmd_config = null;
19+
public VMDImportConfig vmd_config = null;
1920

20-
private List<ConfigBase> update_list;
21+
private List<ConfigBase> update_list = null;
2122
public void OnEnable()
2223
{
23-
// Inspectorで編集をさせない
24-
hideFlags = HideFlags.NotEditable;
25-
if (pmd_config == null)
24+
if (inspector_config == null)
2625
{
27-
// ここで初期化処理を書く
28-
pmd_config = new DefaultPMDImportConfig();
29-
vmd_config = new DefaultVMDImportConfig();
3026
inspector_config = new InspectorConfig();
3127
}
28+
if (pmd_config == null)
29+
{
30+
pmd_config = new PMDImportConfig();
31+
}
32+
if (vmd_config == null)
33+
{
34+
vmd_config = new VMDImportConfig();
35+
}
3236
if (update_list == null)
3337
{
3438
update_list = new List<ConfigBase>();
3539
update_list.Add(inspector_config);
3640
update_list.Add(pmd_config);
3741
update_list.Add(vmd_config);
3842
}
43+
44+
hideFlags = HideFlags.None; //以前の書き換え不可assetが残っているかもしれないので明示的に書き換え可能を設定
3945
}
4046

4147
/// <summary>
@@ -48,6 +54,11 @@ public void OnGUI()
4854
{
4955
item.OnGUI();
5056
});
57+
58+
//変更確認
59+
if (GUI.changed) {
60+
EditorUtility.SetDirty(config_);
61+
}
5162
}
5263

5364
/// <summary>
@@ -67,17 +78,20 @@ public static string GetConfigPath()
6778
/// <returns>読み込んで生成したConfigオブジェクト</returns>
6879
public static Config LoadAndCreate()
6980
{
70-
var path = Config.GetConfigPath();
71-
var config = (Config)AssetDatabase.LoadAssetAtPath(path, typeof(Config));
72-
73-
//// なかったら作成する
74-
if (config == null)
81+
if (config_ == null)
7582
{
76-
config = CreateInstance<Config>();
77-
AssetDatabase.CreateAsset(config, path);
78-
EditorUtility.SetDirty(config);
83+
var path = Config.GetConfigPath();
84+
config_ = (Config)AssetDatabase.LoadAssetAtPath(path, typeof(Config));
85+
86+
//// なかったら作成する
87+
if (config_ == null)
88+
{
89+
config_ = CreateInstance<Config>();
90+
AssetDatabase.CreateAsset(config_, path);
91+
EditorUtility.SetDirty(config_);
92+
}
7993
}
80-
return config;
94+
return config_;
8195
}
8296
}
8397

@@ -87,79 +101,94 @@ public static Config LoadAndCreate()
87101
[Serializable]
88102
public class InspectorConfig : ConfigBase
89103
{
90-
public bool use_pmd_preload = false;
91-
public bool use_vmd_preload = false;
104+
public bool use_pmd_preload = true;
105+
public bool use_vmd_preload = true;
92106

93-
public InspectorConfig()
107+
public override string GetTitle()
94108
{
95-
this.title = "Inspector Config";
109+
return "Inspector Config";
96110
}
97111

98-
public override void OnGUI()
112+
public override void OnGUIFunction()
99113
{
100-
base.OnGUI(() =>
101-
{
102-
use_pmd_preload = EditorGUILayout.Toggle("Use PMD Preload", use_pmd_preload);
103-
use_vmd_preload = EditorGUILayout.Toggle("Use VMD Preload", use_vmd_preload);
104-
}
105-
);
114+
use_pmd_preload = EditorGUILayout.Toggle("Use PMD Preload", use_pmd_preload);
115+
use_vmd_preload = EditorGUILayout.Toggle("Use VMD Preload", use_vmd_preload);
116+
}
117+
118+
public InspectorConfig Clone()
119+
{
120+
return (InspectorConfig)MemberwiseClone();
106121
}
107122
}
108123

109124
/// <summary>
110-
/// PMDインポートのデフォルトコンフィグ
125+
/// PMDインポートのコンフィグ
111126
/// </summary>
112127
[Serializable]
113-
public class DefaultPMDImportConfig : ConfigBase
128+
public class PMDImportConfig : ConfigBase
114129
{
115130
public PMDConverter.ShaderType shader_type = PMDConverter.ShaderType.MMDShader;
116131
public PMXConverter.AnimationType animation_type = PMXConverter.AnimationType.LegacyAnimation;
117132
public bool rigidFlag = true;
118133
public bool use_ik = true;
119134
public float scale = 0.085f;
120-
public bool is_pmx_base_import = false;
135+
public bool is_pmx_base_import = true;
121136

122-
public DefaultPMDImportConfig()
137+
public override string GetTitle()
123138
{
124-
this.title = "Default PMD Import Config";
139+
return "Default PMD Import Config";
125140
}
126141

127-
public override void OnGUI()
142+
public override void OnGUIFunction()
128143
{
129-
base.OnGUI(() =>
130-
{
131-
shader_type = (PMDConverter.ShaderType)EditorGUILayout.EnumPopup("Shader Type", shader_type);
132-
rigidFlag = EditorGUILayout.Toggle("Rigidbody", rigidFlag);
133-
animation_type = (PMXConverter.AnimationType)EditorGUILayout.EnumPopup("Animation Type", animation_type);
134-
use_ik = EditorGUILayout.Toggle("Use IK", use_ik);
135-
is_pmx_base_import = EditorGUILayout.Toggle("Use PMX Base Import", is_pmx_base_import);
144+
shader_type = (PMDConverter.ShaderType)EditorGUILayout.EnumPopup("Shader Type", shader_type);
145+
rigidFlag = EditorGUILayout.Toggle("Rigidbody", rigidFlag);
146+
animation_type = (PMXConverter.AnimationType)EditorGUILayout.EnumPopup("Animation Type", animation_type);
147+
use_ik = EditorGUILayout.Toggle("Use IK", use_ik);
148+
scale = EditorGUILayout.Slider("Scale", scale, 0.001f, 1.0f);
149+
EditorGUILayout.BeginHorizontal();
150+
{
151+
EditorGUILayout.PrefixLabel(" ");
152+
if (GUILayout.Button("Original", EditorStyles.miniButtonLeft)) {
153+
scale = 0.085f;
154+
}
155+
if (GUILayout.Button("1.0", EditorStyles.miniButtonRight)) {
156+
scale = 1.0f;
136157
}
137-
);
158+
}
159+
EditorGUILayout.EndHorizontal();
160+
is_pmx_base_import = EditorGUILayout.Toggle("Use PMX Base Import", is_pmx_base_import);
161+
}
162+
163+
public PMDImportConfig Clone()
164+
{
165+
return (PMDImportConfig)MemberwiseClone();
138166
}
139167
}
140168

141169
/// <summary>
142-
/// VMDインポートのデフォルトコンフィグ
170+
/// VMDインポートのコンフィグ
143171
/// </summary>
144172
[Serializable]
145-
public class DefaultVMDImportConfig : ConfigBase
173+
public class VMDImportConfig : ConfigBase
146174
{
147-
public bool createAnimationFile;
148-
public int interpolationQuality;
175+
public bool createAnimationFile = false;
176+
public int interpolationQuality = 1;
149177

150-
public DefaultVMDImportConfig()
178+
public override string GetTitle()
151179
{
152-
this.title = "Default VMD Import Config";
180+
return "Default VMD Import Config";
153181
}
154182

155-
public override void OnGUI()
183+
public override void OnGUIFunction()
156184
{
157-
base.OnGUI(() =>
158-
{
159-
createAnimationFile = EditorGUILayout.Toggle("Create Asset", createAnimationFile);
160-
interpolationQuality = EditorGUILayout.IntSlider("Interpolation Quality", interpolationQuality, 1, 10);
161-
}
162-
);
185+
createAnimationFile = EditorGUILayout.Toggle("Create Asset", createAnimationFile);
186+
interpolationQuality = EditorGUILayout.IntSlider("Interpolation Quality", interpolationQuality, 1, 10);
187+
}
188+
189+
public VMDImportConfig Clone()
190+
{
191+
return (VMDImportConfig)MemberwiseClone();
163192
}
164193
}
165194

@@ -168,11 +197,6 @@ public override void OnGUI()
168197
/// </summary>
169198
public class ConfigBase
170199
{
171-
/// <summary>
172-
/// このコンフィグのタイトルを指定します
173-
/// </summary>
174-
protected string title = "";
175-
176200
/// <summary>
177201
/// 開け閉めの状態
178202
/// </summary>
@@ -181,19 +205,28 @@ public class ConfigBase
181205
/// <summary>
182206
/// GUI処理を行います
183207
/// </summary>
184-
/// <param name="OnGUIFunction">引数・戻り値なしのラムダ式</param>
185-
public void OnGUI(Action OnGUIFunction)
208+
public void OnGUI()
186209
{
210+
var title = GetTitle();
187211
fold = EditorGUILayout.Foldout(fold, title);
188-
if (fold)
212+
if (fold) {
189213
OnGUIFunction();
214+
}
190215
EditorGUILayout.Space();
191216
}
192217

218+
/// <summary>
219+
/// このコンフィグのタイトルを取得します
220+
/// </summary>
221+
public virtual string GetTitle()
222+
{
223+
return "";
224+
}
225+
193226
/// <summary>
194227
/// GUI処理を行います
195228
/// </summary>
196-
public virtual void OnGUI()
229+
public virtual void OnGUIFunction()
197230
{
198231
}
199232
}

Editor/Config/ConfigWindow.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ namespace MMD
55
{
66
public class ConfigWindow : EditorWindow
77
{
8-
private static Config config;
9-
private static string path;
8+
private Config config;
109

1110
[MenuItem("MMD for Unity/Config")]
1211
public static void Init()
@@ -18,36 +17,18 @@ public static void Init()
1817
// フォーカスが外れて戻ってきたときや再度開かれたときなど
1918
void OnEnable()
2019
{
21-
// オブジェクトを「Hierarchy」に表示しない。また、アセットの中にあれば、プロジェクトビューに表示しない
22-
// オブジェクトがシーンに保存されない。また、新しいシーンを読んでも、オブジェクトが破棄されない
23-
hideFlags = HideFlags.HideAndDontSave;
24-
2520
if (config == null)
2621
{
2722
// 読み込む
2823
config = MMD.Config.LoadAndCreate();
29-
30-
// なかったら作成する
31-
if (config == null)
32-
{
33-
path = MMD.Config.GetConfigPath();
34-
config = CreateInstance<Config>();
35-
AssetDatabase.CreateAsset(config, path);
36-
EditorUtility.SetDirty(config);
37-
}
3824
}
3925
}
4026

4127
// ウィンドウの描画処理
4228
void OnGUI()
4329
{
44-
// たいとる
45-
EditorGUILayout.LabelField("MMD for Unity Configuration");
46-
EditorGUILayout.Space();
47-
4830
// あとは任せる
4931
config.OnGUI();
50-
5132
}
5233
}
5334
}

Editor/Inspector/InspectorBase.cs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,43 @@
1212
using UnityEditor.Callbacks;
1313
using UnityEngine;
1414

15-
#if !(UNITY_3_5 || UNITY_3_4 || UNITY_3_3)
16-
1715
namespace MMD
1816
{
19-
public class InspectorBase : Editor
20-
{
17+
[InitializeOnLoad]
18+
public class InspectorBase : Editor
19+
{
20+
static InspectorBase()
21+
{
22+
EntryEditorApplicationUpdate();
23+
}
24+
2125
[DidReloadScripts]
2226
static void OnDidReloadScripts()
2327
{
24-
EditorApplication.update += () =>
28+
EntryEditorApplicationUpdate();
29+
}
30+
31+
static void EntryEditorApplicationUpdate()
32+
{
33+
EditorApplication.update += Update;
34+
}
35+
36+
static void Update()
37+
{
38+
if (Selection.objects.Length != 0)
2539
{
26-
if (Selection.objects.Length != 0)
40+
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
41+
string extension = Path.GetExtension(path).ToLower();
42+
43+
if (extension == ".pmd" || extension == ".pmx")
44+
{
45+
SetupScriptableObject<PMDScriptableObject>(path);
46+
}
47+
else if (extension == ".vmd")
2748
{
28-
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
29-
string extension = Path.GetExtension(path).ToLower();
30-
31-
if (extension == ".pmd" || extension == ".pmx")
32-
{
33-
SetupScriptableObject<PMDScriptableObject>(path);
34-
}
35-
else if (extension == ".vmd")
36-
{
37-
SetupScriptableObject<VMDScriptableObject>(path);
38-
}
49+
SetupScriptableObject<VMDScriptableObject>(path);
3950
}
40-
};
51+
}
4152
}
4253

4354
static void SetupScriptableObject<T>(string path) where T : ScriptableObjectBase
@@ -49,8 +60,7 @@ static void SetupScriptableObject<T>(string path) where T : ScriptableObjectBase
4960
Selection.activeObject = scriptableObject;
5061
EditorUtility.UnloadUnusedAssets();
5162
}
52-
}
63+
}
5364
}
5465

5566
#endif
56-
#endif

0 commit comments

Comments
 (0)