Tag 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);
}

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));
        }
    }
}

Convert Byte Array To Short Array C#

Convert Byte Array To Short Array C#.

/// <summary>
/// This Function Converting byte array to short Array
/// </summary>
/// <param name="source">Source byte array</param>
/// <returns>short Array</returns>
public short[] ConvertByteArrayToShortArray(byte[] source)
{
	short[] destination = new short[source.Length / 2];
	Buffer.BlockCopy(source, 0, destination, 0, destination.Length);
	return destination;
}

Calculate Gray (Mono) Color From RGBA Color

Calculate Gray (Mono) Color From RGBA Color.This function calculating gray mono color.Importance coefficients of RGBA for gray color:
Msdn Importance coefficients of Rgb

public class Color
{
	public float R { get; set; }
	public float G { get; set; }
	public float B { get; set; }
	public float A { get; set; }
}

/// <summary>
/// This function return Mono(Gray Color)
/// </summary>
/// <param name="rgb">RBGA color</param>
/// <returns>Return 4 byte float data</returns>
public float GetGrayColor(Color rgb)
{
     return (0.299f * rgb.R + 0.587f * rgb.G + 0.114f * rgb.B);
}

Call C++ Function From .Net(C#)

Call C++ Function From .Net(C#)

Example of call c++ function from .Net

CppExample.cpp
extern "C" {
	__declspec(dllexport) void CppFunction();
}

extern __declspec(dllexport) void CppFunction()
{
	//This Function can be called from .Net
}

CppExample.dll must be same location with CsClass.exe

CsClass.cs

class CsClass
{
	[DllImport(@"CppExample.dll", CallingConvention = CallingConvention.Cdecl)]
	public static extern void CppFunction();

	public static void Main()
	{
		CppFunction()
	}
}