批量创建prefab
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量创建prefab相关的知识,希望对你有一定的参考价值。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using UnityEditor;
public class Test : MonoBehaviour {
public string dicStr="";
// Use this for initialization
void Start () {
NewSpriteRender ();
}
public void NewSpriteRender()
{
if(dicStr.Equals("")==true)
return;
string allpathT = Application.dataPath +"/Prefab/"+dicStr;
if(Directory.Exists(allpathT)==true)
{
return;
}
Debug.Log (allpathT);
Sprite [] sp = Resources.LoadAll<Sprite> (dicStr+"/");
Debug.Log (sp.Length);
string allpath = Application.dataPath +"/Resources/"+dicStr+"Prefab";
Debug.LogError("_________allpath:"+allpath);
if (Directory.Exists (allpath) == false)
{
Directory.CreateDirectory (allpath);
}
for(int i=0;i<sp.Length;i++)
{
GameObject obj = new GameObject (sp[i].name);
obj.layer = LayerMask.NameToLayer ("Default");
obj.AddComponent<SpriteRenderer> ().sprite = sp [i];
string prefabPath = allpath +"/"+sp[i].name+".prefab";
string prefabpath = prefabPath.Substring (prefabPath.IndexOf("Assets"));
Debug.Log (prefabpath);
#if UNITY_EDITOR
PrefabUtility.CreatePrefab(prefabpath,obj);
#endif
}
}
// Update is called once per frame
void Update () {
}
}
以上是关于批量创建prefab的主要内容,如果未能解决你的问题,请参考以下文章