Category Archives: C#

C# Conditional Attribute

#undef TEST_CONDITIONAL

public class TestMonoBehaviour : MonoBehaviour
{
    void Start()
    {
        ConditionalTest(TestFunction());
    }

    [System.Diagnostics.Conditional("TEST_CONDITIONAL")]
    public void ConditionalTest(string testData)
    {
        Debug.Log("ConditionalTest " + testData);
    }

    public string TestFunction()
    {
        Debug.Log("TestFunction");
        return "TestFunction";
    }
}

if use #undef TEST_CONDITIONAL
No log will be write.

if use #define TEST_CONDITIONAL
TestFunction
ConditionalTest

will write to console.

Generic Event Raiser

 public static void FireEvent(this EventHandler eventHandler, object sender, EventArgs e)
{
    if (eventHandler != null)
        eventHadler(sender, e);
}

public static void FireEvent<T>(this EventHandler<T> eventHandler, object sender, T e)
    where T : EventArgs
{
    if (eventHandler != null)
        eventHandler(sender, e);
}

How to Handle Push Notification WP Toast Params

How to Handle Push Notification WP Toast Params

If you want to handle Toast Notification Param , you have to set “” property like this :

/PushNotificationCross.WP;component/ToastHandler.xaml?key0=val0&key1=val1 …

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<wp:Notification xmlns:wp=\"WPNotification\">
   <wp:Toast>
		<wp:Text1>YOUR_TITLE</wp:Text1>
		<wp:Text2>YOUR_SUBTITLE</wp:Text2>
		<wp:Param>/PushNotificationCross.WP;component/ToastHandler.xaml?Toast=Hi</wp:Param>
   </wp:Toast>
</wp:Notification> 

Debug Windows Phone Unity Codes

Debug Windows Phone Unity Codes

1 – Build Visual Studio project for Windows Phone (File -> Build Settings -> Build).
2 – Open Generated Visual Studio Project.
3 – Click FILE -> Add -> Existing Project, navigate to Unity project folder and select Assembly-CSharp.csproj file.
4 – Go to BUILD -> Configuration Manager and make sure ARM platform is selected for your project.
5 – Optionally uncheck Build flag for Assembly-CSharp since it has already been built by Unity.
6 – Add breakpoint(s) to your script file(s).
7 – Make sure phone is connected to PC and Wphone Screen is unlocked.
8 – Make sure debugger type is set to Managed Only (default).
9 – Hit F5 to build, deploy, run and debug your app.

Unity Take Screenshot in Editor

Take Screenshot in Unity3d

using UnityEngine;
using System.Collections;
using System;

public class ScreenShoter : MonoBehaviour
{
    public KeyCode screenShotKeyCode;
	public string filePath;
    void Update()
    {
        if (Input.GetKeyDown(screenShotKeyCode))
        {
            Application.CaptureScreenshot(string.Format("{0}\\ss_{1}x{2}_{3}.jpg",
filePath,Screen.width,Screen.height,System.DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds));
        }
    }
}