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

getting mac address with .net

Discussion in 'Scripting' started by EducaSoft, Oct 12, 2009.

  1. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Hi, I would like to be able to retrieve the MAC address of the first networkcard in a pc.

    I found some information which might be usefull on http://msdn.microsoft.com/en-us/lib...tion.networkinterface.getphysicaladdress.aspx

    There in the system.net.networkinformation.networkinterface namespace I found some information and also a c# piece of code.


    public static void DisplayTypeAndAddress()
    {
    IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    Console.WriteLine("Interface information for {0}.{1} ",
    computerProperties.HostName, computerProperties.DomainName);
    foreach (NetworkInterface adapter in nics)
    {
    IPInterfaceProperties properties = adapter.GetIPProperties();
    Console.WriteLine(adapter.Description);
    Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
    Console.WriteLine(" Interface type .......................... : {0}", adapter.NetworkInterfaceType);
    Console.WriteLine(" Physical Address ........................ : {0}",
    adapter.GetPhysicalAddress().ToString());
    Console.WriteLine(" Is receive only.......................... : {0}", adapter.IsReceiveOnly);
    Console.WriteLine(" Multicast................................ : {0}", adapter.SupportsMulticast);
    Console.WriteLine();
    }
    }



    Could somebody please help me out a little to convert this to a valid unity .CS script so I can start exploring the wonderfull world of .NET (which unfortunately I don't know a lot about at this moment so I wanna learn)



    10000000 thanks for any possible help

    Bart
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    While Unity uses .NET, the implementation is restricted to the classes in the Mono class library. Most of the classes described on the MSDN documentation aren't available in Unity, and I think that applies to what you are looking for here.
     
  3. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    well .NET reflector did seem to find the namespace in the unity3d dlls.

    What IF unity does know that namespace? Would you know how I could access it?


    regards,

    Bart
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Sorry, you're quite right - I've found it now.

    You can use these classes by qualifying them with the full namespace path (ie, System.Net.NetworkInformation..., etc) or by adding another "using" line at the start of the file:-
    Code (csharp):
    1. using System.Net.NetworkInformation;
     
  5. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Sorry in advance for the following stupid question, but I never accessed .NET stuff from unity3d or any other programming language before.


    Could you please be so kind to tell me how I could write that little script so that if I atach it to a GameObject it just writes the mac address to the console?

    I guess its not a lot of lines of code, but I really don't have a clue how to begin at it (because I never did such a thing before) and it would be a good start for me to learn more about accessing .NET functionality from unity3d scripts.

    Kind regards,

    Bart
     
  6. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358
    Here's a version of that script adapted to work for Unity. However - for me, GetAllNetworkInterfaces() returns zero itmes, so no information is displayed. I'm not sure why this is yet, but hopefully this is at least a step in the right direction.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Net.NetworkInformation;
    4. using System;
    5.  
    6. public class NetworkInfo {
    7.    
    8.     public static void DisplayTypeAndAddress()
    9.     {
    10.         //IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
    11.         NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    12.         Debug.Log(nics.Length + " nics");
    13.         //Debug.Log("Interface information for "+computerProperties.HostName+"."+computerProperties.DomainName);
    14.         foreach (NetworkInterface adapter in nics)
    15.         {
    16.             IPInterfaceProperties properties = adapter.GetIPProperties();
    17.             Debug.Log(adapter.Description);
    18.             Debug.Log(String.Empty.PadLeft(adapter.Description.Length, '='));
    19.             Debug.Log("  Interface type .......................... : " + adapter.NetworkInterfaceType);
    20.             Debug.Log("  Physical Address ........................ : " + adapter.GetPhysicalAddress().ToString());
    21.             Debug.Log("  Is receive only.......................... : " + adapter.IsReceiveOnly);
    22.             Debug.Log("  Multicast................................ : " + adapter.SupportsMulticast);
    23.         }
    24.     }
    25. }
    EDIT: Forgot to mention, this is a static function, so put the above script in a c# file called "NetworkInfo". Then call it from anywhere using this:

    Code (csharp):
    1. NetworkInfo.DisplayTypeAndAddress();
     
  7. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    tnx Duck

    Returns also 0 nics here

    Strange, no ?
     
  8. Steven-Walker

    Steven-Walker

    Joined:
    Oct 27, 2010
    Posts:
    38
  9. rodrigotakase

    rodrigotakase

    Joined:
    May 31, 2010
    Posts:
    2
    Hey guys! I found the solution. This code works on MacOS and on iOS. I haven't tested on Android.

    Code (csharp):
    1.  
    2. // Returns the 1st valid Mac Address
    3. function GetMacAddress(){
    4.     var macAdress = "";
    5.     var nics : NetworkInterface[] = NetworkInterface.GetAllNetworkInterfaces();
    6.    
    7.     var i = 0;
    8.     for (var adapter: NetworkInterface in nics){
    9.         var address: PhysicalAddress = adapter.GetPhysicalAddress();
    10.         if(address.ToString() != ""){
    11.             macAdress = address.ToString();
    12.             return macAdress;
    13.         }
    14.     }
    15.     return 0;
    16. }
    17.  

    Cheers! Good Luck on your project!