[MenuItem("Tools/创建 Roles Prefab")]
static void Create()
{
string folderName = "Assets/Res/Sprites/Roles/";
DirectoryInfo folder = new DirectoryInfo(folderName);
FileInfo[] files = folder.GetFiles();
int success = 0;
foreach (FileInfo f in files)
{
success++;
if (f.Extension.ToLower() == ".png")
{
DoCreatePrefab(f.FullName.Replace("\\", "/").Replace(Application.dataPath, "Assets"), f.Name.Substring(0, f.Name.Length - 4));
EditorUtility.DisplayProgressBar("创建 Roles Prefab", f.Name, success / 1f / files.Length);
// if (success >= 1) break;
}
}
EditorUtility.ClearProgressBar();
AssetDatabase.Refresh();
}
static void DoCreatePrefab(string file, string filename)
{
string prefabPath = "Assets/Res/Prefabs/NPC/" + filename + ".prefab";
if (File.Exists(prefabPath)) return;
Debug.Log(prefabPath);
string clonePrefabPath = "Assets/Res/Prefabs/NPC/Base.prefab";
GameObject prefabRef = (GameObject)AssetDatabase.LoadMainAssetAtPath(clonePrefabPath);
GameObject prefabObj = (GameObject)PrefabUtility.InstantiatePrefab(prefabRef);
var script = prefabObj.GetComponent<NPC>();
script.skin = filename;
PrefabUtility.UnpackPrefabInstance(prefabObj, PrefabUnpackMode.OutermostRoot, InteractionMode.AutomatedAction);
var spr = prefabObj.GetComponent<SpriteRenderer>();
var assets = AssetDatabase.LoadAllAssetsAtPath(file);
spr.sprite = assets[1] as Sprite;
GameObject obj = PrefabUtility.SaveAsPrefabAsset(prefabObj, prefabPath);
GameObject.DestroyImmediate(prefabObj);
}
用脚本生成 Prefab
?著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 原创:版权?归fclzuzu所有 版本:V1.6.8 整理:李苒晴 特性:很实用的运维工具|网工神器| 前言: 据...
- echo 重定向dst.sh比如从数据库中获取到一个字符串,然后要写到一个文件。a=xxx'ooo'hhhh'ee...