Adding support for custom background images
This change adds support for custom background images. Usage: - Background images are stored in "backgrounds" folder as with jpg or png. - Both 360 degree (equirectangular) and 6-side cubemap images are supported. This is automatically detected based on aspect ratio (with cubemap having 4:3 aspect ratio). - Select the background from Settings. Changes: - The selected background image is persisted in config in this format: "background": "backgrounds/my_background.jpg", - Image is decoded in a background thread (via Android plugin), as Texture2D.LoadImage can cause multi-second freeze on the UI thread. We then compensate for unity (re-ordering coordinate origin and also alpha channel). - Made ground smaller & semi-transparent
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class AppEntry : MonoBehaviour
|
||||
{
|
||||
// Sprite gameobject
|
||||
public GameObject sprite;
|
||||
|
||||
// TMP text
|
||||
public TextMeshProUGUI text;
|
||||
|
||||
// App entry contents
|
||||
public string packageId;
|
||||
public string appName;
|
||||
public string externalIconPath;
|
||||
|
||||
@@ -28,6 +28,9 @@ namespace QuestAppLauncher
|
||||
public const string DownloadRepo_Type_GitHub = "github";
|
||||
public const string DownloadRepo_Default = @"tverona1/QuestAppLauncher_Assets/releases/latest";
|
||||
|
||||
// Background
|
||||
public const string Background_Default = "default";
|
||||
|
||||
/// <summary>
|
||||
/// Grid size
|
||||
/// </summary>
|
||||
@@ -66,6 +69,9 @@ namespace QuestAppLauncher
|
||||
// Whether to auto-download updates
|
||||
public bool autoUpdate = false;
|
||||
|
||||
// Background image path
|
||||
public string background = Background_Default;
|
||||
|
||||
// Github download repos
|
||||
public List<DownloadRepo> downloadRepos = new List<DownloadRepo>();
|
||||
|
||||
|
||||
@@ -34,6 +34,9 @@ namespace QuestAppLauncher
|
||||
// Scroll container game object
|
||||
public GameObject scrollContainer;
|
||||
|
||||
// SKybox handler
|
||||
public SkyboxHandler skyboxHandler;
|
||||
|
||||
// Tab containers
|
||||
public GameObject topTabContainer;
|
||||
public GameObject leftTabContainer;
|
||||
@@ -101,6 +104,12 @@ namespace QuestAppLauncher
|
||||
// Load configuration
|
||||
var config = ConfigPersistence.LoadConfig();
|
||||
|
||||
// Set skybox
|
||||
if (!isRenameMode)
|
||||
{
|
||||
this.skyboxHandler.SetSkybox(config.background);
|
||||
}
|
||||
|
||||
// Process apps in background
|
||||
var apps = await Task.Run(() =>
|
||||
{
|
||||
@@ -377,7 +386,7 @@ namespace QuestAppLauncher
|
||||
var newObj = (GameObject)Instantiate(this.prefabCell, transform);
|
||||
|
||||
// Set app entry info
|
||||
var appEntry = newObj.GetComponent("AppEntry") as AppEntry;
|
||||
var appEntry = newObj.GetComponent<AppEntry>();
|
||||
appEntry.packageId = app.PackageName;
|
||||
appEntry.appName = app.AppName;
|
||||
appEntry.isRenameMode = isRenameMode;
|
||||
@@ -401,8 +410,7 @@ namespace QuestAppLauncher
|
||||
}
|
||||
|
||||
// Set app name in text
|
||||
var text = newObj.transform.Find("AppName").GetComponentInChildren<TextMeshProUGUI>();
|
||||
text.text = app.AppName;
|
||||
appEntry.text.text = app.AppName;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.SceneManagement;
|
||||
@@ -21,7 +22,9 @@ namespace QuestAppLauncher
|
||||
public GameObject gridPopulation;
|
||||
public GameObject show2DToggle;
|
||||
public GameObject autoUpdateToggle;
|
||||
public GameObject skyBoxButton;
|
||||
public DownloadStatusIndicator downloadStatusIndicator;
|
||||
public SkyboxHandler skyboxHandler;
|
||||
|
||||
public Toggle tabsAutoOff;
|
||||
public Toggle tabsAutoTop;
|
||||
@@ -57,6 +60,9 @@ namespace QuestAppLauncher
|
||||
// Load config
|
||||
this.config = ConfigPersistence.LoadConfig();
|
||||
|
||||
// Skybox callback
|
||||
this.skyboxHandler.OnSkyboxSelected = OnSkyboxSelected;
|
||||
|
||||
// Set current cols & rows
|
||||
var colsSlider = this.gridCols.GetComponent<Slider>();
|
||||
colsSlider.value = this.config.gridSize.cols;
|
||||
@@ -75,6 +81,9 @@ namespace QuestAppLauncher
|
||||
// Set 2D toggle
|
||||
this.show2DToggle.GetComponent<Toggle>().SetIsOnWithoutNotify(this.config.show2D);
|
||||
|
||||
// Set skybox button text
|
||||
this.skyBoxButton.GetComponentInChildren<TextMeshProUGUI>().text = SkyboxHandler.GetSkyboxNameFromPath(this.config.background);
|
||||
|
||||
// Set auto-update toggle
|
||||
this.autoUpdateToggle.GetComponent<Toggle>().SetIsOnWithoutNotify(this.config.autoUpdate);
|
||||
|
||||
@@ -142,6 +151,19 @@ namespace QuestAppLauncher
|
||||
}
|
||||
}
|
||||
|
||||
public void OnSkyboxSelected(string skyboxPath)
|
||||
{
|
||||
// Update text
|
||||
this.skyBoxButton.GetComponentInChildren<TextMeshProUGUI>().text = SkyboxHandler.GetSkyboxNameFromPath(skyboxPath);
|
||||
|
||||
// Save config with new skybox selection
|
||||
if (!this.config.background.Equals(skyboxPath, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
this.config.background = skyboxPath;
|
||||
ConfigPersistence.SaveConfig(this.config);
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteRenameFiles()
|
||||
{
|
||||
Debug.Log("Delete Rename files");
|
||||
@@ -166,6 +188,11 @@ namespace QuestAppLauncher
|
||||
rowsText.text = string.Format("{0} Rows", rows);
|
||||
}
|
||||
|
||||
public void ShowSkyboxList()
|
||||
{
|
||||
this.skyboxHandler.ShowList();
|
||||
}
|
||||
|
||||
private bool HasUsageStatsPermissions()
|
||||
{
|
||||
// Check if we have UsageStats permission
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class SkyboxEntry : MonoBehaviour
|
||||
{
|
||||
// TMP text
|
||||
public TextMeshProUGUI text;
|
||||
|
||||
// Relative path to skybox
|
||||
public string path;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6e773b6a61a5a604388f69b6f343f8db
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,383 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace QuestAppLauncher
|
||||
{
|
||||
public class SkyboxHandler : MonoBehaviour
|
||||
{
|
||||
// Max width and height of skybox image.
|
||||
// Anything larger we'll scale down. We restrict it primarily due to memory constraints.
|
||||
const int MaxWidth = 4096;
|
||||
const int MaxHeight = 4096;
|
||||
|
||||
// Skybox selected callback
|
||||
public Action<string> OnSkyboxSelected;
|
||||
|
||||
// Skyview List Container
|
||||
public GameObject skyviewListContainer;
|
||||
|
||||
// Skybox list entry prefab
|
||||
public GameObject prefabSkyboxEntry;
|
||||
|
||||
// Content transform
|
||||
public Transform contentTransform;
|
||||
|
||||
// Default skybox
|
||||
private Material defaultSkybox;
|
||||
|
||||
// Skybox folder
|
||||
private const string SkyboxFolder = "backgrounds";
|
||||
|
||||
// Extension search for images
|
||||
const string JpgExtSearch = "*.jpg";
|
||||
const string PngExtSearch = "*.png";
|
||||
|
||||
/// <summary>
|
||||
/// Show the skybox list dialog
|
||||
/// </summary>
|
||||
public async void ShowList()
|
||||
{
|
||||
// Show the dialog
|
||||
this.skyviewListContainer.SetActive(true);
|
||||
|
||||
// Populate the list
|
||||
await PopulateAsync();
|
||||
}
|
||||
|
||||
public void OnCancel()
|
||||
{
|
||||
// Hide the dialog
|
||||
this.skyviewListContainer.SetActive(false);
|
||||
}
|
||||
|
||||
public void OnHoverEnter(Transform t)
|
||||
{
|
||||
var appEntry = t.gameObject.GetComponent("SkyboxEntry") as SkyboxEntry;
|
||||
if (null != appEntry)
|
||||
{
|
||||
// Enable border
|
||||
EnableBorder(t, true);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnHoverExit(Transform t)
|
||||
{
|
||||
var appEntry = t.gameObject.GetComponent("SkyboxEntry") as SkyboxEntry;
|
||||
if (null != appEntry)
|
||||
{
|
||||
// Disable border
|
||||
EnableBorder(t, false);
|
||||
}
|
||||
}
|
||||
|
||||
public async void OnSelected(Transform t)
|
||||
{
|
||||
var entry = t.gameObject.GetComponent("SkyboxEntry") as SkyboxEntry;
|
||||
if (null != entry)
|
||||
{
|
||||
// Set the skybox
|
||||
SetSkybox(entry.path);
|
||||
this.skyviewListContainer.SetActive(false);
|
||||
|
||||
// Callback if registered
|
||||
if (null != OnSkyboxSelected)
|
||||
{
|
||||
OnSkyboxSelected(entry.path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return skybox name given its path (i.e. filename w/o extension)
|
||||
/// </summary>
|
||||
/// <param name="skyboxPath">Path to skybox image</param>
|
||||
/// <returns></returns>
|
||||
public static string GetSkyboxName(string skyboxPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Path.GetFileNameWithoutExtension(skyboxPath);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogFormat("Error trying to get filename of skybox: {0} ({1})", skyboxPath, e.Message);
|
||||
}
|
||||
|
||||
// Fall back to default
|
||||
return Config.Background_Default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the skybox. Supports either equirectangular or cubemap, auto chosen based on aspect ratio.
|
||||
/// </summary>
|
||||
/// <param name="skyboxPath">Path to skybox image</param>
|
||||
/// <returns></returns>
|
||||
public async Task SetSkybox(string skyboxPath)
|
||||
{
|
||||
Debug.LogFormat("Setting skybox to '{0}'", skyboxPath);
|
||||
|
||||
if (null == this.defaultSkybox)
|
||||
{
|
||||
// Save off the default skybox
|
||||
this.defaultSkybox = RenderSettings.skybox;
|
||||
}
|
||||
|
||||
if (IsDefaultSkybox(skyboxPath))
|
||||
{
|
||||
if (RenderSettings.skybox == this.defaultSkybox)
|
||||
{
|
||||
// Skip if skybox is already the default
|
||||
Debug.LogFormat("Skybox already default, skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Set default skybox
|
||||
SetDefaultSkybox();
|
||||
return;
|
||||
}
|
||||
|
||||
// Read the image
|
||||
int imageHeight = 0;
|
||||
int imageWidth = 0;
|
||||
var image = await Task.Run(() =>
|
||||
{
|
||||
AndroidJNI.AttachCurrentThread();
|
||||
|
||||
try
|
||||
{
|
||||
using (AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
||||
using (AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity"))
|
||||
{
|
||||
// Call Android plugin to load the raw image.
|
||||
var jo = currentActivity.CallStatic<AndroidJavaObject>("loadRawImage", MakeAbsoluteSkymapPath(skyboxPath), MaxHeight, MaxWidth);
|
||||
if (null == jo)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get the width, height and raw image data.
|
||||
imageWidth = jo.Get<int>("width");
|
||||
imageHeight = jo.Get<int>("height");
|
||||
var rawImage = (byte[])(Array)jo.Get<sbyte[]>("rawImage");
|
||||
|
||||
// The image is in ARGB_8888 format (Alpha, Red, Green, Blue - each 1 byte). In addition, (0, 0) coordinates are bottom-left.
|
||||
// Unity expects RGBA (with Alpha as the last byte) and origin at top-left. So we need to compensate for both.
|
||||
|
||||
// Shift alpha
|
||||
for (var i = 0; i < rawImage.Length / 4; i++)
|
||||
{
|
||||
var tmp = rawImage[i * 4 + 3];
|
||||
rawImage[i * 4 + 3] = rawImage[i * 4 + 2];
|
||||
rawImage[i * 4 + 2] = rawImage[i * 4 + 1];
|
||||
rawImage[i * 4 + 1] = rawImage[i * 4];
|
||||
rawImage[i * 4] = tmp;
|
||||
}
|
||||
|
||||
// Swap rows
|
||||
var row = new byte[imageWidth * 4];
|
||||
for (var i = 0; i < imageHeight / 2; i++)
|
||||
{
|
||||
Buffer.BlockCopy(rawImage, i * imageWidth * 4, row, 0, imageWidth * 4);
|
||||
Buffer.BlockCopy(rawImage, (imageHeight - i - 1) * imageWidth * 4, rawImage, i * imageWidth * 4, imageWidth * 4);
|
||||
Buffer.BlockCopy(row, 0, rawImage, (imageHeight - i - 1) * imageWidth * 4, imageWidth * 4);
|
||||
}
|
||||
|
||||
return rawImage;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Fall back to using the apk icon
|
||||
Debug.LogFormat("Error decoding image [{0}]: {1}", skyboxPath, e.Message);
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
AndroidJNI.DetachCurrentThread();
|
||||
}
|
||||
});
|
||||
|
||||
if (null == image)
|
||||
{
|
||||
// Fall back to default skybox
|
||||
SetDefaultSkybox();
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Load the image into a 2D texture. We decode in background thread (above) in Java and load the raw image here
|
||||
// because Texture2D.LoadImage on the main thread can cause significant freezes since it is not async.
|
||||
var texture = new Texture2D(imageWidth, imageHeight, TextureFormat.ARGB32, false);
|
||||
texture.filterMode = FilterMode.Trilinear;
|
||||
texture.anisoLevel = 16;
|
||||
texture.LoadRawTextureData(image);
|
||||
texture.Apply();
|
||||
|
||||
Material material;
|
||||
if (4 * texture.height == 3 * texture.width)
|
||||
{
|
||||
// Texture is a cube map (4:3 aspect ratio).
|
||||
// Load cubemap shader. Also rotate x-axis by 180 degrees to compensate for platform-specific rendering differences
|
||||
// (see https://docs.unity3d.com/Manual/SL-PlatformDifferences.html).
|
||||
Debug.LogFormat("Setting cubemap skybox");
|
||||
material = new Material(Shader.Find("skybox/cube"));
|
||||
material.SetFloat("_RotationX", 180);
|
||||
material.SetTexture("_Tex", CubemapFromTexture2D(texture));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Texture is equirectangular
|
||||
Debug.LogFormat("Setting equirectangular skybox");
|
||||
material = new Material(Shader.Find("skybox/equirectangular"));
|
||||
material.SetTexture("_Tex", texture);
|
||||
}
|
||||
|
||||
RenderSettings.skybox = material;
|
||||
DynamicGI.UpdateEnvironment();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Fall back to default skybox
|
||||
Debug.LogFormat("Exception: {0}", e.Message);
|
||||
SetDefaultSkybox();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets cubemap from a 2D texture (which represents 6-sided cube)
|
||||
/// </summary>
|
||||
/// <param name="texture"></param>
|
||||
/// <returns></returns>
|
||||
private static Cubemap CubemapFromTexture2D(Texture2D texture)
|
||||
{
|
||||
int cubedim = texture.width / 4;
|
||||
Cubemap cube = new Cubemap(cubedim, TextureFormat.ARGB32, false);
|
||||
cube.SetPixels(texture.GetPixels(0, cubedim, cubedim, cubedim), CubemapFace.NegativeX);
|
||||
cube.SetPixels(texture.GetPixels(cubedim, cubedim, cubedim, cubedim), CubemapFace.PositiveZ);
|
||||
cube.SetPixels(texture.GetPixels(2 * cubedim, cubedim, cubedim, cubedim), CubemapFace.PositiveX);
|
||||
cube.SetPixels(texture.GetPixels(3 * cubedim, cubedim, cubedim, cubedim), CubemapFace.NegativeZ);
|
||||
cube.SetPixels(texture.GetPixels(cubedim, 0, cubedim, cubedim), CubemapFace.PositiveY);
|
||||
cube.SetPixels(texture.GetPixels(cubedim, 2 * cubedim, cubedim, cubedim), CubemapFace.NegativeY);
|
||||
cube.Apply();
|
||||
return cube;
|
||||
}
|
||||
|
||||
public void SetDefaultSkybox()
|
||||
{
|
||||
Debug.LogFormat("Setting default skybox");
|
||||
RenderSettings.skybox = this.defaultSkybox;
|
||||
DynamicGI.UpdateEnvironment();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Populates the list of skybox images available for pick from
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task PopulateAsync()
|
||||
{
|
||||
// Get list of skyboxes in background
|
||||
var skyboxes = await Task.Run(() =>
|
||||
{
|
||||
return EnumerateSkyboxFiles();
|
||||
});
|
||||
|
||||
// Clear existing list
|
||||
foreach (Transform child in this.contentTransform)
|
||||
{
|
||||
GameObject.Destroy(child.gameObject);
|
||||
}
|
||||
|
||||
// Populate list of skyboxes
|
||||
foreach(var skybox in skyboxes.OrderBy(key => key.Key))
|
||||
{
|
||||
var newObj = (GameObject)Instantiate(this.prefabSkyboxEntry, this.contentTransform);
|
||||
var entry = newObj.GetComponent<SkyboxEntry>();
|
||||
entry.text.text = skybox.Key;
|
||||
entry.path = skybox.Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct map of backgrond name -> path
|
||||
/// </summary>
|
||||
/// <returns>Returned map</returns>
|
||||
private Dictionary<string, string> EnumerateSkyboxFiles()
|
||||
{
|
||||
var skyboxes = new Dictionary<string, string>();
|
||||
|
||||
// Add default
|
||||
skyboxes[Config.Background_Default] = Config.Background_Default;
|
||||
|
||||
// Enumerate jpg files
|
||||
foreach (var filePath in Directory.GetFiles(
|
||||
GetOrCreateSkymapPath(), JpgExtSearch))
|
||||
{
|
||||
skyboxes[Path.GetFileNameWithoutExtension(filePath)] = MakeRelativeSkymapPath(filePath);
|
||||
}
|
||||
|
||||
// Enumerate png files
|
||||
foreach (var filePath in Directory.GetFiles(
|
||||
GetOrCreateSkymapPath(), PngExtSearch))
|
||||
{
|
||||
skyboxes[Path.GetFileNameWithoutExtension(filePath)] = MakeRelativeSkymapPath(filePath);
|
||||
}
|
||||
|
||||
return skyboxes;
|
||||
}
|
||||
|
||||
private void EnableBorder(Transform t, bool enable)
|
||||
{
|
||||
var border = t.Find("Border");
|
||||
border?.gameObject.SetActive(enable);
|
||||
}
|
||||
|
||||
static private string GetOrCreateSkymapPath()
|
||||
{
|
||||
string path = Path.Combine(UnityEngine.Application.persistentDataPath, SkyboxFolder);
|
||||
Directory.CreateDirectory(path);
|
||||
return path;
|
||||
}
|
||||
|
||||
static private string MakeRelativeSkymapPath(string path)
|
||||
{
|
||||
return path.Substring(UnityEngine.Application.persistentDataPath.Length + 1);
|
||||
}
|
||||
|
||||
static private string MakeAbsoluteSkymapPath(string path)
|
||||
{
|
||||
return Path.Combine(UnityEngine.Application.persistentDataPath, path);
|
||||
}
|
||||
|
||||
static public bool IsDefaultSkybox(string path)
|
||||
{
|
||||
return Config.Background_Default.Equals(path, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
static public string GetSkyboxNameFromPath(string path)
|
||||
{
|
||||
if (IsDefaultSkybox(path))
|
||||
{
|
||||
return Config.Background_Default;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return Path.GetFileNameWithoutExtension(path);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Fall back to default
|
||||
Debug.LogFormat("Error trying to get filename of path: {0} ({1})", path, e.Message);
|
||||
}
|
||||
|
||||
return Config.Background_Default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30eacec5c6a73a14894eea6fafe7a53a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user