Unity 2D Game Core Utilities Integration Guide

Namespace Requirement

Ensure the following directive is included in scripts using these utilities:

using UtilityCore;

Async Scene Loader

Configuration Parameters

Parameter Description
progressText UI Text component displaying load percentage
fillRect RectTransform representing the loading bar fill
fullWidth Target screen width representing 100% completion

Implementation

Attach this script to the root of the loading panel prefab. Instantiate the object before calling the load method.

public GameObject loadingScreenPrefab;

public void InitiateLevelTransition() {
    SceneManager.Reference.NextLevelName = "HubLevel";
    AsyncLoadScene manager = Instantiate(loadingScreenPrefab).GetComponent<AsyncLoadScene>();
}

Binary Data Storage

Available Operations

Operation Description
LoadBinary Parse binary files (.bin) into objects
SaveBinary Serialize objects to binary storage

Usage Example

[System.Serializable]
public class UserProfile {
    public int playerScore = 100;
    public string playerName = "GuestUser";
}

string savePath = Application.persistentDataPath + "/savedata.bin";
BinaryDataSerializer.SaveBinary<UserProfile>(new UserProfile(), savePath);
var data = BinaryDataSerializer.LoadBinary<UserProfile>(savePath);
Debug.Log(data.playerName);

Camera Positioning Controller

Required Attributes

Attribute Description
followTarget Transform reference to track
transitionSpeed Duration required to reach target position (seconds)

Attach the controller script to the Main Camera game object. Assign the desired target transform in the Inspector.

JSON File Handling

Supported Functions

Function Description
ReadFromDisk Deserialize JSON file to object
WriteToDisk Serialize object to JSON file
CreateDocument Initialize empty JSON structure

Code Snippet

[System.Serializable]
public class ConfigSettings {
    public bool enableEffects = true;
    public int difficultyLevel = 1;
}

JsonFileHandler.WriteToFile(ConfigSettings settings, Application.streamingAssetsPath + "/config.json");
Console.WriteLine(JsonFileHandler.ReadFile<ConfigSettings>(Application.streamingAssetsPath + "/config.json").difficultyLevel);

Localization Manager

Configuration Keys

Property Description
languageFilePath Absolute path to the language definision file
currentSelection Active language index (Chinese, English, Japanese)

Setup Instructions

  1. Create an empty GameObject and attach LocalLanguageManager. Set the file path.
  2. Attach LocalLanguageListener to UI Text comopnents that require translation. Set translationKey to match entries in the language config.
  3. Add a ToggleGroup component to the container holding multiple language selector toggles. Bind individual Toggles to the group to prevent conflict.

Global Instance Patterns

Mono-based Singleton

Inherit from the base singleton class directly within your MonoBehaviour.

public class GameManager : BaseMonoSingleton<GameManager> {
    public int currentLevelId = 1;
    void OnEnable() {
        Debug.Log("Game State:", BaseMonoSingleton<GameManager>.Instance.LevelId);
    }
}

Static Accessor Pattern

Utilize a static wrapper class for non-MonoBehavior singletons.

public class NetworkService : UnityEngine.MonoBehaviour {
    // Standard MonoBehaviour logic
    void Start() {
        var instance = StaticSingleInstance<NetworkService>.Get();
        Debug.Log(instance.ToString());
    }
}

Audio System Control

Array Dependencies

Field Type Requirement
soundLookup string[] Identifiers matching clip names
clipSources AudioClip[] Actual audio assets corresponding by index

Ensure both arrays maintain identical ordering. Index i in soundLookup must correspond to Index i in clipSources.

XML Persistence Utility

Data Transfer Methods

Method Description
ExportData Convert object to XML stream
ImportData Parse XML stream into object

Example Implementation

[System.Serializable]
public class PlayerStats {
    public int healthPoints = 100;
    public string characterClass = "Warrior";
}

void SaveGameSession() {
    XmlHelper.Save(new PlayerStats(), Application.persistentDataPath + "/profile.xml");
}

void LoadGameSession() {
    var stats = XmlHelper.Load<PlayerStats>(Application.persistentDataPath + "/profile.xml");
    Debug.Log(stats.healthPoints);
}

Tags: unity C# 2D-Development Utilities Scripting

Posted on Wed, 07 Oct 2026 16:21:47 +0000 by darkvengance