Skip to content

ImageUtils Class

Metious edited this page Jun 29, 2022 · 1 revision

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

Methods

LoadTextureFromFile(string filePathToImage, TextureFormat)
LoadSpriteFromFile(string filePathToImage, TextureFormat)
LoadSpriteFromTexture(Texture2D texture2D)

Examples

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);
		}
	}
}

LoadTextureFromFile(string filePathToImage, TextureFormat)

Creates a new texture from an image file.

public static Texture2D LoadTextureFromFile(string filePathToImage, TextureFormat format = TextureFormat.BC7);

Parameters

  • string filePathToImage
    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.

Returns

Texture2D if the image was found; otherwise, null.

LoadSpriteFromFile(string filePathToImage, TextureFormat)

Creates a new sprite from an image file.

public static Sprite LoadSpriteFromFile(string filePathToImage, TextureFormat format = TextureFormat.BC7);

Parameters

  • string filePathToImage
    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.

Returns

If the image was found:

  • Subnautica: Atlas.Sprite
  • Below Zero: Sprite

otherwise, null.

LoadSpriteFromTexture(Texture2D texture2D)

Creates a new sprite from a texture object.

public static Sprite LoadSpriteFromTexture(Texture2D texture2D);

Parameters

  • Texture2D texture2D
    The 2D texture to convert into a sprite.

Returns

If the image was found:

  • Subnautica: Atlas.Sprite
  • Below Zero: Sprite

otherwise, null.

Please note that some pages are under construction and the links to them will be enabled as they are completed

Home
Quick Start Guide

[Adding]

[Editing]

[General Utilities]

[Language]

Clone this wiki locally