I am trying to synchronise a native code (c++ project) as a .dll with a C# unity script. Native code:
Audio.h:
#pragma once
#include <iostream>
#define EXPORT __declspec(dllexport)
extern "C" {
EXPORT void initialize(std::string soundFilePaths[]);
EXPORT void setSourcePosition(std::string soundFilePath, float x, float y, float z);
EXPORT void play();
EXPORT void stop();
EXPORT void setListenerRotation(float x, float y, float z);
}
class Audio {
public:
static Audio& instance() { // Singleton
static Audio INSTANCE;
return INSTANCE;
}
void initialize(std::string soundFilePaths[]);
void setSourcePosition(std::string soundFilePath, float x, float y, float z);
void play();
void stop();
void setListenerRotation(float x, float y, float z);
~Audio();
private:
Audio();
};
Audio.cpp:
#include "Audio.h"
extern "C" {
void initialize(std::string soundFilePaths[]) {
Audio::instance().initialize(soundFilePaths);
}
void setSourcePosition(std::string soundFilePath, float x, float y, float z) {
Audio::instance().setSourcePosition(soundFilePath, x, y, z);
}
void play() {
Audio::instance().play();
}
void stop() {
Audio::instance().stop();
}
void setListenerRotation(float x, float y, float z) {
Audio::instance().setListenerRotation(x, y, z);
}
}
Audio::Audio()
{
}
Audio::~Audio()
{
}
void Audio::initialize(std::string soundFilePaths[])
{
}
void Audio::setSourcePosition(std::string soundFilePath, float x, float y, float z)
{
}
void Audio::play()
{
}
void Audio::stop()
{
}
void Audio::setListenerRotation(float x, float y, float z)
{
}
The Unity C# script is the following:
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class AudioPlugin : MonoBehaviour
{
public AudioContainer[] audioContainers;
//public AudioClip[] audioFiles;
//public Transform[] audioSources;
public Transform headGeometry;
public float rotationSpeed = 50;
public float maxYRotation = 90;
public float minYRotation = -90;
float _currentYRotation;
void Start()
{
_currentYRotation = headGeometry.transform.rotation.eulerAngles.y;
string[] filePaths = GetAllFilePathsFromClips();
AudioPluginConnection.Initialize(filePaths);
AudioPluginConnection.Play();
}
void Update()
{
TurnHeadWithInput();
UpdateListenerRotation();
UpdateSoundPositions();
}
void OnDestroy()
{
AudioPluginConnection.Stop();
}
void TurnHeadWithInput()
{
float horizontal = Input.GetAxis("Horizontal");
horizontal *= Time.deltaTime * rotationSpeed;
_currentYRotation = Mathf.Clamp(_currentYRotation + horizontal, minYRotation, maxYRotation);
Vector3 eulerAngles = headGeometry.rotation.eulerAngles;
eulerAngles.y = _currentYRotation;
headGeometry.rotation = Quaternion.Euler(eulerAngles);
}
void UpdateListenerRotation()
{
Vector3 eulerAngles = headGeometry.rotation.eulerAngles;
AudioPluginConnection.SetListenerRotation(eulerAngles.x, eulerAngles.y, eulerAngles.y);
}
void UpdateSoundPositions()
{
foreach (AudioContainer container in audioContainers)
{
Vector3 position = container.source.position;
AudioPluginConnection.SetSourcePosition(container.filePath, position.x, position.y, position.z);
}
}
string[] GetAllFilePathsFromClips()
{
List<string> audioFilePaths = new List<string>();
foreach (AudioContainer container in audioContainers)
{
audioFilePaths.Add(container.filePath);
}
return audioFilePaths.ToArray();
}
}
[System.Serializable]
public class AudioContainer
{
public AudioClip clip;
public Transform source;
public string filePath { get { return Application.dataPath + "/Audio/" + clip.name + ".wav"; } }
}
public class AudioPluginConnection
{
[DllImport("AudioPlugin", EntryPoint = "test")]
public static extern int Test();
[DllImport("AudioPlugin", EntryPoint = "initialize")]
public static extern void Initialize(string[] soundFilePaths);
[DllImport("AudioPlugin", EntryPoint = "setSourcePosition")]
public static extern void SetSourcePosition(string soundFilePath, float x, float y, float z);
[DllImport("AudioPlugin", EntryPoint = "play")]
public static extern void Play();
[DllImport("AudioPlugin", EntryPoint = "stop")]
public static extern void Stop();
[DllImport("AudioPlugin", EntryPoint = "setListenerRotation")]
public static extern void SetListenerRotation(float x, float y, float z);
}
Every time I try to run the unity scene, the Unity program crashes. I think the problem is in the native code because I don't really know how to export classes and methods to a .dll. My assumption is that maybe string (I have used it as an argument of two methods) doesn't work because the program also crashed when I made a test method with string return value. But I am not sure.
I would like to know why my program is crashing and how to synchronise both codes (c++ and c#).
Thank you in advance