2

I am working on a project where I am having two Unity Projects that need to communicate with each other. I am trying to solve this by using the .net Remoting Framework. For That I created a dll which both Unity projects will use. The dll consists of:

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();
}

Now I have my Menu - Unity project, acting as the remoting server

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;
    }
}

This Server implements the IObserver Functions NotifyFinished and QueryCreditCount (returns dummy value for the moment) When calling the NotifyFinished function from the client, following error occurs:

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.

Can someone tell me, how to solve this problem?

Thanks in advance,

Hoffmanuel

hoffmanuel
  • 403
  • 1
  • 5
  • 14

1 Answers1

0

After lots of searching, i came to the solution: Using the Loom Unity Package from: Unity Gems entry about threading and using it like mentioned in Unity answers entry about threading:

    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());
        }
    }
hoffmanuel
  • 403
  • 1
  • 5
  • 14
  • This link is no longer available so I flagged this as a duplicate to help others with your problem. – Programmer Dec 26 '16 at 19:01
  • Just a small question, but why am I the duplicate of a question asked years later than me? – hoffmanuel Sep 21 '18 at 09:34
  • Too many reasons. **1.** The Gems plugin package you linked is no longer available like I said in my first comment. When people find this answer, it won't be useful to them. **2.** Your answer never really explained the issue or provide a way to fix it. Your answer is basically saying "use this plugin". It is basically a link only answer. When the linked site is gone, the question and answer will no longer be useful. **3.** Duplicates are not marked based on date but based how detailed or helpful the post is. I consider all these before marking a question a duplicate. – Programmer Sep 21 '18 at 10:06
  • 1
    Ok thank you for your feedback, i will consider this in the future. – hoffmanuel Sep 22 '18 at 11:35