public static void CreateTexturePrefab(string prefabName)
    {
        List <PBTextureData> datas = PBDataBaseManager.Instance.GetTextureDatas();

        PBTextureData data = datas.FirstOrDefault(s => s.prefab == prefabName);

        try
        {
            GameObject fbx =
                AssetDatabase.LoadAssetAtPath <GameObject>(Path.Combine(BlockPath.Texture_Fbx_Dir,
                                                                        data.model + ".fbx"));


            string   matPath = Path.Combine(BlockPath.Texture_Material_Dir, data.prefab + ".mat");
            Material mat     = AssetDatabase.LoadAssetAtPath <Material>(matPath);
            if (mat == null)
            {
                mat = AssetDatabase.LoadAssetAtPath <Material>(Path.Combine(BlockPath.Texture_Material_Dir,
                                                                            data.material + ".mat"));

                AssetDatabase.CreateAsset(GameObject.Instantiate(mat), matPath);
                mat = AssetDatabase.LoadAssetAtPath <Material>(matPath);
            }

            Texture2D tex =
                AssetDatabase.LoadAssetAtPath <Texture2D>(Path.Combine(BlockPath.Texture_Texture_Dir,
                                                                       data.texture + ".png"));


            mat.mainTexture = tex;

            GameObject go = GameObject.Instantiate(fbx);
            go.name = data.prefab;

            Animator animator = go.GetComponent <Animator>();
            if (animator != null)
            {
                GameObject.DestroyImmediate(animator);
            }

            Renderer render = go.GetComponent <Renderer>();
            render.receiveShadows    = false;
            render.shadowCastingMode = ShadowCastingMode.Off;
            render.sharedMaterials   = new[] { mat };

            if (!Directory.Exists(BlockPath.Texture_Prefab_Dir))
            {
                Directory.CreateDirectory(BlockPath.Texture_Prefab_Dir);
            }
            PrefabUtility.CreatePrefab(BlockPath.Texture_Prefab_Dir + "/" + data.prefab + ".prefab", go,
                                       ReplacePrefabOptions.ReplaceNameBased);
            GameObject.DestroyImmediate(go);
        }
        finally
        {
            AssetDatabase.Refresh();
        }
    }
Ejemplo n.º 2
0
    public void AddTextureData(PBTextureData data)
    {
        PBTextureItem item;

        if (!texDatas.TryGetValue(data.texture, out item))
        {
            item          = new PBTextureItem();
            item.texName  = data.texture;
            item.prefab   = new List <string>();
            item.material = new List <string>();
            texDatas.Add(data.texture, item);
        }
        item.prefab.Add(data.prefab);
        item.material.Add(data.material);
    }
        static void CreateTextureMat()
        {
            List <PBTextureData> datas = PBDataBaseManager.Instance.GetTextureDatas();

            List <string> fbxErrors = new List <string>();
            List <string> matErrors = new List <string>();
            List <string> texErrors = new List <string>();

            try
            {
                for (int i = 0; i < datas.Count; i++)
                {
                    EditorUtility.DisplayProgressBar("生成丝印材质", "", (float)i / datas.Count);

                    PBTextureData data = datas[i];
                    GameObject    fbx  = AssetDatabase.LoadAssetAtPath <GameObject>(Path.Combine(BlockPath.Texture_Fbx_Dir, data.model + ".fbx"));
                    if (fbx == null)
                    {
                        if (!fbxErrors.Contains(data.model))
                        {
                            fbxErrors.Add(data.model);
                        }
                        continue;
                    }

                    string   matPath = Path.Combine(BlockPath.Texture_Material_Dir, data.prefab + ".mat");
                    Material mat     = AssetDatabase.LoadAssetAtPath <Material>(matPath);
                    if (mat == null)
                    {
                        mat = AssetDatabase.LoadAssetAtPath <Material>(Path.Combine(BlockPath.Texture_Material_Dir, data.material + ".mat"));
                        if (mat == null)
                        {
                            if (!matErrors.Contains(data.material))
                            {
                                matErrors.Add(data.material);
                            }
                            continue;
                        }

                        AssetDatabase.CreateAsset(GameObject.Instantiate(mat), matPath);
                        mat = AssetDatabase.LoadAssetAtPath <Material>(matPath);
                    }

                    Texture2D tex = AssetDatabase.LoadAssetAtPath <Texture2D>(Path.Combine(BlockPath.Texture_Texture_Dir, data.texture + ".png"));
                    if (tex == null)
                    {
                        if (!texErrors.Contains(data.texture))
                        {
                            texErrors.Add(data.texture);
                        }
                        continue;
                    }

                    mat.mainTexture = tex;
                }
            }
            finally
            {
                EditorUtility.ClearProgressBar();

                AssetDatabase.Refresh();

                if (fbxErrors.Count > 0)
                {
                    DisplayErrorDialog("找不到以下fbx\n", fbxErrors);
                }
                if (matErrors.Count > 0)
                {
                    DisplayErrorDialog("找不到以下material\n", matErrors);
                }
                if (texErrors.Count > 0)
                {
                    DisplayErrorDialog("找不到以下textures\n", texErrors);
                }
            }
        }