Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Game in Unity play mode doesn't work but works in build

Discussion in 'Editor & General Support' started by mokhabadi, Jul 10, 2020.

  1. mokhabadi

    mokhabadi

    Joined:
    Jul 25, 2018
    Posts:
    27
    Hello! I have a strange problem with unity that i can't find any clue about the source of that. when i use some mixture of TAP (Async) and EAP and multi-threading, i run into a problem: unity player doesn't work but build works well!
    I don't have a exact sample code, and i'm not a professional at multi-threading and concurrent programming. but i know one thing: my code is correct and woks well in a C# application and also in PC or Android build. but it doesn't work in unity player. i guess it stops when first event get called form another thread.

    in this thread i like to discuss about restrictions of using Asynchronous Programming Patterns in unity and also the unity player limitations over build.

    i must mention that i use https://assetstore.unity.com/packages/tools/integration/log-viewer-12047 in my build but i guess in the first callback event from another thread, debugger doesn't log anything. and i completely get blind and i can't detect exceptions and error or any log!
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    If you can't show the specific code, I'd recommend creating a simplified version of that code. If you're saying that doing a certain multi-threaded operation causes the game not to work in Play Mode, then perhaps you can create a really simple version of that code in a new project to demonstrate the behavior. Maybe just spin up a new thread, and have it do nothing, if that's enough to reproduce the issue. From that point, you could either post the code, or report a bug if it's reliably broken.
     
  3. mokhabadi

    mokhabadi

    Joined:
    Jul 25, 2018
    Posts:
    27
    this is the shortest code that i can provide to show the problem: in unity just create this script and reference a UI text
    Code (CSharp):
    1. using System.Net;
    2. using System.Net.Sockets;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour
    7. {
    8.     public Text text;
    9.     Socket socket;
    10.  
    11.     private void Start()
    12.     {
    13.         socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    14.         socket.Connect(IPAddress.Parse("127.0.0.1"), 11111);
    15.         SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs();
    16.         socketAsyncEventArgs.Completed += SocketAsyncEventArgs_Completed;
    17.         socketAsyncEventArgs.SetBuffer(new byte[4], 0, 4);
    18.         socket.ReceiveAsync(socketAsyncEventArgs);
    19.     }
    20.  
    21.     private void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
    22.     {
    23.         print(e.BytesTransferred);
    24.         text.text = e.Buffer[0].ToString();
    25.         socket.ReceiveAsync(e);
    26.     }
    27. }
    28.  
    then create a windows form app like this:
    Code (CSharp):
    1. using System;
    2. using System.Windows.Forms;
    3. using System.Net;
    4. using System.Net.Sockets;
    5. using System.Timers;
    6.  
    7. namespace WindowsFormsApp1
    8. {
    9.     public partial class Form1 : Form
    10.     {
    11.         Socket socket;
    12.         byte counter;
    13.         public Form1()
    14.         {
    15.             InitializeComponent();
    16.         }
    17.  
    18.         private void Form1_LoadAsync(object sender, EventArgs e)
    19.         {
    20.             Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    21.             listener.Bind(new IPEndPoint(IPAddress.Any, 11111));
    22.             listener.Listen(1);
    23.             socket = listener.Accept();
    24.  
    25.             System.Timers.Timer timer = new System.Timers.Timer(1000d);
    26.             timer.Elapsed += Timer_Elapsed;
    27.             timer.Start();
    28.         }
    29.  
    30.         private void Timer_Elapsed(object sender, ElapsedEventArgs e)
    31.         {
    32.             socket.Send(new byte[] { counter });
    33.             this.Invoke(new Action(() => { listBox1.Items.Add(counter.ToString() + " sent"); }));        
    34.             counter++;
    35.         }
    36.     }
    37. }
    38.  
    this code doesn't work in unity player for me and UI text never get changed but in build it works like a charm!!!
     
  4. mokhabadi

    mokhabadi

    Joined:
    Jul 25, 2018
    Posts:
    27
    I posted shortest possible code! i'm using unity 2019.3.3 in windows 7.
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    Well, you can try submitting a bug report with your repro project. I personally would never try to use Windows Forms within a Unity project, and I have no idea whether that's at all viable. Do you have some particular requirement to use Windows Forms here? That seems very strange to me.

    Anyway, I don't have much advice to offer here, other than finding a different way to do what you're trying to do that's more along the lines of how things are done in Unity.