> For the complete documentation index, see [llms.txt](https://kinematicsoup.gitbook.io/reactor/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinematicsoup.gitbook.io/reactor/api-reference/ks.reactor/ks.reactor.ksbaseassetloader.md).

# Class ksBaseAssetLoader

Namespace: [KS.Reactor](/reactor/api-reference/ks.reactor.md)\
Assembly: KSCommon.dll

Abstract base class for asset loaders that load and cache [ksScriptAsset](/reactor/api-reference/ks.reactor/ks.reactor.ksscriptasset.md)s.

```csharp
public abstract class ksBaseAssetLoader
```

#### Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) â† [ksBaseAssetLoader](/reactor/api-reference/ks.reactor/ks.reactor.ksbaseassetloader.md)

#### Derived

[ksAssetLoader](/reactor/api-reference/ks.reactor.client.unity/ks.reactor.client.unity.ksassetloader.md)

#### Inherited Members

[object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals\(system-object\)), [object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals\(system-object-system-object\)), [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode), [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype), [object.MemberwiseClone()](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone), [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals), [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

## Fields

### m\_cache <a href="#ks_reactor_ksbaseassetloader_m_cache" id="ks_reactor_ksbaseassetloader_m_cache"></a>

Cache of loaded assets. Keys are asset ids.

```csharp
protected Dictionary<uint, WeakReference> m_cache
```

#### Field Value

[Dictionary](https://learn.microsoft.com/dotnet/api/system.collections.generic.dictionary-2)<[uint](https://learn.microsoft.com/dotnet/api/system.uint32), [WeakReference](https://learn.microsoft.com/dotnet/api/system.weakreference)>

### m\_nameToIdMap <a href="#ks_reactor_ksbaseassetloader_m_nametoidmap" id="ks_reactor_ksbaseassetloader_m_nametoidmap"></a>

Maps asset names to a list of all asset ids with that name. It is the responsibility of the derived class to fill this map before assets can be loaded.

```csharp
protected Dictionary<string, List<uint>> m_nameToIdMap
```

#### Field Value

[Dictionary](https://learn.microsoft.com/dotnet/api/system.collections.generic.dictionary-2)<[string](https://learn.microsoft.com/dotnet/api/system.string), [List](https://learn.microsoft.com/dotnet/api/system.collections.generic.list-1)<[uint](https://learn.microsoft.com/dotnet/api/system.uint32)>>

### m\_pathToIdMap <a href="#ks_reactor_ksbaseassetloader_m_pathtoidmap" id="ks_reactor_ksbaseassetloader_m_pathtoidmap"></a>

Map of asset paths to asset ids. It is the responsibility of the derived class to fill this map before assets can be loaded.

```csharp
protected Dictionary<string, uint> m_pathToIdMap
```

#### Field Value

[Dictionary](https://learn.microsoft.com/dotnet/api/system.collections.generic.dictionary-2)<[string](https://learn.microsoft.com/dotnet/api/system.string), [uint](https://learn.microsoft.com/dotnet/api/system.uint32)>

### m\_paths <a href="#ks_reactor_ksbaseassetloader_m_paths" id="ks_reactor_ksbaseassetloader_m_paths"></a>

Map of asset ids to asset paths. It is the responsibility of the derived class to fill this map before assets can be loaded.

```csharp
protected Dictionary<uint, string> m_paths
```

#### Field Value

[Dictionary](https://learn.microsoft.com/dotnet/api/system.collections.generic.dictionary-2)<[uint](https://learn.microsoft.com/dotnet/api/system.uint32), [string](https://learn.microsoft.com/dotnet/api/system.string)>

## Methods

### CacheAsset(uint, ksScriptAsset) <a href="#ks_reactor_ksbaseassetloader_cacheasset_system_uint32_ks_reactor_ksscriptasset" id="ks_reactor_ksbaseassetloader_cacheasset_system_uint32_ks_reactor_ksscriptasset"></a>

Adds an asset to the cache and sets its id and path.

```csharp
protected void CacheAsset(uint assetId, ksScriptAsset asset)
```

#### Parameters

`assetId` [uint](https://learn.microsoft.com/dotnet/api/system.uint32)

Asset id. There must be an associated asset path in [m\_paths](#KS_Reactor_ksBaseAssetLoader_m_paths).

`asset` [ksScriptAsset](/reactor/api-reference/ks.reactor/ks.reactor.ksscriptasset.md)

Asset to cache.

### CreateInstance\<T>(uint, bool) <a href="#ks_reactor_ksbaseassetloader_createinstance__1_system_uint32_system_boolean" id="ks_reactor_ksbaseassetloader_createinstance__1_system_uint32_system_boolean"></a>

Creates a new instance of an asset.

```csharp
public T CreateInstance<T>(uint assetId, bool silent = false) where T : ksScriptAsset
```

#### Parameters

`assetId` [uint](https://learn.microsoft.com/dotnet/api/system.uint32)

Asset id

`silent` [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

If true, don't log warnings if the asset isn't found or is the wrong type.

#### Returns

T

Asset instance, or null if the asset could not be loaded or was the wrong type.

#### Type Parameters

`T`

Type of asset

### CreateInstance\<T>(string) <a href="#ks_reactor_ksbaseassetloader_createinstance__1_system_string" id="ks_reactor_ksbaseassetloader_createinstance__1_system_string"></a>

Creates a new instance of an asset. If there are multiple assets with the same name, creates a new instance of the first one that matches the type `T`.

```csharp
public T CreateInstance<T>(string pathOrName) where T : ksScriptAsset
```

#### Parameters

`pathOrName` [string](https://learn.microsoft.com/dotnet/api/system.string)

Asset path or asset name.

#### Returns

T

#### Type Parameters

`T`

Type of asset

### Get\<T>(uint, bool) <a href="#ks_reactor_ksbaseassetloader_get__1_system_uint32_system_boolean" id="ks_reactor_ksbaseassetloader_get__1_system_uint32_system_boolean"></a>

Gets an asset from the cache, or loads it if it is not in the cache.

```csharp
public T Get<T>(uint assetId, bool silent = false) where T : ksScriptAsset
```

#### Parameters

`assetId` [uint](https://learn.microsoft.com/dotnet/api/system.uint32)

Id of the asset to get.

`silent` [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

If true, don't log warnings if the asset isn't found or is the wrong type.

#### Returns

T

The loaded asset, or null if the asset could not be loaded or was the wrong type.

#### Type Parameters

`T`

Type of asset to get.

### Get\<T>(string) <a href="#ks_reactor_ksbaseassetloader_get__1_system_string" id="ks_reactor_ksbaseassetloader_get__1_system_string"></a>

Gets an asset by path or name. Loads the asset if it is not already loaded. If there are multiple assets with the same name, returns the first one that matches the type `T`.

```csharp
public T Get<T>(string pathOrName) where T : ksScriptAsset
```

#### Parameters

`pathOrName` [string](https://learn.microsoft.com/dotnet/api/system.string)

Asset path or asset name.

#### Returns

T

The loaded asset, or null if the asset could not be loaded or was the wrong type.

#### Type Parameters

`T`

Type of asset to get.

### GetCached(uint) <a href="#ks_reactor_ksbaseassetloader_getcached_system_uint32" id="ks_reactor_ksbaseassetloader_getcached_system_uint32"></a>

Gets an asset from the cache.

```csharp
protected ksScriptAsset GetCached(uint assetId)
```

#### Parameters

`assetId` [uint](https://learn.microsoft.com/dotnet/api/system.uint32)

Asset id

#### Returns

[ksScriptAsset](/reactor/api-reference/ks.reactor/ks.reactor.ksscriptasset.md)

The asset, or null if it was not in the cache.

### GetId(string, bool) <a href="#ks_reactor_ksbaseassetloader_getid_system_string_system_boolean" id="ks_reactor_ksbaseassetloader_getid_system_string_system_boolean"></a>

Gets an asset id from an asset path or name. If there is more than one asset with the same name, logs a warning and returns zero.

```csharp
public uint GetId(string pathOrName, bool fullPathOnly = false)
```

#### Parameters

`pathOrName` [string](https://learn.microsoft.com/dotnet/api/system.string)

Asset path or asset name.

`fullPathOnly` [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

If true, will only try to get the asset id by asset path.

#### Returns

[uint](https://learn.microsoft.com/dotnet/api/system.uint32)

### GetPath(uint) <a href="#ks_reactor_ksbaseassetloader_getpath_system_uint32" id="ks_reactor_ksbaseassetloader_getpath_system_uint32"></a>

Gets an asset path from an asset id.

```csharp
public string GetPath(uint assetId)
```

#### Parameters

`assetId` [uint](https://learn.microsoft.com/dotnet/api/system.uint32)

Asset id

#### Returns

[string](https://learn.microsoft.com/dotnet/api/system.string)

Asset path, or null if there is no asset with that id.

### Load\<T>(uint, bool) <a href="#ks_reactor_ksbaseassetloader_load__1_system_uint32_system_boolean" id="ks_reactor_ksbaseassetloader_load__1_system_uint32_system_boolean"></a>

Loads an asset.

```csharp
protected abstract T Load<T>(uint assetId, bool silent) where T : ksScriptAsset
```

#### Parameters

`assetId` [uint](https://learn.microsoft.com/dotnet/api/system.uint32)

Id of the asset to load.

`silent` [bool](https://learn.microsoft.com/dotnet/api/system.boolean)

If true, don't log warnings if the asset isn't found or is the wrong type.

#### Returns

T

The loaded asset, or null if the asset could not be loaded or was the wrong type.

#### Type Parameters

`T`

Type of asset to load.

### MapNameToId(string, uint) <a href="#ks_reactor_ksbaseassetloader_mapnametoid_system_string_system_uint32" id="ks_reactor_ksbaseassetloader_mapnametoid_system_string_system_uint32"></a>

Adds a mapping between an asset name and an asset id.

```csharp
protected void MapNameToId(string name, uint id)
```

#### Parameters

`name` [string](https://learn.microsoft.com/dotnet/api/system.string)

Asset name

`id` [uint](https://learn.microsoft.com/dotnet/api/system.uint32)

Asset id

### Unload(ksScriptAsset) <a href="#ks_reactor_ksbaseassetloader_unload_ks_reactor_ksscriptasset" id="ks_reactor_ksbaseassetloader_unload_ks_reactor_ksscriptasset"></a>

Removes an asset from the cache, allowing it to be garbage collected when there are no more references to it. Subsequent calls to [Get%60%601](#KS_Reactor_ksBaseAssetLoader_Get_60_601_System_UInt32_2cSystem_Boolean_) will return a different instance of the asset.

```csharp
public bool Unload(ksScriptAsset asset)
```

#### Parameters

`asset` [ksScriptAsset](/reactor/api-reference/ks.reactor/ks.reactor.ksscriptasset.md)

Asset to remove from the cache.

#### Returns

[bool](https://learn.microsoft.com/dotnet/api/system.boolean)

True if the asset was removed from the cache. False if the asset was not in the cache.
