Skip to content

Commit

Permalink
helper method for importing asset bundle (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
amazingalek authored Dec 28, 2019
1 parent f5eef66 commit bdb462b
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions OWML.Common/IModAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace OWML.Common
{
public interface IModAssets
{
AssetBundle LoadBundle(string filename);
IModAsset<GameObject> Load3DObject(string objectFilename, string imageFilename);
IModAsset<MeshFilter> LoadMesh(string objectFilename);
IModAsset<MeshRenderer> LoadTexture(string imageFilename);
Expand Down
12 changes: 12 additions & 0 deletions OWML.ModHelper.Assets/ModAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ public ModAssets(IModConsole console, IModManifest manifest)
_objImporter = new ObjImporter();
}

public AssetBundle LoadBundle(string filename)
{
var path = _manifest.FolderPath + filename;
_console.WriteLine("Loading asset bundle from " + path);
var bundle = AssetBundle.LoadFromFile(path);
if (bundle == null)
{
_console.WriteLine("Bundle is null");
}
return bundle;
}

public IModAsset<GameObject> Load3DObject(string objectFilename, string imageFilename)
{
var objectPath = _manifest.FolderPath + objectFilename;
Expand Down
24 changes: 23 additions & 1 deletion OWML.SampleMods/OWML.LoadCustomAssets/LoadCustomAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ public class LoadCustomAssets : ModBehaviour
private AudioSource _shootSound;
private AudioSource _music;
private SaveFile _saveFile;
private GameObject _cube;

private void Start()
{
ModHelper.Console.WriteLine($"In {nameof(LoadCustomAssets)}!");
_saveFile = ModHelper.Storage.Load<SaveFile>("savefile.json");
ModHelper.Console.WriteLine("Ducks shot: " + _saveFile.NumberOfDucks);

var assetBundle = ModHelper.Assets.LoadBundle("cubebundle");
_cube = assetBundle.LoadAsset<GameObject>("Cube");
if (_cube == null)
{
ModHelper.Console.WriteLine("Cube is null");
}

var gunSoundAsset = ModHelper.Assets.LoadAudio("blaster-firing.wav");
gunSoundAsset.OnLoaded += OnGunSoundLoaded;
var duckAsset = ModHelper.Assets.Load3DObject("duck.obj", "duck.png");
Expand Down Expand Up @@ -64,15 +72,24 @@ private void OnEvent(MonoBehaviour behaviour, Events ev)

private void Update()
{
if (_isStarted && Input.GetMouseButtonDown(0))
if (!_isStarted)
{
return;
}
if (Input.GetMouseButtonDown(0))
{
ShootDuck();
}
else if (Input.GetMouseButtonDown(1))
{
CreateCube();
}
}

private void ShootDuck()
{
var duckBody = Instantiate(_duckBody);

duckBody.SetPosition(_playerTransform.position + _playerTransform.forward * 2f);
duckBody.SetRotation(_playerTransform.rotation);
duckBody.SetVelocity(_playerBody.GetVelocity() + _playerTransform.forward * 10f);
Expand All @@ -83,5 +100,10 @@ private void ShootDuck()
ModHelper.Storage.Save(_saveFile, "savefile.json");
}

private void CreateCube()
{
Instantiate(_cube, _playerTransform.position + _playerTransform.forward * 2f, Quaternion.identity);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="cubebundle">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="cubebundle.manifest">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="duck.obj">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Binary file added OWML.SampleMods/OWML.LoadCustomAssets/cubebundle
Binary file not shown.
32 changes: 32 additions & 0 deletions OWML.SampleMods/OWML.LoadCustomAssets/cubebundle.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
ManifestFileVersion: 0
CRC: 3362541067
Hashes:
AssetFileHash:
serializedVersion: 2
Hash: baa36fbae80a9b5ad29fde15f7adb684
TypeTreeHash:
serializedVersion: 2
Hash: a58cdfdbce924fe62f868363af9a01af
HashAppended: 0
ClassTypes:
- Class: 1
Script: {instanceID: 0}
- Class: 4
Script: {instanceID: 0}
- Class: 21
Script: {instanceID: 0}
- Class: 23
Script: {instanceID: 0}
- Class: 33
Script: {instanceID: 0}
- Class: 43
Script: {instanceID: 0}
- Class: 48
Script: {instanceID: 0}
- Class: 65
Script: {instanceID: 0}
- Class: 135
Script: {instanceID: 0}
Assets:
- Assets/Cube.prefab
Dependencies: []
2 changes: 2 additions & 0 deletions createrelease.bat
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ copy "OWML.SampleMods\OWML.LoadCustomAssets\bin\Debug\duck.png" "Release\Mods\OW
copy "OWML.SampleMods\OWML.LoadCustomAssets\bin\Debug\blaster-firing.wav" "Release\Mods\OWML.LoadCustomAssets\blaster-firing.wav"
copy "OWML.SampleMods\OWML.LoadCustomAssets\bin\Debug\spiral-mountain.mp3" "Release\Mods\OWML.LoadCustomAssets\spiral-mountain.mp3"
copy "OWML.SampleMods\OWML.LoadCustomAssets\bin\Debug\savefile.json" "Release\Mods\OWML.LoadCustomAssets\savefile.json"
copy "OWML.SampleMods\OWML.LoadCustomAssets\bin\Debug\cubebundle" "Release\Mods\OWML.LoadCustomAssets\cubebundle"
copy "OWML.SampleMods\OWML.LoadCustomAssets\bin\Debug\cubebundle.manifest" "Release\Mods\OWML.LoadCustomAssets\cubebundle.manifest"

7z a "Release\OWML.zip" "Release"

0 comments on commit bdb462b

Please sign in to comment.