Question by
conan1987 · Nov 01, 2019 at 03:32 PM ·
c#sdkmain thread
How do i resolve UnityException: get_isActiveAndEnabled can only be called from the main thread ?
Hi guys
I am converting a C# SDK to unity it uses dll files , i put them in plugins folder. when i click connect button ( btnConnect_Click void ) i get this error " UnityException: get_isActiveAndEnabled can only be called from the main thread." in the first time i open unity , then the code only gives me (1)(connection type method ) in txtmsg .
Here is my script file
using UnityEngine;
using SunVoteSDK;
using System;
using System.Linq;
using UnityEngine.UI;
using System.Runtime.InteropServices;
public class voting : MonoBehaviour {
SunVoteDll SunVote = null;
public SunVoteDll.ConnectEventCallbackDelegate cecd;
public SunVoteDll.HDParamEventCallbackDelegate hdpecd;
public SunVoteDll.VoteEventCallbackDelegate vecd;
public SunVoteDll.KeyEventCallbackDelegate kecd;
public Text txtMsg;
public Text txtPar;
void Start()
{
if (SunVoteDll.Is64Exe())
{
SunVote = new SunVoteDll64();
Debug.Log("64");
}
else
{
SunVote = new SunVoteDll();
Debug.Log("32");
}
txtMsg.text = SunVote.LicenseA(1, "SUNARS2013").ToString();
//若用下面模式绑定事件,多点几次按钮会报错:对象已释放
//SunVote.SetConnectEventCallBackA(ConnectEventCallBackFunction);
//SunVote.SetVoteEventCallBackA(VoteEventCallBackFunction);
//SunVote.SetKeyEventCallBackA(KeyEventCallBackFunction);
//SunVote.SetHDParamEventCallBackA(HDParamEventCallbackFunction);
cecd = new SunVoteDll.ConnectEventCallbackDelegate(ConnectEventCallBackFunction);
hdpecd = new SunVoteDll.HDParamEventCallbackDelegate(HDParamEventCallbackFunction);
vecd = new SunVoteDll.VoteEventCallbackDelegate(VoteEventCallBackFunction);
// kecd = new SunVoteDll.KeyEventCallbackDelegate(KeyEventCallBackFunction);
SunVote.SetConnectEventCallBackA(cecd);
SunVote.SetHDParamEventCallBackA(hdpecd);
SunVote.SetVoteEventCallBackA(vecd);
SunVote.SetKeyEventCallBackA(kecd);
}
public void HDParamEventCallbackFunction(int iBaseID, int iMode, string sInfo)
{
txtMsg.text += "HDParamEventCallbackFunction iBaseID=" + iBaseID +
" iMode=" + iMode +
" sInfo=" + sInfo + "\r\n";
Debug.Log( sInfo);
}
public void KeyEventCallBackFunction(int iBaseID, int KeyID, string keySN, int iMode, float Time, string sInfo)
{
string s = "VoteEventCallBackFunction BaseID=" + iBaseID +
" KeyID=" + KeyID +
" iMode=" + iMode +
" Time=" + Time +
" sInfo=" + sInfo + "\r\n";
//this.txtMsg.text += s;
txtMsg.text += s;
}
public void ConnectEventCallBackFunction(int iBaseID, int iMode, string sInfo)
{
txtMsg.text += "Connect_onConnectEventCallBack iBaseID=" + iBaseID +
" iMode=" + iMode +
" sInfo=" + sInfo + "\r\n";
}
public void VoteEventCallBackFunction(int iBaseID, int iMode, string sInfo)
{
txtMsg.text += "VoteEventCallBackFunction iBaseID=" + iBaseID +
" iMode=" + iMode +
" sInfo=" + sInfo + "\r\n";
}
public void btnConnect_Click()
{
try
{
int a=SunVote.ConnectA((int)SunVoteDll.ConnectType.conn_type_usb, "");
txtMsg.text += "\r\n";
}
catch (Exception ex)
{
txtMsg.text += ex.ToString();
}
}
public void btnStart_Click()
{
try
{
int n = SunVote.VoteStartA((int)SunVoteDll.VoteType.VoteType_Choice, "1,1,0,0,10,1");
txtMsg.text += n.ToString() + "\r\n";
}
catch (Exception ex)
{
txtMsg.text += ex.ToString();
}
}
public void btnDisconnect_Click()
{
try
{
int n = SunVote.DisconnectA(0);
txtMsg.text += n.ToString() + "\r\n";
}
catch (Exception ex)
{
txtMsg.text += ex.ToString();
}
}
public void btnReadHDParam_Click()
{
try
{
int n = SunVote.ReadHDParamA(0, (int)SunVoteDll.ParamType.BaseStation_Channel);
txtMsg.text += n.ToString() + "\r\n";
}
catch (Exception ex)
{
txtMsg.text += ex.ToString();
}
}
public void btnWriteHDParam_Click()
{
try
{
int n = SunVote.WriteHDParamA(0, (int)SunVoteDll.ParamType.BaseStation_Channel, txtPar.text);
txtMsg.text += n.ToString() + "\r\n";
}
catch (Exception ex)
{
txtMsg.text += ex.ToString();
}
}
public void btnStartTest_Click()
{
try
{
int n = SunVote.VoteStartA((int)SunVoteDll.VoteType.VoteType_KeyPadTest, "2,0,1");
txtMsg.text += n.ToString() + "\r\n";
}
catch (Exception ex)
{
txtMsg.text += ex.ToString();
}
}
public void btnStopTest_Click()
{
try
{
int n = SunVote.VoteStopA();
txtMsg.text += n.ToString() + "\r\n";
}
catch (Exception ex)
{
txtMsg.text += ex.ToString();
}
}
}
and here is the sunvote 64 class
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace SunVoteSDK
{
public class SunVoteDll64 : SunVoteDll
{
private const string DllPath = "Assets/Plugins/sunvote_android/SunVoteSDK_x64.dll";
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern int Connect(int iMode, string sParam);
public override int ConnectA(int iMode, string sParam)
{
return Connect(iMode, sParam);
}
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern int Disconnect(int baseId);
public override int DisconnectA(int baseId)
{
return Disconnect(baseId);
}
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern int WriteHDParam(int BaseID, int iMode, string sSetting);
public override int WriteHDParamA(int BaseID, int iMode, string sSetting)
{
return WriteHDParam(BaseID, iMode, sSetting);
}
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern int ReadHDParam(int BaseID, int iMode);
public override int ReadHDParamA(int BaseID, int iMode)
{
return ReadHDParam(BaseID, iMode);
}
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern int VoteStart(int iMode, string sSetting);
public override int VoteStartA(int iMode, string sSetting)
{
return VoteStart(iMode, sSetting);
}
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern int VoteStop();
public override int VoteStopA()
{
return VoteStop();
}
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern int License(int Mode, string sInfo);
public override int LicenseA(int Mode, string sInfo)
{
return License(Mode, sInfo);
}
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern void SetConnectEventCallBack(ConnectEventCallbackDelegate ConnectEventCallback);
public override void SetConnectEventCallBackA(ConnectEventCallbackDelegate ConnectEventCallback)
{
SetConnectEventCallBack(ConnectEventCallback);
}
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern void SetHDParamEventCallBack(HDParamEventCallbackDelegate HDParamEventCallback);
public override void SetHDParamEventCallBackA(HDParamEventCallbackDelegate HDParamEventCallback)
{
SetHDParamEventCallBack(HDParamEventCallback);
}
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern void SetVoteEventCallBack(VoteEventCallbackDelegate VoteEventCallback);
public override void SetVoteEventCallBackA(VoteEventCallbackDelegate VoteEventCallback)
{
SetVoteEventCallBack(VoteEventCallback);
}
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern void SetKeyEventCallBack(KeyEventCallbackDelegate KeyEventCallback);
public override void SetKeyEventCallBackA(KeyEventCallbackDelegate KeyEventCallback)
{
SetKeyEventCallBack(KeyEventCallback);
}
}
}
Comment