Exemplo n.º 1
0
        public void CreatePrefab(string assetFolder, string name, bool createFolder, Texture2D texture)
        {
            var data   = UVUnwrapData.Instance;
            var prefab = Instantiate(target);

            prefab.name = target.name;
            var mesh = Instantiate(target.sharedMesh);

            mesh.name = prefab.name + "_mesh";
            var mat = Instantiate(target.GetComponent <MeshRenderer>().sharedMaterial);

            mat.name          = prefab.name + "_mat";
            mesh.uv           = data.GenerateUV();
            prefab.sharedMesh = mesh;
            if (createFolder)
            {
                AssetDatabase.CreateFolder(assetFolder, name);
                assetFolder += "/" + name.ToUpperInvariant();
            }
            var meshPath   = assetFolder + "/" + name + "_mesh.asset";
            var prefabPath = assetFolder + "/" + name + ".prefab";
            var matPath    = assetFolder + "/" + name + "_mat.mat";
            var texPath    = assetFolder + "/" + name + "_decal.png";


            PrefabUtility.CreatePrefab(prefabPath, prefab.gameObject);

            HandyEditor.MakeTextureReadable(texture);
            //   System.IO.File.WriteAllBytes(Application.dataPath + texPath.Remove(0, 6), texture.EncodeToPNG());
            AssetDatabase.ImportAsset(texPath);
            var t = Instantiate(texture);

            t.name = prefab.name + "_decal";
            mat.SetTexture("_Decal", t);
            AssetDatabase.AddObjectToAsset(mesh, prefabPath);
            AssetDatabase.AddObjectToAsset(mat, prefabPath);
            AssetDatabase.AddObjectToAsset(t, prefabPath);
            AssetDatabase.ImportAsset(prefabPath);
            EditorUtility.SetDirty(prefab);
            EditorUtility.SetDirty(mesh);



            DestroyImmediate(prefab.gameObject);

            prefab            = AssetDatabase.LoadAssetAtPath <GameObject>(prefabPath).GetComponent <MeshFilter>();
            prefab.sharedMesh = mesh;
            prefab.GetComponent <MeshRenderer>().sharedMaterial = mat;

            target = PrefabUtility.ConnectGameObjectToPrefab(target.gameObject, prefab.gameObject).GetComponent <MeshFilter>();
            Selection.activeGameObject = target.gameObject;
        }
Exemplo n.º 2
0
        public static Texture2D SaveTextureFromSprite(Sprite sprite, bool clear)
        {
            var t      = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height, TextureFormat.ARGB32, true);
            var offset = new Vector2(sprite.rect.x, sprite.rect.y);

            HandyEditor.MakeTextureReadable(sprite.texture);
            for (int x = 0; x < t.width; x++)
            {
                for (int y = 0; y < t.height; y++)
                {
                    var c = sprite.texture.GetPixel((int)(x + offset.x), (int)(y + offset.y));
                    if (clear)
                    {
                        c.a = 0;
                    }
                    t.SetPixel(x, y, c);
                }
            }
            t.Apply();
            return(t);
        }