Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 676a7fb

Browse files
committed
增加简单的资源导入
1 parent e8a9337 commit 676a7fb

File tree

4 files changed

+275
-1
lines changed

4 files changed

+275
-1
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
//using System.Collections;
2+
//using System.Collections.Generic;
3+
//using UnityEditor;
4+
//using UnityEngine;
5+
//using System.Reflection;
6+
//using LitJson;
7+
//using OdinSerializer;
8+
9+
//namespace Wanderer.GameFramework
10+
//{
11+
// public class StandardAssetEditor : EditorWindow
12+
// {
13+
// private const string _configName = "StandardAssetEditor.json";
14+
// private JsonData _config;
15+
// private Vector2 _scrollView = Vector2.zero;
16+
17+
// private Dictionary<StandardAssetBase, bool> _allStandardAssets;
18+
19+
// [MenuItem("Tools/Asset Management/Standard Asset #&K")]
20+
// private static void OpenWindow()
21+
// {
22+
// GetWindow<StandardAssetEditor>("Standard Asset Editor");
23+
// }
24+
25+
// private void OnEnable()
26+
// {
27+
// _config = ProjectSettingsConfig.LoadJsonData(_configName);
28+
// if (_config == null)
29+
// {
30+
// _config = new JsonData();
31+
// }
32+
33+
// _allStandardAssets = new Dictionary<StandardAssetBase, bool>() { { new TextureStandardAsset(), true } };
34+
// foreach (var item in _allStandardAssets.Keys)
35+
// {
36+
// AssetImporter assetImporter = null;
37+
// if (_config.ContainsKey(item.Name))
38+
// {
39+
// JsonData importItem = _config[item.Name];
40+
// if (importItem.ContainsKey("AssetImporter"))
41+
// {
42+
// byte[] buffer = System.Text.Encoding.UTF8.GetBytes(importItem["AssetImporter"].ToString());
43+
// assetImporter = SerializationUtility.DeserializeValue<AssetImporter>(buffer, DataFormat.Binary);
44+
// }
45+
// }
46+
// else
47+
// {
48+
// _config[item.Name] = new JsonData();
49+
// }
50+
// item.Importer = assetImporter;
51+
// }
52+
// }
53+
54+
55+
// private void OnGUI()
56+
// {
57+
// if (_allStandardAssets == null
58+
// || _allStandardAssets.Count <= 0)
59+
// return;
60+
// if (GUILayout.Button("Save", GUILayout.Width(100)))
61+
// {
62+
// foreach (var item in _allStandardAssets.Keys)
63+
// {
64+
// JsonData importItem = _config[item.Name];
65+
// byte[] buffer = SerializationUtility.SerializeValue(item.Importer, DataFormat.Binary);
66+
// importItem["AssetImporter"] = System.Text.Encoding.UTF8.GetString(buffer);
67+
// }
68+
69+
// ProjectSettingsConfig.SaveJsonData(_configName, _config);
70+
// AssetDatabase.Refresh();
71+
// EditorUtility.DisplayDialog("", "Save data.", "ok");
72+
// }
73+
// _scrollView = GUILayout.BeginScrollView(_scrollView);
74+
// foreach (var item in _allStandardAssets)
75+
// {
76+
// bool foldout = EditorGUILayout.Foldout(item.Value, item.Key.Name);
77+
// if (foldout != item.Value)
78+
// {
79+
// _allStandardAssets[item.Key] = foldout;
80+
// GUIUtility.ExitGUI();
81+
// break;
82+
// }
83+
// if (foldout)
84+
// {
85+
// GUILayout.BeginVertical("HelpBox");
86+
// item.Key.OnDraw();
87+
// GUILayout.EndVertical();
88+
// }
89+
// }
90+
// GUILayout.EndScrollView();
91+
// }
92+
// }
93+
94+
// abstract class StandardAssetBase
95+
// {
96+
// public virtual string Name
97+
// {
98+
// get
99+
// {
100+
// return GetType().Name;
101+
// }
102+
// }
103+
104+
// public virtual AssetImporter Importer { get; set; }
105+
106+
// protected FieldInfo[] _filedInfos;
107+
// protected PropertyInfo[] _propertyInfos;
108+
109+
110+
// public abstract void OnDraw();
111+
// }
112+
113+
// class TextureStandardAsset : StandardAssetBase
114+
// {
115+
// private TextureImporter _textureImporter;
116+
// public override AssetImporter Importer
117+
// {
118+
// get
119+
// {
120+
// return _textureImporter;
121+
// }
122+
// set
123+
// {
124+
// if (value == null)
125+
// {
126+
// var template = AssetDatabase.LoadAssetAtPath<TextAsset>($"Assets/UnityGameFramework/GameFramework/Editor/AssetManagement/StandardAsset/TextureImporter.bytes");
127+
// _textureImporter = SerializationUtility.DeserializeValue<TextureImporter>(template.bytes, DataFormat.Binary);
128+
// }
129+
// else
130+
// {
131+
// _textureImporter = value as TextureImporter;
132+
// }
133+
// }
134+
// }
135+
136+
// public override void OnDraw()
137+
// {
138+
// GUILayout.Button("XXX");
139+
140+
// if (_textureImporter == null)
141+
// return;
142+
// GUILayout.Button("XXX");
143+
// _textureImporter.textureType = (TextureImporterType)EditorGUILayout.EnumPopup(_textureImporter.textureType);
144+
// }
145+
// }
146+
//}

GameFramework/Editor/AssetManagement/StandardAssetEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,128 @@
1+
using OdinSerializer;
12
using System.Collections;
23
using System.Collections.Generic;
4+
using System.IO;
35
using UnityEditor;
46
using UnityEngine;
57

68
namespace Wanderer.GameFramework
79
{
810
public class StandardAssetPostprocessor : AssetPostprocessor
911
{
12+
/// <summary>
13+
/// 在完成任意数量的资源导入后(当资源进度条到达末尾时)调用此函数。
14+
/// </summary>
15+
/// <param name="importedAssets"></param>
16+
/// <param name="deletedAssets"></param>
17+
/// <param name="movedAssets"></param>
18+
/// <param name="movedFromAssetPaths"></param>
19+
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
20+
{
21+
22+
}
23+
24+
/// <summary>
25+
/// 将此函数添加到一个子类中,以在导入所有资源之前获取通知。
26+
/// </summary>
27+
void OnPreprocessAsset()
28+
{
29+
//string fileName = assetImporter.GetType().Name;
30+
//fileName = $"ProjectSettings/{fileName}.AssetImporter";
31+
//if (!File.Exists(fileName))
32+
//{
33+
// byte[] buffer = SerializationUtility.SerializeValue(assetImporter, DataFormat.Binary);
34+
// File.WriteAllBytes(fileName, buffer);
35+
//}
36+
}
37+
38+
/// <summary>
39+
/// 将此函数添加到一个子类中,以在纹理导入器运行之前获取通知。
40+
/// </summary>
41+
void OnPreprocessTexture()
42+
{
43+
if (!CheckFlag("TextureFlag"))
44+
{
45+
TextureImporter textureImporter = (TextureImporter)assetImporter;
46+
textureImporter.wrapMode = TextureWrapMode.Clamp;
47+
textureImporter.filterMode = FilterMode.Bilinear;
48+
textureImporter.mipmapEnabled = false;
49+
textureImporter.sRGBTexture = false;
50+
textureImporter.isReadable = false;
51+
if (assetPath.StartsWith("Assets/Game/Texture/UI"))
52+
{
53+
textureImporter.textureType = TextureImporterType.Sprite;
54+
}
55+
else
56+
{
57+
//Android设置
58+
TextureImporterPlatformSettings androidSettings = new TextureImporterPlatformSettings();
59+
androidSettings.overridden = true;
60+
androidSettings.name = "Android";
61+
androidSettings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
62+
androidSettings.format = TextureImporterFormat.ETC2_RGBA8;
63+
textureImporter.SetPlatformTextureSettings(androidSettings);
64+
65+
//iOS设置
66+
TextureImporterPlatformSettings iOSSettings = new TextureImporterPlatformSettings();
67+
iOSSettings.overridden = true;
68+
iOSSettings.name = "iOS";
69+
iOSSettings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
70+
iOSSettings.format = TextureImporterFormat.PVRTC_RGBA4;
71+
textureImporter.SetPlatformTextureSettings(iOSSettings);
72+
}
73+
EditorUtility.SetDirty(assetImporter);
74+
textureImporter.SaveAndReimport();
75+
AssetDatabase.Refresh();
76+
}
77+
}
78+
79+
/// <summary>
80+
/// 将此函数添加到一个子类中,以在导入模型(.fbx、.mb 文件等)之前获取通知。
81+
/// </summary>
82+
void OnPreprocessModel()
83+
{
84+
85+
}
86+
87+
/// <summary>
88+
/// 将此函数添加到一个子类中,以在导入音频剪辑之前获取通知。
89+
/// </summary>
90+
void OnPreprocessAudio()
91+
{
92+
if (!CheckFlag("AudioFlag"))
93+
{
94+
AudioImporter audioImporter = (AudioImporter)assetImporter;
95+
audioImporter.forceToMono = true;
96+
//Android设置
97+
AudioImporterSampleSettings androidSettings = new AudioImporterSampleSettings();
98+
androidSettings.loadType = AudioClipLoadType.Streaming;
99+
androidSettings.compressionFormat = AudioCompressionFormat.AAC;
100+
androidSettings.quality = 100;
101+
androidSettings.sampleRateSetting = AudioSampleRateSetting.OverrideSampleRate;
102+
androidSettings.sampleRateOverride = 22050;
103+
audioImporter.SetOverrideSampleSettings("Android", androidSettings);
104+
//iOS设置
105+
AudioImporterSampleSettings iOSSettings = new AudioImporterSampleSettings();
106+
iOSSettings.loadType = AudioClipLoadType.DecompressOnLoad;
107+
iOSSettings.compressionFormat = AudioCompressionFormat.AAC;
108+
iOSSettings.quality = 100;
109+
iOSSettings.sampleRateSetting = AudioSampleRateSetting.OverrideSampleRate;
110+
iOSSettings.sampleRateOverride = 22050;
111+
audioImporter.SetOverrideSampleSettings("iOS", iOSSettings);
112+
113+
EditorUtility.SetDirty(assetImporter);
114+
audioImporter.SaveAndReimport();
115+
AssetDatabase.Refresh();
116+
}
117+
}
118+
119+
private bool CheckFlag(string key)
120+
{
121+
if (assetImporter.userData.Equals(key))
122+
return true;
123+
assetImporter.userData = key;
124+
return false;
125+
}
10126
}
11127

12128
}

GameFramework/Editor/GameFramework.Editor.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"references": [
55
"GameFramework",
66
"Libraries",
7-
"Unity.Addressables.Editor"
7+
"Unity.Addressables.Editor",
8+
"OdinSerializer"
89
],
910
"includePlatforms": [
1011
"Editor"

0 commit comments

Comments
 (0)