ImageUtils Class - SubnauticaModding/Nautilus GitHub Wiki
Namespace: SMLHelper.V2.Utility
A collection of image loading utility functions that can create Unity objects from image files at runtime.
public static class ImageUtils| LoadTextureFromFile(string filePathToImage, TextureFormat) |
|---|
| LoadSpriteFromFile(string filePathToImage, TextureFormat) |
| LoadSpriteFromTexture(Texture2D texture2D) |
The following example looks for an image file within the {GAME_PATH}/QMods/{MOD_PATH}/Assets path and sets it as the Titanium icon.
using System.IO;
using System.Reflection;
using SMLHelper.V2.Handlers;
using SMLHelper.V2.Utility;
using UnityEngine;
#if SUBNAUTICA
using Sprite = Atlas.Sprite;
#endif
// You can also just use the ImageUtils.LoadSpriteFromFile() method
// to directly convert an image file to a sprite, but it's not shown in
// this example.
[QModCore]
public static class MyMainClass
{
private static Assembly _assembly = Assembly.GetExecutingAssembly();
private static string _assetFolderPath = Path.Combine(Path.GetDirectoryName(_assembly.Location), "Assets");
[QModPatch]
public static void MyPatch()
{
var iconPath = Path.Combine(_assetFolderPath, "new-titanium-icon.png");
if (File.Exists(iconPath))
{
Texture2D texture = ImageUtils.LoadTextureFromfile(iconPath);
Sprite icon = ImageUtils.LoadSpriteFromTexture(texture);
SpriteHandler.RegisterSprite(TechType.Titanium, icon);
}
}
}Creates a new texture from an image file.
public static Texture2D LoadTextureFromFile(string filePathToImage, TextureFormat format = TextureFormat.BC7);-
stringfilePathToImage
The path to the image file. -
TextureFormat format
The texture format. Defaulted to TextureFormat.BC7
We advise against modifying this parameter unless you know what you're doing.
Texture2D if the image was found; otherwise, null.
Creates a new sprite from an image file.
public static Sprite LoadSpriteFromFile(string filePathToImage, TextureFormat format = TextureFormat.BC7);-
stringfilePathToImage
The path to the image file. -
TextureFormat format
The texture format. Defaulted to TextureFormat.BC7.
We advise against modifying this parameter unless you know what you're doing.
If the image was found:
- Subnautica:
Atlas.Sprite - Below Zero: Sprite
otherwise, null.
Creates a new sprite from a texture object.
public static Sprite LoadSpriteFromTexture(Texture2D texture2D);-
Texture2D texture2D
The 2D texture to convert into a sprite.
If the image was found:
- Subnautica:
Atlas.Sprite - Below Zero: Sprite
otherwise, null.