批量创建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;

		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 () {
	
	}
}

  

posted on 2016-02-15 15:46  602147629  阅读(389)  评论(0编辑  收藏  举报