Unity Editor(三)Resources目录限制不能出现中文

来源:互联网 发布:数控车宏程序编程入门 编辑:程序博客网 时间:2024/06/06 06:31

Unity Editor(三)Resources目录限制不能出现中文

在UnityEditor下,使用AssetPostprocessor类,来实现对Resources下的文件名进行限制,让文件名不能是中文。下面是实现的代码:

using UnityEngine;using System.Collections;using UnityEditor;using System.Text.RegularExpressions;using System.IO;using System.Collections.Generic;public class ResourcesCanNotBeChinese : AssetPostprocessor{    public enum CheckState { success,failure}        /// <summary>    /// 该方法是由AssetPostprocessor的回调方法    /// </summary>static void  OnPostprocessAllAssets (string[] importedAssets ,string[] deletedAssets ,string[] movedAssets ,string[] movedFromAssetPaths){        foreach (var importedAsset in importedAssets)        {            Debug.Log("importedAssets:"+ importedAsset);            if (ChekFileIsResourceAndNotChianese(importedAsset) == CheckState.failure)            {                //删除文件                AssetDatabase.DeleteAsset(importedAsset);                EditorUtility.DisplayDialog("error", "Resource下的文件均不能为中文", "ok");            }        }         for (int i = 0; i < movedAssets.Length; i++)        {            if (ChekFileIsResourceAndNotChianese(movedAssets[i]) == CheckState.failure)            {                //删除文件                // AssetDatabase.DeleteAsset(str);                EditorUtility.DisplayDialog("error", "Resource下的文件均不能为中文", "ok");                AssetDatabase.MoveAsset(movedAssets[i], movedFromAssetPaths[i]);            }        }        foreach (var str in movedFromAssetPaths)            Debug.Log("movedFromAssetPaths:" + str);}    /// <summary>    /// 检查文件是否属于Resources目录,并且文件为中文    /// </summary>    public static CheckState ChekFileIsResourceAndNotChianese(string path)    {        string fileName = Path.GetFileName(path);        string filePath = Path.GetDirectoryName(path);        filePath = Path.GetFileName(filePath);        if (filePath == "Resources")        {            if (Regex.Match(fileName, "^[a-zA-Z\0-9]+$").Success)            {                return CheckState.success;            }            else            {                return CheckState.failure;            }        }        return CheckState.success;    }}



=======================================================================

结束。

0 0
原创粉丝点击