Main/Assets/Editor/DIY/AssetPostProcessorExternal.cs
2025-01-25 04:38:09 +08:00

169 lines
6.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using Thousandto.Code.Center;
using Thousandto.DIY;
using UnityEditor;
using UnityEngine;
/// <summary>
/// AssetPostProcessorExternal 贴图、模型、声音等资源导入时调用,可自动设置相应参数
/// 导入图片时自动设置图片的参数
/// </summary>
public class AssetPostProcessorExternal : AssetPostprocessor
{
void OnPreprocessAnimation()
{
var path = assetImporter.assetPath;
if (path.Contains("RawResources/player"))
{
AnimationClip oldAnim = null;
if(FuncellEditorStartup.AnimDic != null && FuncellEditorStartup.AnimDic.TryGetValue(path, out oldAnim))
{
GameObject.DestroyImmediate(oldAnim);
}
//加载此动作
var anim = AssetDatabase.LoadAssetAtPath<AnimationClip>(assetImporter.assetPath);
if(FuncellEditorStartup.AnimDic != null && anim != null)
{
GameObject.DontDestroyOnLoad(anim);
FuncellEditorStartup.AnimDic[path] = anim;
}
}
}
//模型导入之前调用
public void OnPreprocessModel()
{
Debug.Log("OnPreprocessModel=" + this.assetPath);
}
//模型导入之前调用
public void OnPostprocessModel(GameObject go)
{
Debug.Log("OnPostprocessModel=" + go.name);
}
public void OnPostprocessAudio(AudioClip clip)
{
Debug.Log("OnPostprocessAudio=" + clip.name);
}
public void OnPreprocessAudio()
{
Debug.Log("OnPostprocessAudio=" + this.assetPath);
//AudioImporter audio = this.assetImporter as AudioImporter;
//audio.format = AudioImporterFormat.Compressed;
}
/// <summary>
/// 图片导入之前调用可设置图片的格式、Tag……
/// </summary>
void OnPreprocessTexture()
{
//特效的纹理导入时,对iphone格式进行调整.
if (this.assetPath.Replace('\\','/').IndexOf("vfx/fxresources/texture/") >=0)
{
TextureImporter texImporter = (TextureImporter)assetImporter;
TextureImporterPlatformSettings pset = new TextureImporterPlatformSettings();
pset.name = "iPhone";
pset.overridden = false;
pset.format = TextureImporterFormat.Automatic;
pset.textureCompression = TextureImporterCompression.Compressed;
texImporter.SetPlatformTextureSettings(pset);
}
Debug.Log("OnPreProcessTexture=" + this.assetPath);
}
/// <summary>
/// 图片已经被压缩、保存到指定目录下之后调用
/// </summary>
/// <param name="texture"></param>
void OnPostprocessTexure(Texture2D texture)
{
Debug.Log(texture.name);
}
/// <summary>
/// 所有资源被导入、删除、移动完成之后调用
/// </summary>
/// <param name="importedAssets"></param>
/// <param name="deletedAssets"></param>
/// <param name="movedAssets"></param>
/// <param name="movedFromAssetPaths"></param>
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (string str in importedAssets)
{
Debug.Log("Reimported Asset: " + str);
if (!Application.isPlaying)
return;
if (str.Contains("GameAssets/Resources/Lua"))
{
ReLoadLuaFile(str, "UI/Forms/");
ReLoadLuaFile(str, "Logic/");
if (str.Contains("KeyCodeSystem"))
{
GameCenter.LuaSystem.DoString("KeyCodeSystem = nil;Utils.RemoveRequiredByName(\"Common.CustomLib.KeyCodeSystem\");KeyCodeSystem = require(\"Common.CustomLib.KeyCodeSystem\")");
}
}
}
foreach (string str in deletedAssets)
{
Debug.Log("Deleted Asset: " + str);
}
for (int i = 0; i < movedAssets.Length; i++)
{
Debug.Log("Moved Asset: " + movedAssets[i] + " from: " + movedFromAssetPaths[i]);
}
}
//重新加载Lua脚本
static void ReLoadLuaFile(string assetPath, string folder)
{
if (!assetPath.Contains(folder))
return;
string path = "";
string name = GetName(assetPath, folder, out path);
GetFilesPath(path, folder);
if(folder == "UI/Forms/")
GameCenter.LuaSystem.Adaptor.RenewForm(name, GetFilesPath(path, folder));
else if(folder == "Logic/")
{
GameCenter.LuaSystem.Adaptor.RenewSystem(name, GetFilesPath(path, folder));
}
}
static string GetName(string assetPath, string folder, out string filePath)
{
int startIndex = assetPath.IndexOf(folder);
int nameStartIndex = startIndex + folder.Length;
var str2 = assetPath.Substring(nameStartIndex);//UINewServerListForm\UIServerListPanel\UIServerCharItem.lua
int nameEndIndex = str2.IndexOf("/") + nameStartIndex - 1;
string name = assetPath.Substring(nameStartIndex, nameEndIndex - nameStartIndex + 1);
//Debug.Log("name >>>>>>>>> " + name);
filePath = assetPath.Substring(0, nameEndIndex + 1);
//Debug.Log("path >>>>>>>>> " + filePath);
return name;
}
static string[] GetFilesPath(string path,string folder)
{
var dirctory = new System.IO.DirectoryInfo(path);
int startIndex = dirctory.FullName.Replace("\\", "/").LastIndexOf(folder);
var files = dirctory.GetFiles("*", System.IO.SearchOption.AllDirectories);
List<string> pathList = new List<string>();
for (int i = 0; i < files.Length; i++)
{
if (files[i].Name.EndsWith(".meta")) continue;
//Debug.Log("FullName:" + files[i].FullName.Replace("\\", "/"));
var itemName = files[i].FullName.Replace("\\", "/");
var itemPath = itemName.Substring(startIndex, itemName.LastIndexOf(".lua") - startIndex).Replace("/", ".");
pathList.Add(itemPath);
//Debug.Log("========================= " +i+" , "+ itemPath);
}
return pathList.ToArray();
}
}