2013-04-23 34 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