or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

asset-classes.mdasset-export.mdasset-loading.mdbinary-data.mdenumerations.mdfile-formats.mdindex.md

asset-loading.mddocs/

0

# Asset Loading and Environment Management

1

2

Core functionality for loading Unity asset files, directories, and compressed bundles. The Environment class serves as the primary interface for managing multiple asset files and provides unified access to all contained objects.

3

4

## Capabilities

5

6

### Environment Class

7

8

The main class for loading and managing Unity assets. It can load individual files, entire directories, or mixed collections of asset files.

9

10

```python { .api }

11

class Environment:

12

"""

13

Main interface for loading and managing Unity asset files.

14

15

Can load:

16

- Individual asset files (.assets, .unity3d, .bundle)

17

- Directories containing asset files

18

- Compressed bundles and archives

19

- ZIP files containing Unity assets

20

"""

21

22

def __init__(self, *args, fs=None, path=None):

23

"""

24

Initialize Environment with asset files or directories.

25

26

Parameters:

27

- *args: File paths, directory paths, or file-like objects to load

28

- fs: Optional filesystem interface (defaults to local filesystem)

29

- path: Base path for relative file operations

30

"""

31

32

def load_file(self, file, parent=None, name=None, is_dependency=False):

33

"""

34

Load a single asset file into the Environment.

35

36

Parameters:

37

- file: File path, file-like object, or bytes to load

38

- parent: Parent Environment or File object

39

- name: Optional name for the loaded file

40

- is_dependency: Whether this is a dependency file

41

42

Returns:

43

Loaded file object (SerializedFile, BundleFile, or WebFile)

44

"""

45

46

def load_folder(self, path: str):

47

"""

48

Load all files in a directory and subdirectories.

49

50

Parameters:

51

- path: Directory path to scan recursively

52

"""

53

54

def load_files(self, files):

55

"""

56

Load multiple files and merge split files automatically.

57

58

Parameters:

59

- files: List of file paths to load

60

"""

61

62

def save(self, pack="none", out_path="output"):

63

"""

64

Save all modified assets to disk.

65

66

Parameters:

67

- pack: Compression method ("none", "lz4")

68

- out_path: Output directory path

69

"""

70

71

@property

72

def objects(self):

73

"""

74

Get all objects from all loaded files.

75

76

Returns:

77

List[ObjectReader]: All objects in the Environment

78

"""

79

80

@property

81

def container(self):

82

"""

83

Get the container mapping for asset paths.

84

85

Returns:

86

Dict mapping asset paths to objects

87

"""

88

```

89

90

### Loading Functions

91

92

Convenience functions for asset loading with various input types.

93

94

```python { .api }

95

def load(*args, **kwargs):

96

"""

97

Create and return an Environment with loaded assets.

98

99

Parameters:

100

- *args: Same as Environment.__init__

101

- **kwargs: Same as Environment.__init__

102

103

Returns:

104

Environment: Initialized Environment with loaded assets

105

"""

106

```

107

108

### Asset Bundle Decryption

109

110

Support for encrypted asset bundles with custom decryption keys.

111

112

```python { .api }

113

def set_assetbundle_decrypt_key(key):

114

"""

115

Set the decryption key for encrypted asset bundles.

116

117

Parameters:

118

- key: Decryption key as bytes or string

119

"""

120

```

121

122

## Usage Examples

123

124

### Loading Individual Files

125

126

```python

127

import UnityPy

128

129

# Load a single asset file

130

env = UnityPy.Environment("game_data.assets")

131

132

# Load multiple specific files

133

env = UnityPy.Environment("file1.assets", "file2.bundle", "file3.unity3d")

134

135

# Alternative using load function

136

env = UnityPy.load("path/to/asset.bundle")

137

```

138

139

### Loading Directories

140

141

```python

142

import UnityPy

143

144

# Load all assets in a directory

145

env = UnityPy.Environment("Assets/")

146

147

# Load folder with specific path

148

env = UnityPy.Environment()

149

env.load_folder("StreamingAssets/")

150

```

151

152

### Loading ZIP Archives

153

154

```python

155

import UnityPy

156

157

# Load ZIP file containing Unity assets

158

env = UnityPy.Environment("game_assets.zip")

159

160

# Load APK file (which is a ZIP)

161

env = UnityPy.Environment("game.apk")

162

```

163

164

### Working with Encrypted Bundles

165

166

```python

167

import UnityPy

168

169

# Set decryption key before loading

170

UnityPy.set_assetbundle_decrypt_key(b"my_secret_key")

171

172

# Load encrypted bundle

173

env = UnityPy.Environment("encrypted.bundle")

174

```

175

176

### Accessing Loaded Objects

177

178

```python

179

import UnityPy

180

181

env = UnityPy.load("game_assets/")

182

183

# Get all objects

184

all_objects = env.objects

185

print(f"Loaded {len(all_objects)} objects")

186

187

# Access container mapping

188

container = env.container

189

for path, obj in container.items():

190

print(f"Asset path: {path} -> Object ID: {obj.path_id}")

191

192

# Filter objects by type

193

textures = [obj for obj in env.objects if obj.type.name == "Texture2D"]

194

audio_clips = [obj for obj in env.objects if obj.type.name == "AudioClip"]

195

```

196

197

### Saving Modified Assets

198

199

```python

200

import UnityPy

201

202

env = UnityPy.load("original_assets/")

203

204

# Modify some objects...

205

for obj in env.objects:

206

if obj.type.name == "Texture2D":

207

texture = obj.read()

208

texture.name = "modified_" + texture.name

209

obj.save(texture)

210

211

# Save with no compression

212

env.save(pack="none", out_path="modified_assets/")

213

214

# Save with LZ4 compression

215

env.save(pack="lz4", out_path="compressed_assets/")

216

```

217

218

### Advanced Loading Scenarios

219

220

```python

221

import UnityPy

222

from fsspec.implementations.http import HTTPFileSystem

223

224

# Load from HTTP

225

http_fs = HTTPFileSystem()

226

env = UnityPy.Environment("https://example.com/assets.bundle", fs=http_fs)

227

228

# Load with custom base path

229

env = UnityPy.Environment(path="/game/assets/")

230

env.load_file("relative/path/to/asset.bundle")

231

232

# Load split files (automatically merged)

233

env = UnityPy.Environment("large_asset.split0") # Will load all split parts

234

```