2

我正在做一个项目,我有两个需要相互通信的 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;
    }
}

缓存.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 项目,充当远程服务器

菜单控制器.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(暂时返回 dummy 值)当从客户端调用 NotifyFinished 函数时,出现以下错误:

get_animation 只能从主线程调用。加载场景时,构造函数和字段初始化程序将从加载线程中执行。不要在构造函数或字段初始化程序中使用此函数,而是将初始化代码移至 Awake 或 Start 函数。

有人可以告诉我,如何解决这个问题?

提前致谢,

霍夫曼纽尔

4

1 回答 1

0

经过大量搜索,我找到了解决方案:使用 Loom Unity 包来自: 关于线程的Unity Gems 条目,并像Unity 回答关于线程的条目 中提到的那样使用它:

    void Start()
    {
        var tmp = Loom.Current;
        ...
    }
    //Function called from other Thread
    public void NotifyFinished(int status)
    {
        Debug.Log("Notify");
        try
        {
            if (status == (int)LevelStatusCode.OK)
            {
                Loom.QueueOnMainThread(() =>
                {
                    PresentNameInputController();
                });
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
        }
    }
于 2013-04-24T10:51:17.407 回答