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