2013-04-23 84 views
2

我正在一个项目中,我有两个需要相互沟通的Unity项目。我正在尝试通过使用.net远程处理框架解决此问题。 为此我创建了一个两个Unity项目都将使用的dll。该DLL包括:Unity .net远程处理服务器get_animation只能从主线程调用

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

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

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只能从主线程调用。 加载场景时将从加载线程执行构造函数和字段初始值设定项。 不要在构造函数或字段初始值设定项中使用此函数,而应将初始化代码移动到唤醒或启动函数。

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

由于提前,

Hoffmanuel

回答

0

经过大量的搜索,我来到了解决方案:从使用的织布机统一包装: Unity Gems entry about threading ,并用它喜欢在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()); 
     } 
    } 
+0

此链接不再可用,所以我将其标记为重复以帮助其他人处理您的问题。 – Programmer 2016-12-26 19:01:47