gpt4 book ai didi

c# - unity .net Remoting Server get_animation只能从主线程调用

转载 作者:太空宇宙 更新时间:2023-11-03 16:14:22 25 4
gpt4 key购买 nike

<分区>

我正在开发一个项目,其中有两个需要相互通信的 Unity 项目。我试图通过使用 .net Remoting Framework 来解决这个问题。为此,我创建了两个 Unity 项目都将使用的 dll。 dll 包括:

MyRemotableObject.cs

public class MyRemotableObject : MarshalByRefObject
{
public MyRemotableObject()
{

}
public void NotifyStatusChange(int status)
{
Cache.GetInstance().Status = 0;
}
public int GetCreditCount()
{
return Cache.GetInstance().CreditCount;
}
}

Cache.cs

public class Cache
{
private static Cache myInstance;
public static IObserver Observer;
private Cache()
{

}
public static void Attach(IObserver observer)
{
Observer = observer;
}
public static Cache GetInstance()
{
if(myInstance==null)
{
myInstance = new Cache();
}
return myInstance;
}

public int Status
{
set
{
Observer.NotifyFinished(value);
}
}
public int CreditCount
{
get
{
return Observer.QueryCreditCount();
}
}
}

IObserver.cs

public interface IObserver
{
void NotifyFinished(int status);
int QueryCreditCount();
}

现在我有我的菜单 - Unity 项目,充当远程服务器

MenuController.cs

public class MenuController : MonoBehaviour, IObserver
{
private object lockObject;
List<ControllerBase> controllers;
private MyRemotableObject remotableObject;
private System.ComponentModel.Container components = null;

void Awake()
{
lockObject = new object();
try
{
remotableObject = new MyRemotableObject();

//für fehler: //http://www.mycsharp.de/wbb2/thread.php?postid=199935
//************************************* TCP *************************************//
// using TCP protocol
TcpChannel channel = new TcpChannel(124);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemotableObject), "TargetShooterMenu", WellKnownObjectMode.SingleCall);
//************************************* TCP *************************************//
RemotableObjects.Cache.Attach(this);
}
catch (Exception e)
{
Debug.Log(e.ToString());
}

controllers = new List<ControllerBase>();
foreach (GameObject controllerObject in GameObject.FindGameObjectsWithTag(GlobalNames.Tags.CONTROLLEROBJECT))
{
if (controllerObject.GetComponent<ControllerBase>())
controllers.Add(controllerObject.GetComponent<ControllerBase>());
}
}
delegate void PresentNameInputControllerDelegate(int status);
private void PresentNameInputController(int status)
{
if (status == (int)LevelStatusCode.OK)
foreach (ControllerBase controller in controllers)
{
controller.Hide();
if (controller.GetType() == typeof(NameInputController))
controller.Show();
}
}
public void NotifyFinished(int status)
{
Debug.Log("Notify");
lock (lockObject)
{
PresentNameInputControllerDelegate d = PresentNameInputController;

d(status);
}
}
public int QueryCreditCount()
{
Debug.Log("Query");
return 100;
}
}

此服务器实现了 IObserver 函数 NotifyFinished 和 QueryCreditCount(暂时返回虚拟值)从客户端调用 NotifyFinished 函数时,出现以下错误:

get_animation can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

谁能告诉我,如何解决这个问题?

提前致谢

霍夫曼纽尔

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com