Esempio n. 1
0
    /**
     * pass in System.IO.File.WriteAllBytes for parameter fileSaveFunction. This is necessary because on Web Player file saving
     * functions only exist for Editor classes
     */
    public void SaveAtlasToAssetDatabase(Texture2D atlas, string texPropertyName, int atlasNum, Material resMat)
    {
        string prefabPth = AssetDatabase.GetAssetPath(resMat);

        if (prefabPth == null || prefabPth.Length == 0)
        {
            Debug.LogError("Could save atlas. Could not find result material in AssetDatabase.");
            return;
        }
        string baseName       = System.IO.Path.GetFileNameWithoutExtension(prefabPth);
        string folderPath     = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 4);
        string fullFolderPath = Application.dataPath + folderPath.Substring("Assets".Length, folderPath.Length - "Assets".Length);

        string pth = fullFolderPath + baseName + "-" + texPropertyName + "-atlas" + atlasNum + ".png";

        Debug.Log("Created atlas for: " + texPropertyName + " at " + pth);
        //need to create a copy because sometimes the packed atlases are not in ARGB32 format
        Texture2D newTex = SAUtility.createTextureCopy(atlas);
        int       size   = Mathf.Max(newTex.height, newTex.width);

        byte[] bytes = newTex.EncodeToPNG();
        Editor.DestroyImmediate(newTex);


        System.IO.File.WriteAllBytes(pth, bytes);

        AssetDatabase.Refresh();

        string relativePath = folderPath + baseName + "-" + texPropertyName + "-atlas" + atlasNum + ".png";

        SetTextureSize((Texture2D)(AssetDatabase.LoadAssetAtPath(relativePath, typeof(Texture2D))), size);
        SetMaterialTextureProperty(resMat, texPropertyName, relativePath);
    }