Ejemplo n.º 1
0
    private static Texture2D PersistLookupTexture(string assetName, Texture2D tex)
    {
        if (!System.IO.Directory.Exists(kDirectoryName))
        {
            System.IO.Directory.CreateDirectory(kDirectoryName);
        }

        string assetPath = System.IO.Path.Combine(kDirectoryName, AssetHelper.CleanFileName(assetName) + "." + kExtensionName);
        bool   newAsset  = !System.IO.File.Exists(assetPath);

        System.IO.File.WriteAllBytes(assetPath, tex.EncodeToPNG());
        AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);

        TextureImporter texSettings = AssetImporter.GetAtPath(assetPath) as TextureImporter;

        if (!texSettings)
        {
            // workaround for bug when importing first generated texture in the project
            AssetDatabase.Refresh();
            AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
            texSettings = AssetImporter.GetAtPath(assetPath) as TextureImporter;
        }
        texSettings.textureFormat = TextureImporterFormat.AutomaticTruecolor;
        texSettings.wrapMode      = TextureWrapMode.Clamp;
        texSettings.mipmapEnabled = false;        // !
        texSettings.linearTexture = true;         // !

        if (newAsset)
        {
            AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
        }

        AssetDatabase.Refresh();

        Texture2D newTex = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D)) as Texture2D;

        return(newTex);
    }