2017-03-06 82 views
0

我有一個樂高EV3。我正在使用API​​在Visual Studio中使用C#。將Unity3d連接到4.0 .NET框架API visual studio程序

我試過了,它看起來像EV3一樣使用unity3d的唯一方法是使用兩個程序並讓它們通信。

Unity使用2.0框架,所以async功能不起作用(我敢肯定,如果任何人可以得到async統一,那會更好)。

所以我試圖使用套接字,我相信我的代碼都很好,但是當我運行我的計算機時都主動拒絕連接。

防火牆關閉,以管理員身份運行。

那麼我該如何解決我的套接字問題,以便我可以與unity3d進行通信,或者我如何與unity3d進行通信?

對不起,代碼是多麼渺茫,但這是我工作的主要代碼,所以我運行這個,我控制我的機器人,但不能發送數據到我在unity3d中運行的程序。

轉到通代碼和// 7777777777777777777777777777777777將展示它停止,當我點擊按鈕3,說積極地在我的電腦拒絕

`using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using Lego.Ev3.Core; 
using Lego.Ev3.Desktop; 
using UnityEngine; 
using System.Data; 
using System.Data.SqlClient; 
using System.Net; 

using System.Net.Sockets; 
using System.Net.Mail; 

namespace leeeego 
{ 
    public class LegoMove : MonoBehaviour 
{ 

    //Brick _brick; 
    int _forward = 40; 
    int _backward = 30; 
    uint _time = 3000; 

    public float moveSpeed; 

    private Vector3 input; 
    // Use this for initialization 
    void Start() 
    { 

    } 


    // Update is called once per frame 
    void Update() 
    { 

     input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 
     input = new Vector3(2, 0, 2); 
     //_brick = new Brick(new UsbCommunication()); 
     // _brick.BrickChanged += _brick_BrickChanged; 
     // _brick.ConnectAsync(); 
     // _brick.DirectCommand.PlayToneAsync(100, 1000, 300); 
    } 

    //private void _brick_BrickChanged(object sender, BrickChangedEventArgs e) 
    //{ 

    //} 
} 

} 
namespace TheBrick 
{ 
public class LegoMove : MonoBehaviour 
{ 

    //Brick _brick; 
    int _forward = 40; 
    int _backward = 30; 
    uint _time = 3000; 

    public float moveSpeed; 

    private Vector3 input; 
    // Use this for initialization 
    void Start() 
    { 

    } 


    // Update is called once per frame 
    void Update() 
    { 

     input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 
     //_brick = new Brick(new UsbCommunication()); 
     // _brick.BrickChanged += _brick_BrickChanged; 
     // _brick.ConnectAsync(); 
     // _brick.DirectCommand.PlayToneAsync(100, 1000, 300); 
    } 

    //private void _brick_BrickChanged(object sender, BrickChangedEventArgs e) 
    //{ 

    //} 
} 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    Brick _brick; 
    int _forward = 50; 
    int _backward = 30; 
    uint _time = 3000; 
    int _x = 0; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private async void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     _brick = new Brick(new UsbCommunication()); 
     _brick.BrickChanged += _brick_BrickChanged; 
     await _brick.ConnectAsync(); 
     await _brick.DirectCommand.PlayToneAsync(10, 1000, 300); 



     TheBrick.Database1DataSet database1DataSet = ((TheBrick.Database1DataSet)(this.FindResource("database1DataSet"))); 
     // Load data into the table Table. You can modify this code as needed. 
     TheBrick.Database1DataSetTableAdapters.TableTableAdapter database1DataSetTableTableAdapter = new TheBrick.Database1DataSetTableAdapters.TableTableAdapter(); 
     database1DataSetTableTableAdapter.Fill(database1DataSet.Table); 
     System.Windows.Data.CollectionViewSource tableViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tableViewSource"))); 
     tableViewSource.View.MoveCurrentToFirst(); 
    } 

    private void _brick_BrickChanged(object sender, BrickChangedEventArgs e) 
    { 

    } 

    private async void Move_up_Click(object sender, RoutedEventArgs e) 
    { 
     _brick = new Brick(new UsbCommunication()); 
     _brick.BrickChanged += _brick_BrickChanged; 
     await _brick.ConnectAsync(); 
     await _brick.DirectCommand.PlayToneAsync(100, 1000, 300); 
    } 

    private async void button_Click(object sender, RoutedEventArgs e) 
    { 
     await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync(OutputPort.B | OutputPort.C, _forward, _time, false); 
     _x += 1; 
     //label = _x; 
    } 

    private async void button1_Click(object sender, RoutedEventArgs e) 
    { 
     int a = Score.instace.CurrentScore; 
     int b = 10; 
     if (a == b) 
     { 
      await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync(OutputPort.B | OutputPort.C, _forward, _time, false); 
     } 
    } 


    private void textBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 

    } 

    private async void button2_Click(object sender, RoutedEventArgs e) 
    { 
     SqlConnection cn = new SqlConnection(global::TheBrick.Properties.Settings.Default.Database1ConnectionString); 
     try 
     { 
      string sql = "INSERT INTO Table (moveLego,noiseLego) values(" + txtMove.Text + "," + txtNoise.Text + ")"; 
      SqlCommand exesql = new SqlCommand(sql, cn); 
      cn.Open(); 
      exesql.ExecuteNonQuery(); 


     } 
     catch 
     { 

     } 
     finally 
     { 
      cn.Close(); 
     } 
     _forward = Convert.ToInt32(txtMove.Text); 
     _time = Convert.ToUInt32(txtNoise.Text); 

     await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync  (OutputPort.B  | OutputPort.C, _forward, _time, false); 
    } 

    private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 

    } 

    string serverIP = "localhost"; 
    int port = 8080; 

    private async void button3_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      TcpClient client = new TcpClient(serverIP, port);//777777777777777777777777 

      int bytecount = Encoding.ASCII.GetByteCount(txtMove.Text); 

      byte[] sendData = new byte[bytecount]; 

      sendData = Encoding.ASCII.GetBytes(txtMove.Text); 

      NetworkStream stream = client.GetStream(); 

      stream.Write(sendData, 0, sendData.Length); 

      stream.Close(); 
      client.Close(); 
     } 
     catch 
     { 
      _forward = Convert.ToInt32(txtMove.Text); 
      _time = Convert.ToUInt32(txtNoise.Text); 

      await _brick.DirectCommand.TurnMotorAtPowerForTimeAsync(OutputPort.B | OutputPort.C, _forward, _time, false); 

     } 




    } 
    } 
}` 
+0

你有什麼試過?任何代碼示例是什麼使你陷入困境? – GoldBishop

+0

Unity使用Mono,它是dotnet4.0的子集,但只有高達2.0的成員實現。這就是爲什麼linq請求工作的原因 –

+0

您可以嘗試通過類似於https://www.nuget.org/packages/AsyncBridge.Net35/的工具非正式地使用異步/等待。 –

回答

0

團結具有作爲某種安全協議。所以你只能連接Unity程序。 Photon服務器是一種可能的方式,或Unity上的包裝以更高的C#工作#

相關問題