Search Unity

How to write cross platfrom conditional compile work for system timer implementation?

Discussion in 'Windows' started by lyh1, May 23, 2014.

  1. lyh1

    lyh1

    Joined:
    Jun 30, 2010
    Posts:
    92
    I am porting a project from ios/android, which use system timer for Socket IO operation.
    I do some dirty work and try to make it just compile. I use macro to seperate code from wp8 and ios/android like following.
    Code (csharp):
    1. public class dummy : MonoBehaviour {
    2.     #if NETFX_CORE  
    3.     System.Windows.Threading.DispatcherTimer timer = null;
    4.     #else
    5.     private System.Timers.Timer timer = null;  
    6.     #endif
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.    
    15.     }
    16. }
    The code look fine in editor and after you build the code, it show error:
    Error building Player: Exception: Error: type `System.Timers.Timer` doesn't exist in target framework. It is referenced from Assembly-CSharp.dll at dummy.

    I should use NETFX_CORE or something else to make the project built?
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    NETFX_CORE trick works only for Windows Store Apps. It won't work for Windows Phone 8 - you'll have to implement it in a separate DLL, rather than in your script.
     
  3. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    992
    Well thats just sad. WP8 is much bigger platform, why was this not done there as the namespace exists for WP8 as well...
     
  4. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Use:

    #if UNITY_WP8 && !UNITY_EDITOR

    that should get you through the editor compile issue. It will compile your original version for editor and then will compile the new version for wp8 using the SDK.
     
  5. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    992
    No that will not work as you can't target the NETFX_CORE namespaces on WP8... only Win8 thus making it basically pointless as you normally use the same code for both. But its fine as I have a working solution that modifies the C# project file and adds in the correct post build cs files.
     
  6. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Ahh, yeah that would be an issue unless you're targeting 8.1 or building a universal app.